使用 LINQ to SQL 可执行的操作

LINQ to SQL 支持你作为 SQL 开发人员所期望的所有关键功能。 您可以查询表中的信息、在表中插入信息以及更新和删除表中的信息。

选择

通过在自己的编程语言中编写 LINQ 查询,然后执行此查询以检索结果,即可以实现选择(投影)。 LINQ to SQL 自行将所有必要操作转换为你所熟悉的必要 SQL 操作。 有关详细信息,请参阅 LINQ to 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);
}
' Northwnd inherits from System.Data.Linq.DataContext.
Dim nw As New Northwnd("c:\northwnd.mdf")
' or, if you are not using SQL Server Express
' Dim nw As New Northwnd("Database=Northwind;Server=dschwart7;Integrated Security=SSPI")

Dim companyNameQuery = _
    From cust In nw.Customers _
    Where cust.City = "London" _
    Select cust.CompanyName

For Each customer In companyNameQuery
    Console.WriteLine(customer)
Next

插入

若要执行 SQL Insert,只需向您已创建的对象模型添加对象,然后对 SubmitChanges 调用 DataContext即可。

在下面的示例中,通过使用 CustomersInsertOnSubmit表添加了一位新客户以及有关该客户的信息。

// Northwnd inherits from System.Data.Linq.DataContext.
Northwnd nw = new Northwnd(@"northwnd.mdf");

Customer cust = new Customer();
cust.CompanyName = "SomeCompany";
cust.City = "London";
cust.CustomerID = "98128";
cust.PostalCode = "55555";
cust.Phone = "555-555-5555";
nw.Customers.InsertOnSubmit(cust);

// At this point, the new Customer object is added in the object model.
// In LINQ to SQL, the change is not sent to the database until
// SubmitChanges is called.
nw.SubmitChanges();
' Northwnd inherits from System.Data.Linq.DataContext.
Dim nw As New Northwnd("c:\northwnd.mdf")

Dim cust As New Customer With {.CompanyName = "SomeCompany", _
    .City = "London", _
    .CustomerID = 98128, _
    .PostalCode = 55555, .Phone = "555-555-5555"}
nw.Customers.InsertOnSubmit(cust)
' At this point, the new Customer object is added in the object model.
' In LINQ to SQL, the change is not sent to the database until
' SubmitChanges is called.
nw.SubmitChanges()

更新

若要 Update 某一数据库项,首先要检索该项,然后直接在对象模型中编辑它。 在修改了该对象之后,请对 SubmitChanges 调用 DataContext 以更新数据库。

在下面的示例中,检索来自伦敦的所有客户。 然后将其所在城市的名称从“London”更改为“London - Metro”。 最后,调用 SubmitChanges 以将所做的更改发送至数据库。

Northwnd nw = new Northwnd(@"northwnd.mdf");

var cityNameQuery =
    from cust in nw.Customers
    where cust.City.Contains("London")
    select cust;

foreach (var customer in cityNameQuery)
{
    if (customer.City == "London")
    {
        customer.City = "London - Metro";
    }
}
nw.SubmitChanges();
Dim nw As New Northwnd("c:\northwnd.mdf")
Dim cityNameQuery = _
    From cust In nw.Customers _
    Where cust.City.Contains("London") _
    Select cust

For Each customer In cityNameQuery
    If customer.City = "London" Then
        customer.City = "London - Metro"
    End If
Next
nw.SubmitChanges()

正在删除

若要 Delete 某一项,请从其所属集合中移除该项,然后对 SubmitChanges 调用 DataContext 以提交所做的更改。

注意

LINQ to SQL 无法识别级联删除操作。 如果要删除表中具有约束的行,请参阅如何:从数据库中删除行

在下面的示例中,从数据库中检索 CustomerID98128 的客户。 然后,在确认检索到客户行之后,调用 DeleteOnSubmit 以将该对象从集合中移除。 最后,调用 SubmitChanges 以将删除内容转发至数据库。

Northwnd nw = new Northwnd(@"northwnd.mdf");
var deleteIndivCust =
    from cust in nw.Customers
    where cust.CustomerID == "98128"
    select cust;

if (deleteIndivCust.Count() > 0)
{
    nw.Customers.DeleteOnSubmit(deleteIndivCust.First());
    nw.SubmitChanges();
}
Dim nw As New Northwnd("c:\northwnd.mdf")
Dim deleteIndivCust = _
    From cust In nw.Customers _
    Where cust.CustomerID = 98128 _
    Select cust

If deleteIndivCust.Count > 0 Then
    nw.Customers.DeleteOnSubmit(deleteIndivCust.First)
    nw.SubmitChanges()
End If

请参阅