天天看點

如何使用<Beginning ASP.NET 3.5 in C# and VB>書中的示例代碼

如何使用<Beginning ASP.NET 3.5 in C# and VB>書中的示例代碼

以第13章為例:LINQ

在VS2008裡先建立一個website項目,命名為ch13。

如何使用<Beginning ASP.NET 3.5 in C# and VB>書中的示例代碼
如何使用<Beginning ASP.NET 3.5 in C# and VB>書中的示例代碼

然後你就會得到一個這樣的初始目錄:

如何使用<Beginning ASP.NET 3.5 in C# and VB>書中的示例代碼

然後将書中附例代碼中13章的檔案夾下的檔案全部複制到這個路徑中,覆寫掉原本的檔案:

如何使用<Beginning ASP.NET 3.5 in C# and VB>書中的示例代碼

在solution explorer中,找到資料庫檔案,輕按兩下之:

如何使用<Beginning ASP.NET 3.5 in C# and VB>書中的示例代碼

這樣就會在server explorer中打開一個新資料連接配接:

如何使用<Beginning ASP.NET 3.5 in C# and VB>書中的示例代碼

右鍵App_Code檔案夾,選擇添加項目,在新視窗裡選擇LINQ to SQL,按照書中訓示更改名字:

如何使用<Beginning ASP.NET 3.5 in C# and VB>書中的示例代碼

其餘部分直接按照書中提示來做就行了:

如何使用<Beginning ASP.NET 3.5 in C# and VB>書中的示例代碼

不過在編譯項目的時候,多半你會遇到一個錯誤:

“PlanetWroxDataContext”不包含“Reviews”的定義,并且找不到可接受類型為“PlanetWroxDataContext”的第一個參數的擴充方法“Reviews”(是否缺少 using 指令或程式集引用?)

'PlanetWroxDataContext' does not contain a definition for 'Reviews' and no extension method 'Reviews' accepting a first argument of type 'PlanetWroxDataContext' could be found (are you missing a using directive or an assembly reference?)

作者在這本書的官網給予了對這個問題的回答:

在非英文版的VWD中,對于資料表名稱或許不會自動變成複數,是以本來應該是Reviews的,變成了Review,解決辦法有兩個,要麼你自己手動改生成的cs模型,要麼你就用Review先。

參考:LINQ: Chapter 13, the first example cannot be run