LINQ to SQL을 사용하여 할 수 있는 작업

LINQ to SQL에서는 SQL 개발자가 필요로 하는 모든 주요 기능을 지원합니다. 정보를 쿼리하고 테이블에 정보를 삽입, 업데이트 및 삭제할 수 있습니다.

선택

원하는 프로그래밍 언어로 LINQ 쿼리를 작성한 다음, 해당 쿼리를 실행하여 결과를 검색하는 간단한 방법으로 선택(프로젝션) 작업을 수행할 수 있습니다. 필요한 모든 작업은 LINQ to SQL을 통해 사용자에게 익숙한 필수 SQL 작업으로 변환됩니다. 자세한 내용은 LINQ to SQL을 참조하십시오.

다음 예제에서는 London의 고객사 이름이 검색되어 콘솔 창에 표시됩니다.

// 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를 호출합니다.

다음 예제에서는 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()

업데이트

데이터베이스 항목을 Update 하려면 먼저 해당 항목을 검색하고 이를 개체 모델에서 직접 편집합니다. 개체를 수정한 후 SubmitChanges 에서 DataContext 를 호출하여 데이터베이스를 업데이트합니다.

다음 예제에서는 London의 모든 고객들이 검색됩니다. 그런 다음 도시 이름이 "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

참고 항목