The LINQ Project

昨天在微軟那兒逛到的新東西,本想搶先介紹的,可是還沒看完只好作罷.
沒想到今天就已經有人介紹了…
LINQ: a post-modern revolution in software development
預覽.NET 3.0未來版本
以下的範例對 VFP developer 來說,可能會發出會心的一笑.

Northwind db = new Northwind(“c:\\northwind\\northwnd.mdf”);
var q =
from c in db.Customers
where c.City == “London”
select c;
foreach (var cust in q)
Console.WriteLine(“id = {0}, City = {1}”,cust.CustomerID, cust.City);

這跟 VFP 的

select c.* from Customers c where c.City==”London” into array q

有什麼不一樣呢??
接下來得找一些時間裝起來研究一下.
參考資料:
*The LINQ Project
*Samples

VS.Net bug

錯誤訊息:”Refreshing the project failed. Unable to retrieve folder information from the server.”
果然早有人遇到此問題…
這是因為 Visual Studio.Net 無法接受 .svn, 也就是以 ‘.’ 開頭的目錄名稱.
所以他發出哀號.
解決方法:
改下載另外一個特別版本的 tortoisesvn, 他在 checkout 之後,不會使用 “.svn”,而改以 “_svn”.這樣就解決問題了…
參考資料:

‘@’ in c# string literal

為什麼網路上很多範例都會在字串前面加上 ‘@’ 呢??
幸好有c# specification, 這才解答了我的疑惑…
c# specification裡面對此有相當多的描述,但是看下面的範例,應該會很容易瞭解.

string a = “Happy birthday, Joel”; // Happy birthday, Joel
string b = @”Happy birthday, Joel”; // Happy birthday, Joel
string c = “hello \t world”; // hello world
string d = @”hello \t world”; // hello \t world
string e = “Joe said \”Hello\” to me”; // Joe said “Hello” to me
string f = @”Joe said “”Hello”” to me”; // Joe said “Hello” to me
string g = “\\\\server\\share\\file.txt”; // \\server\share\file.txt
string h = @”\\server\share\file.txt”; // \\server\share\file.txt
string i = “one\r\ntwo\r\nthree”;
string j = @”one
two
three”;

mod_aspdotnet

久久沒去Apache網站,想不到 HTTP server 專案下多了一個子專案: cli(.NET).
簡單的說,他就是把 Request 都轉成 ASP.Net 可以接的 WebRequest, 然後再讓 ASP.Net application 去處理. 太神奇了,你可以就這麼讓 Apache 把 IIS 替代掉…
目前只支援 Win32 平台上的 .Net 1.0 / 1.1 / 2.0, 而不支援 rotor, mono
這裡有他的Introduction
早知道就不把家裡的 IIS 裝起來了,嗚嗚嗚…

Warning when new web solution

之前安裝 Visual Studio.Net 的時候,沒照步驟來,裝完 Visual Studio.Net 之後,才去安裝 IIS.
這樣當然,Visual Studio.Net 還是可以用,但是在新增 Web solution 的時候,老是會出現這個錯誤訊息.

The class file ‘WebForm1.aspx.cs’ specified as the codebehind for ‘WebForm1.aspx’ could not be loaded.
Make sure that the codebehind attribute in the page or control directive properly references an existing code behind file.

網路上可以找到不少關於此問題的描述,但是卻沒有 solution.
找了好久好久,終於找到方法了…
請參考這篇討論:VS.Net Architect – no editor available for aspx files
簡單的說,就是移除掉 Visual Studio.Net,.Net framework以及必要的 registry 與殘存的檔案,再重新安裝.

1. HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\7.0
2. HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\7.0
3. HKEY_Local_Machine\Software\Microsoft\.NetFramework
4. HKEY_Local_Machine\Software\Microsoft\Net Framework Setup
5. The installation directory for VS.NET.
6. .\Documents and Settings\[USERNAME]\Local Settings\Application Data\Microsoft\VisualStudio
7. .\Documents and Settings\[USERNAME]\Application Data\Microsoft\VisualStudio
8. c:\program files\microsoft visual studio .Net
9. C:\windows or winnt\Microsoft.Net
Search for and delete all instances of mscoree.dll

