다음을 통해 공유


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을 호출하십시오.

다음 예제에서는 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 데이터베이스 항목을 처리하려면 먼저 항목을 검색하고 개체 모델에서 직접 편집하세요. 개체를 수정한 후에는 SubmitChangesDataContext를 호출하여 데이터베이스를 업데이트합니다.

다음 예제에서는 런던 출신의 모든 고객이 검색됩니다. 그런 다음 도시의 이름이 "런던"에서 "런던 - 메트로"로 변경됩니다. 마지막으로 데이터베이스 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

참고하십시오