다음을 통해 공유


DataSet에 데이터 로드

업데이트: November 2007

DataSet 개체를 먼저 채워야 LINQ to DataSet을 사용하여 해당 개체를 쿼리할 수 있습니다. DataSet은 여러 방법으로 채울 수 있습니다. 예를 들어, LINQ to SQL를 사용하여 데이터베이스를 쿼리한 다음 그 결과를 DataSet에 로드할 수 있습니다. 자세한 내용은 LINQ to SQL을 참조하십시오.

DataSet에 데이터를 로드하는 일반적인 방법 중에는 DataAdapter 클래스를 사용하여 데이터베이스에서 데이터를 검색하는 방법도 있습니다. 다음 예제에서 이 방법을 보여 줍니다.

예제

이 예제에서는 DataAdapter를 사용하여 AdventureWorks 데이터베이스에서 2002년 판매 정보를 쿼리한 다음 그 결과를 DataSet에 로드합니다. DataSet이 채워지면 LINQ to DataSet을 사용하여 쿼리를 다시 작성할 수 있습니다. 이 예제의 FillDataSet 메서드는 LINQ to DataSet 예제의 예제 쿼리에 사용됩니다. 자세한 내용은 DataSet 쿼리(LINQ to DataSet)를 참조하십시오.

Try
    Dim connectionString As String

    connectionString = "Data Source=localhost;Initial Catalog=AdventureWorks;" & _
    "Integrated Security=true;"

    ' Create a new adapter and give it a query to fetch sales order, contact, 
    ' address, and product information for sales in the year 2002. Point connection 
    ' information to the configuration setting "AdventureWorks".
    Dim da = New SqlDataAdapter( _
    "SELECT SalesOrderID, ContactID, OrderDate, OnlineOrderFlag, " & _
        "TotalDue, SalesOrderNumber, Status, ShipToAddressID, BillToAddressID " & _
        "FROM Sales.SalesOrderHeader " & _
        "WHERE DATEPART(YEAR, OrderDate) = @year; " & _
        "SELECT d.SalesOrderID, d.SalesOrderDetailID, d.OrderQty, " & _
        "d.ProductID, d.UnitPrice " & _
        "FROM Sales.SalesOrderDetail d " & _
        "INNER JOIN Sales.SalesOrderHeader h " & _
        "ON d.SalesOrderID = h.SalesOrderID  " & _
        "WHERE DATEPART(YEAR, OrderDate) = @year; " & _
        "SELECT p.ProductID, p.Name, p.ProductNumber, p.MakeFlag, " & _
        "p.Color, p.ListPrice, p.Size, p.Class, p.Style  " & _
        "FROM Production.Product p; " & _
        "SELECT DISTINCT a.AddressID, a.AddressLine1, a.AddressLine2, " & _
        "a.City, a.StateProvinceID, a.PostalCode " & _
        "FROM Person.Address a " & _
        "INNER JOIN Sales.SalesOrderHeader h " & _
        "ON  a.AddressID = h.ShipToAddressID OR a.AddressID = h.BillToAddressID " & _
        "WHERE DATEPART(YEAR, OrderDate) = @year; " & _
        "SELECT DISTINCT c.ContactID, c.Title, c.FirstName, " & _
        "c.LastName, c.EmailAddress, c.Phone " & _
        "FROM Person.Contact c " & _
        "INNER JOIN Sales.SalesOrderHeader h " & _
        "ON c.ContactID = h.ContactID " & _
        "WHERE DATEPART(YEAR, OrderDate) = @year;", _
    connectionString)

    ' Add table mappings.
    da.SelectCommand.Parameters.AddWithValue("@year", 2002)
    da.TableMappings.Add("Table", "SalesOrderHeader")
    da.TableMappings.Add("Table1", "SalesOrderDetail")
    da.TableMappings.Add("Table2", "Product")
    da.TableMappings.Add("Table3", "Address")
    da.TableMappings.Add("Table4", "Contact")

    da.Fill(ds)

    ' Add data relations.
    Dim orderHeader As DataTable = ds.Tables("SalesOrderHeader")
    Dim orderDetail As DataTable = ds.Tables("SalesOrderDetail")
    Dim co As DataRelation = New DataRelation("SalesOrderHeaderDetail", _
                                    orderHeader.Columns("SalesOrderID"), _
                                    orderDetail.Columns("SalesOrderID"), True)
    ds.Relations.Add(co)

    Dim contact As DataTable = ds.Tables("Contact")
    Dim orderContact As DataRelation = New DataRelation("SalesOrderContact", _
                                    contact.Columns("ContactID"), _
                                    orderHeader.Columns("ContactID"), True)
    ds.Relations.Add(orderContact)