但我只有移除掉 Visual Studio.Net,然後移除掉必要的 registry 與殘存的檔案,.

1. HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\7.0
2. HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\7.0
3. The installation directory for VS.NET.
4. .\Documents and Settings\[USERNAME]\Local Settings\Application Data\Microsoft\VisualStudio
5. .\Documents and Settings\[USERNAME]\Application Data\Microsoft\VisualStudio
6. c:\program files\microsoft visual studio .Net

重新安裝之後就 ok 啦~
真的是花了不少功夫.
When I installed Visual Studio.Net, I don’t follow the correct instrution. I installed IIS after Visual Studio.Net installation.
Of course, Visual Studio.Net still worked, but there are some messages when you try to new a web solution:

The class file ‘WebForm1.aspx.cs’ specified as the codebehind for ‘WebForm1.aspx’ could not be loaded.
Make sure that the codebehind attribute in the page or control directive properly references an existing code behind file.

You can find some articles about this question, but no articles provide real solutions.
I spend lots of time, finally I got the solution.
Please refer this discussion:VS.Net Architect – no editor available for aspx files
I try to summary the conclution below:
1. Remove visual studio.net and .Net framework.
2. Remove necessary registry and unremoved files.

1. HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\7.0
2. HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\7.0
3. HKEY_Local_Machine\Software\Microsoft\.NetFramework
4. HKEY_Local_Machine\Software\Microsoft\Net Framework Setup
5. The installation directory for VS.NET.
6. .\Documents and Settings\[USERNAME]\Local Settings\Application Data\Microsoft\VisualStudio
7. .\Documents and Settings\[USERNAME]\Application Data\Microsoft\VisualStudio
8. c:\program files\microsoft visual studio .Net
9. C:\windows or winnt\Microsoft.Net
Search for and delete all instances of mscoree.dll

3. Reinstall.
But I only do these steps:
1. Remove Visual Studio.Net.
2. Remove necessary registry and unremoved files.

1. HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\7.0
2. HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\7.0
3. The installation directory for VS.NET.
4. .\Documents and Settings\[USERNAME]\Local Settings\Application Data\Microsoft\VisualStudio
5. .\Documents and Settings\[USERNAME]\Application Data\Microsoft\VisualStudio
6. c:\program files\microsoft visual studio .Net

3. Reinstall.
And everything seems fine now. 🙂

dotLucene(3)

為了要測知添加/刪除的效能,稍微寫了一些程式來測試.
結果還算令人滿意.
添加與刪除每種各測 10 次.
平均起來,添加只花了約一秒左右.
而刪除只花了約 0.015 秒.
算是很快的了.
測試的程序我想就不用貼上來了.
這一陣子也發現了不錯的系列文章
順便一併貼上來.
dotLucene 系列文章

DataSet and xml

Export DataSet to XML 的方法:

StreamWriter xmlSW = new StreamWriter(“test.xml”, false, Encoding.Default );
ds.WriteXml(xmlSW, XmlWriteMode.WriteSchema);
xmlSW.Close();

第一行很重要,你得指定 encoding, 否則如果資料編碼不對,xml裡面的字就會顯示不出來.
所以如果你的當地編碼是 big5, 而且你的資料庫資料的編碼也是 big5, 那麼你應該使用 Encoding.Default.
Import DataSet from XML 的方法:

StreamReader xmlSR = new StreamReader( fileName, Encoding.Default );
ds.ReadXml( xmlSR, XmlReadMode.ReadSchema );
xmlSR.Close();

同理可證,你怎麼寫出去,就怎麼讀回來,這樣資料的編碼才不會有問題…
這我試了快兩個小時… =_=”
居然沒發現到 Encoding 有個 Default 的屬性,害我用 Encoding.ASCII 在那邊試半天…

UI/worker thread

為了要解決進度顯示的問題,上 MSDN 找了一下,果然得到圓滿答覆.
請直接參考這幾篇吧…

