다음을 통해 공유


LINQ to SQL로 할 수 있는 작업

업데이트: November 2007

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

선택

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

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

' Northwnd inherits from System.Data.Linq.DataContext.
Dim nw As New Northwnd("c:\northwnd.mdf")

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

For Each customer In companyNameQuery
    Console.WriteLine(customer)
Next
// Northwnd inherits from System.Data.Linq.DataContext.
Northwnd nw = new Northwnd(@"northwnd.mdf");

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

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

삽입

SQL Insert를 실행하려면 개체를 사용자가 만든 개체 모델에 추가하고 DataContext에서 SubmitChanges를 호출합니다.

다음 예제에서는 InsertOnSubmit을 사용하여 새 고객 및 새 고객에 대한 정보가 Customers 테이블에 추가됩니다.

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

업데이트

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

다음 예제에서는 London의 모든 고객들이 검색됩니다. 그런 다음 도시 이름이 "London"에서 "London - Metro"로 변경됩니다. 마지막으로 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()
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();

삭제

항목을 Delete하려면 항목이 속해 있는 컬렉션에서 항목을 제거한 다음 DataContext에서 SubmitChanges를 호출하여 변경 내용을 커밋합니다.

참고:

LINQ to SQL에서는 하위 삭제 작업을 인식하지 못합니다. 제약 조건이 있는 테이블의 행을 삭제하려면 방법: 데이터베이스에서 행 삭제(LINQ to SQL)를 참조하십시오.

다음 예제에서는 CustomerID가 98128인 고객이 데이터베이스에서 검색됩니다. 그런 다음 고객 열이 검색되었는지 확인한 후 DeleteOnSubmit이 호출되어 컬렉션에서 개체가 제거됩니다. 마지막으로 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
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();
}

참고 항목

개념

LINQ to SQL 개체 모델

시작(LINQ to SQL)

기타 리소스

프로그래밍 가이드(LINQ to SQL)