Catch ex As SqlException
    Console.WriteLine("SQL exception occurred: " & ex.Message)
End Try
try
{
    // Create a new adapter and give it a query to fetch sales order, contact, 
    // address, and product information for sales in the year 2002. Point connection 
    // information to the configuration setting "AdventureWorks".
    string connectionString = "Data Source=localhost;Initial Catalog=AdventureWorks;"
        + "Integrated Security=true;";


    SqlDataAdapter da = new SqlDataAdapter(
        "SELECT SalesOrderID, ContactID, OrderDate, OnlineOrderFlag, " +
        "TotalDue, SalesOrderNumber, Status, ShipToAddressID, BillToAddressID " +
        "FROM Sales.SalesOrderHeader " +
        "WHERE DATEPART(YEAR, OrderDate) = @year; " +

        "SELECT d.SalesOrderID, d.SalesOrderDetailID, d.OrderQty, " +
        "d.ProductID, d.UnitPrice " +
        "FROM Sales.SalesOrderDetail d " +
        "INNER JOIN Sales.SalesOrderHeader h " +
        "ON d.SalesOrderID = h.SalesOrderID  " +
        "WHERE DATEPART(YEAR, OrderDate) = @year; " +

        "SELECT p.ProductID, p.Name, p.ProductNumber, p.MakeFlag, " +
        "p.Color, p.ListPrice, p.Size, p.Class, p.Style, p.Weight  " +
        "FROM Production.Product p; " +

        "SELECT DISTINCT a.AddressID, a.AddressLine1, a.AddressLine2, " +
        "a.City, a.StateProvinceID, a.PostalCode " +
        "FROM Person.Address a " +
        "INNER JOIN Sales.SalesOrderHeader h " +
        "ON  a.AddressID = h.ShipToAddressID OR a.AddressID = h.BillToAddressID " +
        "WHERE DATEPART(YEAR, OrderDate) = @year; " +

        "SELECT DISTINCT c.ContactID, c.Title, c.FirstName, " +
        "c.LastName, c.EmailAddress, c.Phone " +
        "FROM Person.Contact c " +
        "INNER JOIN Sales.SalesOrderHeader h " +
        "ON c.ContactID = h.ContactID " +
        "WHERE DATEPART(YEAR, OrderDate) = @year;",
    connectionString);

    // Add table mappings.
    da.SelectCommand.Parameters.AddWithValue("@year", 2002);
    da.TableMappings.Add("Table", "SalesOrderHeader");
    da.TableMappings.Add("Table1", "SalesOrderDetail");
    da.TableMappings.Add("Table2", "Product");
    da.TableMappings.Add("Table3", "Address");
    da.TableMappings.Add("Table4", "Contact");

    // Fill the DataSet.
    da.Fill(ds);

    // Add data relations.
    DataTable orderHeader = ds.Tables["SalesOrderHeader"];
    DataTable orderDetail = ds.Tables["SalesOrderDetail"];
    DataRelation order = new DataRelation("SalesOrderHeaderDetail",
                             orderHeader.Columns["SalesOrderID"],
                             orderDetail.Columns["SalesOrderID"], true);
    ds.Relations.Add(order);

    DataTable contact = ds.Tables["Contact"];
    DataTable orderHeader2 = ds.Tables["SalesOrderHeader"];
    DataRelation orderContact = new DataRelation("SalesOrderContact",
                                    contact.Columns["ContactID"],
                                    orderHeader2.Columns["ContactID"], true);
    ds.Relations.Add(orderContact);
}
catch (SqlException ex)
{
    Console.WriteLine("SQL exception occurred: " + ex.Message);
}

참고 항목

개념

LINQ to DataSet 개요

DataSet 쿼리(LINQ to DataSet)

기타 리소스

LINQ to DataSet 예제