簡單說明一下,主要是應用 .Net framework 本身就提供的架構,呼叫 Control 的 BeginInvoke() 去調用自己的 delegate 來幫你畫 UI, 以避免因為訊息繁忙而來不及更新畫面的問題.
如果要讓它可以被 cancel, 那麼,可以再配合 Event 來達到這個目的.
Cancel 的部份,我後來沒有用 Event 來做,這是我比較偷懶的地方.
對這方面如果還需要更詳細的了解,可以參考侯捷的Win32多緒程式設計.
不過這本書好像絕版了?!

dotLucene(2)

其實,依照 dotLucene 網站所提供的 Tutorial,就已經蠻足夠的了.不過我還是同時參考了源碼裡所附的 Demo.
這邊我就只寫出跟 Tutorial / Demo 不同的地方…
1.在 Document 部分,我參考了 Demo 的寫法, 另外寫了一個 FileDocument, 來產生 IndexWriter 所需要的 Document 類別.

public class FileDocument
{
public static Document Document( string title, string content)
{
// make a new, empty document
Document doc = new Document();
// Add necessary fields
// 這邊要 tokenized, 為了中文…
doc.Add( new Field(“title”, title, Field.Store.YES, Field.Index.TOKENIZED));
// Add the contents of the file to a field named “contents”.
// 原來是用 stream reader 來把整個檔案內容讀進來
// 這邊改成只把欄位資料放進去.
doc.Add( new Field(“contents”, content, Field.Store.YES, Field.Index.TOKENIZED));
// return the document
return doc;
}
private FileDocument()
{
}

2. 我把原來的 QueryParser 改成 MultiFieldQueryParser, 因為我想同時搜索主旨與內容.

string[] fields = { “title”, “contents” };
Query query = MultiFieldQueryParser.Parse( txtQueryString.Text, fields, analyzer );
Hits hits = searcher.Search(query);

大致上就這樣子…
我之後想搞一個進度顯示,讓使用者不會覺得太慢,不過怎麼樣都不行.
這應該就要應用所謂的 UI/worker thread 的技術了吧…
真的是對 Windows form.Net 太不熟了…
這再慢慢摸索吧…

dotLucene(1)

上星期逛網路的時候,不小心讓我給逛到 Lucene
*Lucene:基于Java的全文检索引擎简介
*DotLucene:37行代码实现全文搜索
*DotLucene: Full-Text Search for Your Intranet or Website using 37 Lines of Code
雖然以前就大略知道 Lucene, 但卻搞不太清楚他是幹麼的,逛完這幾篇之後,才知道是幹麼的.
於是就很想應用在紅狐論壇上.
上面的文章都是很不錯的入門文章,但是在我一開始投入的時候,的確讓我有很多困擾:
1.例子多半都是用 File / Directory 當作範例,真的可以用來搜索資料庫嗎?
2.又資料庫本身就有不錯的全文檢索,那麼,還需要這個嗎??
3.DotLuceneFbDirectory: DotLucene Index Storage for Firebird 這篇文章說可以拿 Firebird database 當作 storage, 那麼,我需要參考這篇文章這麼作嗎??
想了兩三天,也仔細看了兩三天…
1. 的確可以, dotLucene 有提供 Document 類別,而此類別可以讓你自由添加 Field 進去,所以我可以直接添加 Table 裡的某些欄位進去. 在 asp.net 上應用時,我可以參考這些我自定的欄位來產生網址,讓使用者可以直接點選網址進入.
2. 像 Microsoft SQL server 的確有提供全文檢索功能,可是據說很費效能. dotLucene 還不錯,搜索的時候速度蠻快的,也沒花什麼資源(用工作管理員觀察),不過這部分還需要詳細的實驗才知道.
3. 仔細看完這篇文章以後,才發現,雖然可以利用 database 當作 storage,可是並不建議,因為效能不高. 可惜的是文章內容並沒有提供效能評比,無法作進一步的比較.
在整個考量以後,決定先以紅狐所提供的論壇資料作為一個練習,也可以順便作一些實驗.