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を実行するには、前もって作成したオブジェクト モデルにオブジェクトを追加し、 SubmitChangesDataContextを呼び出します。

Customers を使って、新規の顧客の情報を InsertOnSubmitテーブルに追加する方法を次の例に示します。

// 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()

Updating

データベース エントリの Update (更新) を実行するには、目的の項目をまず取得し、それをオブジェクト モデル内で直接編集します。 オブジェクトを変更した後で、 SubmitChangesDataContext を呼び出してデータベースを更新します。

ロンドン出身のすべての顧客を取得する例を次に示します。 その後で、市の名前を "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 (削除) するには、項目を所属先のコレクションから削除してから、 SubmitChangesDataContext を呼び出して、変更をコミットします。

Note

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

関連項目