2013-04-26 17 views
0

MSDN expalins它作爲初級講座如何使數據庫對象與使用LINQ到SQL

// Northwnd inherits from System.Data.Linq.DataContext. 
Northwnd nw = new Northwnd(@"northwnd.mdf"); 
// or, if you are not using SQL Server Express 
// Northwnd nw = new Northwnd("Database=Northwind;Server=server_name;Integrated Security=SSPI"); 

var companyNameQuery = 
    from cust in nw.Customers 
    where cust.City == "London" 
    select cust.CompanyName; 

foreach (var customer in companyNameQuery) 
{ 
    Console.WriteLine(customer); 
} 

這裏我的數據庫名是馬頭和表是學生與名稱,課程和地址。我的編碼是低於

Madu md = new Madu("Database=Madu;Server=Madu-DV6080;Integrated Security=SSPI"); 

var courseQuery =from stu in md.Student where stu.Address == "London" select stu.Name; 

foreach (var stu in courseQuery) 
{ 
    Console.WriteLine(stu); 
} 

我已經加入「將System.Data.Linq」引用作爲well.But它給了我一個錯誤「的類型或命名空間名稱‘馬頭’找不到(是否缺少使用指令或裝配參考?)「

請幫助我。 Thanx提前..!

+0

你能告訴我們班級馬杜的定義嗎?它是什麼名字空間?什麼組裝? – 2013-04-26 10:12:50

+0

你生成了你的模型嗎,它叫什麼?使用EF和reasearch數據庫的第一種方法,http://msdn.microsoft.com/en-us/data/jj206878.aspx。 – Jodrell 2013-04-26 10:13:40

回答

0

因爲它說,在你發佈的代碼:

// Northwnd inherits from System.Data.Linq.DataContext. 

所以你需要這是最起碼的:

using System.Data.Linq; 

namespace Foo 
{ 
    public class Mabu : DataContext 
    { 
     // Stuff 
    } 
} 

其中「東西」,需要設置您的上下文匹配你正在使用的數據庫。

This MSDN article解釋瞭如何設置您的上下文。

相關問題