다음을 통해 공유


DataSet.CreateDataReader 메서드

정의

테이블이 DataTableReader 컬렉션에 나타나는 순서대로 DataTable마다 결과 집합이 하나씩 있는 Tables를 반환합니다.

오버로드

CreateDataReader(DataTable[])

DataTableReader마다 결과 집합이 하나씩 있는 DataTable를 반환합니다.

CreateDataReader()

테이블이 DataTableReader 컬렉션에 나타나는 순서대로 DataTable마다 결과 집합이 하나씩 있는 Tables를 반환합니다.

예제

이 예제에서는 콘솔 애플리케이션을 세 개를 만듭니다 DataTable 인스턴스를 추가 하는 각각에 DataSet합니다. 이 예제에서는 메서드를 CreateDataReader 호출하고 반환 DataTableReader된 의 내용을 표시합니다. 의 결과 집합 DataTableReader 순서는 매개 변수로 전달된 인스턴스의 DataTable 순서에 따라 제어됩니다.

참고

이 예제에서는 오버로드된 버전의 CreateDataReader중 하나를 사용하는 방법을 보여줍니다. 사용할 수 있는 다른 예제를 오버 로드 개별 항목을 참조 하십시오.

static DataTable customerTable;
static DataTable productTable;
static DataTable emptyTable;

static void Main()
{
    DataSet dataSet = new DataSet();

    // Add some DataTables to the DataSet, including
    // an empty DataTable:
    emptyTable = new DataTable();
    productTable = GetProducts();
    customerTable = GetCustomers();

    dataSet.Tables.Add(customerTable);
    dataSet.Tables.Add(emptyTable);
    dataSet.Tables.Add(productTable);
    TestCreateDataReader(dataSet);

    Console.WriteLine("Press any key to continue.");
    Console.ReadKey();
}

private static void TestCreateDataReader(DataSet dataSet)
{
    // Given a DataSet, retrieve a DataTableReader
    // allowing access to all the DataSet's data.
    // Even though the dataset contains three DataTables,
    // this code will only display the contents of two of them,
    // because the code has limited the results to the
    // DataTables stored in the tables array. Because this
    // parameter is declared using the ParamArray keyword,
    // you could also include a list of DataTable instances
    // individually, as opposed to supplying an array of
    // DataTables, as in this example:
    using (DataTableReader reader =
        dataSet.CreateDataReader(productTable, emptyTable))
    {
        do
        {
            if (!reader.HasRows)
            {
                Console.WriteLine("Empty DataTableReader");
            }
            else
            {
                PrintColumns(reader);
            }
            Console.WriteLine("========================");
        } while (reader.NextResult());
    }
}

private static DataTable GetCustomers()
{
    // Create sample Customers table, in order
    // to demonstrate the behavior of the DataTableReader.
    DataTable table = new DataTable();

    // Create two columns, ID and Name.
    DataColumn idColumn = table.Columns.Add("ID", typeof(int));
    table.Columns.Add("Name", typeof(string));

    // Set the ID column as the primary key column.
    table.PrimaryKey = new DataColumn[] { idColumn };

    table.Rows.Add(new object[] { 1, "Mary" });
    table.Rows.Add(new object[] { 2, "Andy" });
    table.Rows.Add(new object[] { 3, "Peter" });
    table.Rows.Add(new object[] { 4, "Russ" });
    table.AcceptChanges();
    return table;
}

private static DataTable GetProducts()
{
    // Create sample Products table, in order
    // to demonstrate the behavior of the DataTableReader.
    DataTable table = new DataTable();

    // Create two columns, ID and Name.
    DataColumn idColumn = table.Columns.Add("ID", typeof(int));
    table.Columns.Add("Name", typeof(string));

    // Set the ID column as the primary key column.
    table.PrimaryKey = new DataColumn[] { idColumn };

    table.Rows.Add(new object[] { 1, "Wireless Network Card" });
    table.Rows.Add(new object[] { 2, "Hard Drive" });
    table.Rows.Add(new object[] { 3, "Monitor" });
    table.Rows.Add(new object[] { 4, "CPU" });
    table.AcceptChanges();
    return table;
}

private static void PrintColumns(DataTableReader reader)
{
    // Loop through all the rows in the DataTableReader
    while (reader.Read())
    {
        for (int i = 0; i < reader.FieldCount; i++)
        {
            Console.Write(reader[i] + " ");
        }
        Console.WriteLine();
    }
}
Private emptyTable As DataTable
Private customerTable As DataTable
Private productTable As DataTable

Sub Main()
  Dim dataSet As New DataSet
  ' Add some DataTables to the DataSet, including
  ' an empty DataTable:

  emptyTable = New DataTable()
  productTable = GetProducts()
  customerTable = GetCustomers()

  dataSet.Tables.Add(customerTable)
  dataSet.Tables.Add(emptyTable)
  dataSet.Tables.Add(productTable)
  TestCreateDataReader(dataSet)

  Console.WriteLine("Press any key to continue.")
  Console.ReadKey()
End Sub

Private Sub TestCreateDataReader(ByVal dataSet As DataSet)
  ' Given a DataSet, retrieve a DataTableReader
  ' allowing access to all the DataSet's data.
  ' Even though the dataset contains three DataTables,
  ' this code will only display the contents of two of them,
  ' because the code has limited the results to the 
  ' DataTables stored in the tables array. Because this
  ' parameter is declared using the ParamArray keyword, 
  ' you could also include a list of DataTable instances 
  ' individually, as opposed to supplying an array of 
  ' DataTables, as in this example:
  Using reader As DataTableReader = _
      dataSet.CreateDataReader(productTable, emptyTable)
    Do
      If Not reader.HasRows Then
        Console.WriteLine("Empty DataTableReader")
      Else
        PrintColumns(reader)
      End If
      Console.WriteLine("========================")
    Loop While reader.NextResult()
  End Using
End Sub

Private Function GetCustomers() As DataTable
  ' Create sample Customers table, in order
  ' to demonstrate the behavior of the DataTableReader.
  Dim table As New DataTable

  ' Create two columns, ID and Name.
  Dim idColumn As DataColumn = table.Columns.Add("ID", GetType(Integer))
  table.Columns.Add("Name", GetType(String))

  ' Set the ID column as the primary key column.
  table.PrimaryKey = New DataColumn() {idColumn}

  table.Rows.Add(New Object() {1, "Mary"})
  table.Rows.Add(New Object() {2, "Andy"})
  table.Rows.Add(New Object() {3, "Peter"})
  table.Rows.Add(New Object() {4, "Russ"})
  table.AcceptChanges()
  Return table
End Function

Private Function GetProducts() As DataTable
  ' Create sample Products table, in order
  ' to demonstrate the behavior of the DataTableReader.
  Dim table As New DataTable

  ' Create two columns, ID and Name.
  Dim idColumn As DataColumn = table.Columns.Add("ID", GetType(Integer))
  table.Columns.Add("Name", GetType(String))

  ' Set the ID column as the primary key column.
  table.PrimaryKey = New DataColumn() {idColumn}

  table.Rows.Add(New Object() {1, "Wireless Network Card"})
  table.Rows.Add(New Object() {2, "Hard Drive"})
  table.Rows.Add(New Object() {3, "Monitor"})
  table.Rows.Add(New Object() {4, "CPU"})
  table.AcceptChanges()
  Return table
End Function

Private Sub PrintColumns( _
   ByVal reader As DataTableReader)

  ' Loop through all the rows in the DataTableReader.
  Do While reader.Read()
    For i As Integer = 0 To reader.FieldCount - 1
      Console.Write(reader(i).ToString() & " ")
    Next
    Console.WriteLine()
  Loop
End Sub

이 예제에서는 콘솔 창에 다음 코드를 표시합니다.

설명

반환 DataTableReader된 내에서 결과 집합의 순서를 확인하기 위해 내의 가 DataTableDataSet 비어 있으면 반환 DataTableReader된 내의 빈 결과 집합으로 표시됩니다.

CreateDataReader(DataTable[])

Source:
DataSet.cs
Source:
DataSet.cs
Source:
DataSet.cs

DataTableReader마다 결과 집합이 하나씩 있는 DataTable를 반환합니다.

public:
 System::Data::DataTableReader ^ CreateDataReader(... cli::array <System::Data::DataTable ^> ^ dataTables);
public System.Data.DataTableReader CreateDataReader (params System.Data.DataTable[] dataTables);
member this.CreateDataReader : System.Data.DataTable[] -> System.Data.DataTableReader
Public Function CreateDataReader (ParamArray dataTables As DataTable()) As DataTableReader

매개 변수

dataTables
DataTable[]

DataTableReader에 반환될 결과 집합의 순서를 제공하는 DataTables의 배열입니다.

반환

결과 집합을 하나 이상 포함하는 DataTableReader이고, 소스 DataTable에 포함된 DataSet 인스턴스에 해당합니다. 반환된 결과 집합은 dataTables 매개 변수에 의해 지정된 순서를 따릅니다.

예제

이 예제에서는 콘솔 애플리케이션을 세 개를 만듭니다 DataTable 인스턴스를 추가 하는 각각에 DataSet합니다. 이 예제에서는 메서드를 CreateDataReader 호출하고 반환 DataTableReader된 의 내용을 표시합니다. 의 결과 집합 DataTableReader 순서는 매개 변수로 전달된 인스턴스의 DataTable 순서에 따라 제어됩니다. 이 예제에서는 콘솔 창에 결과를 표시합니다.

static DataTable customerTable;
static DataTable productTable;
static DataTable emptyTable;

static void Main()
{
    DataSet dataSet = new DataSet();

    // Add some DataTables to the DataSet, including
    // an empty DataTable:
    emptyTable = new DataTable();
    productTable = GetProducts();
    customerTable = GetCustomers();

    dataSet.Tables.Add(customerTable);
    dataSet.Tables.Add(emptyTable);
    dataSet.Tables.Add(productTable);
    TestCreateDataReader(dataSet);

    Console.WriteLine("Press any key to continue.");
    Console.ReadKey();
}

private static void TestCreateDataReader(DataSet dataSet)
{
    // Given a DataSet, retrieve a DataTableReader
    // allowing access to all the DataSet's data.
    // Even though the dataset contains three DataTables,
    // this code will only display the contents of two of them,
    // because the code has limited the results to the
    // DataTables stored in the tables array. Because this
    // parameter is declared using the ParamArray keyword,
    // you could also include a list of DataTable instances
    // individually, as opposed to supplying an array of
    // DataTables, as in this example:
    using (DataTableReader reader =
       dataSet.CreateDataReader(productTable, emptyTable))
    {
        do
        {
            if (!reader.HasRows)
            {
                Console.WriteLine("Empty DataTableReader");
            }
            else
            {
                PrintColumns(reader);
            }
            Console.WriteLine("========================");
        } while (reader.NextResult());
    }
}

private static DataTable GetCustomers()
{
    // Create sample Customers table, in order
    // to demonstrate the behavior of the DataTableReader.
    DataTable table = new DataTable();

    // Create two columns, ID and Name.
    DataColumn idColumn = table.Columns.Add("ID", typeof(int));
    table.Columns.Add("Name", typeof(string));

    // Set the ID column as the primary key column.
    table.PrimaryKey = new DataColumn[] { idColumn };

    table.Rows.Add(new object[] { 1, "Mary" });
    table.Rows.Add(new object[] { 2, "Andy" });
    table.Rows.Add(new object[] { 3, "Peter" });
    table.Rows.Add(new object[] { 4, "Russ" });
    return table;
}

private static DataTable GetProducts()
{
    // Create sample Products table, in order
    // to demonstrate the behavior of the DataTableReader.
    DataTable table = new DataTable();

    // Create two columns, ID and Name.
    DataColumn idColumn = table.Columns.Add("ID", typeof(int));
    table.Columns.Add("Name", typeof(string));

    // Set the ID column as the primary key column.
    table.PrimaryKey = new DataColumn[] { idColumn };

    table.Rows.Add(new object[] { 1, "Wireless Network Card" });
    table.Rows.Add(new object[] { 2, "Hard Drive" });
    table.Rows.Add(new object[] { 3, "Monitor" });
    table.Rows.Add(new object[] { 4, "CPU" });
    return table;
}

private static void PrintColumns(DataTableReader reader)
{
    // Loop through all the rows in the DataTableReader
    while (reader.Read())
    {
        for (int i = 0; i < reader.FieldCount; i++)
        {
            Console.Write(reader[i] + " ");
        }
        Console.WriteLine();
    }
}
Private emptyTable As DataTable
Private customerTable As DataTable
Private productTable As DataTable

Sub Main()
  Dim dataSet As New DataSet
  ' Add some DataTables to the DataSet, including
  ' an empty DataTable:

  emptyTable = New DataTable()
  productTable = GetProducts()
  customerTable = GetCustomers()

  dataSet.Tables.Add(customerTable)
  dataSet.Tables.Add(emptyTable)
  dataSet.Tables.Add(productTable)
  TestCreateDataReader(dataSet)

  Console.WriteLine("Press any key to continue.")
  Console.ReadKey()
End Sub

Private Sub TestCreateDataReader(ByVal dataSet As DataSet)
  ' Given a DataSet, retrieve a DataTableReader
  ' allowing access to all the DataSet's data.
  ' Even though the dataset contains three DataTables,
  ' this code will only display the contents of two of them,
  ' because the code has limited the results to the 
  ' DataTables stored in the tables array. Because this
  ' parameter is declared using the ParamArray keyword, 
  ' you could also include a list of DataTable instances 
  ' individually, as opposed to supplying an array of 
  ' DataTables, as in this example:
  Using reader As DataTableReader = _
    dataSet.CreateDataReader(productTable, emptyTable)
    Do
      If Not reader.HasRows Then
        Console.WriteLine("Empty DataTableReader")
      Else
        PrintColumns(reader)
      End If
      Console.WriteLine("========================")
    Loop While reader.NextResult()
  End Using
End Sub

Private Function GetCustomers() As DataTable
  ' Create sample Customers table, in order
  ' to demonstrate the behavior of the DataTableReader.
  Dim table As New DataTable

  ' Create two columns, ID and Name.
  Dim idColumn As DataColumn = table.Columns.Add("ID", GetType(Integer))
  table.Columns.Add("Name", GetType(String))

  ' Set the ID column as the primary key column.
  table.PrimaryKey = New DataColumn() {idColumn}

  table.Rows.Add(New Object() {1, "Mary"})
  table.Rows.Add(New Object() {2, "Andy"})
  table.Rows.Add(New Object() {3, "Peter"})
  table.Rows.Add(New Object() {4, "Russ"})
  Return table
End Function

Private Function GetProducts() As DataTable
  ' Create sample Products table, in order
  ' to demonstrate the behavior of the DataTableReader.
  Dim table As New DataTable

  ' Create two columns, ID and Name.
  Dim idColumn As DataColumn = table.Columns.Add("ID", GetType(Integer))
  table.Columns.Add("Name", GetType(String))

  ' Set the ID column as the primary key column.
  table.PrimaryKey = New DataColumn() {idColumn}

  table.Rows.Add(New Object() {1, "Wireless Network Card"})
  table.Rows.Add(New Object() {2, "Hard Drive"})
  table.Rows.Add(New Object() {3, "Monitor"})
  table.Rows.Add(New Object() {4, "CPU"})
  Return table
End Function

Private Sub PrintColumns( _
   ByVal reader As DataTableReader)

  ' Loop through all the rows in the DataTableReader.
  Do While reader.Read()
    For i As Integer = 0 To reader.FieldCount - 1
      Console.Write(reader(i).ToString() & " ")
    Next
    Console.WriteLine()
  Loop
End Sub

설명

결과 집합의 순서가 반환 DataTableReader된 내에서 설정되도록 하기 위해 내의 가 DataTableDataSet 비어 있으면 반환 DataTableReader된 내의 빈 결과 집합으로 표시됩니다. 이 오버로드된 버전을 사용하면 인스턴스 목록을 DataTable 매개 변수로 제공할 수 있으므로 반환 DataTableReader된 내에 결과 집합이 표시되는 순서를 지정할 수 있습니다.

추가 정보

적용 대상

CreateDataReader()

Source:
DataSet.cs
Source:
DataSet.cs
Source:
DataSet.cs

테이블이 DataTableReader 컬렉션에 나타나는 순서대로 DataTable마다 결과 집합이 하나씩 있는 Tables를 반환합니다.

public:
 System::Data::DataTableReader ^ CreateDataReader();
public System.Data.DataTableReader CreateDataReader ();
member this.CreateDataReader : unit -> System.Data.DataTableReader
Public Function CreateDataReader () As DataTableReader

반환

결과 집합을 하나 이상 포함하는 DataTableReader이고, 소스 DataTable에 포함된 DataSet 인스턴스에 해당합니다.

예제

다음 예제에서는 세 DataTable 개의 인스턴스를 만들고 각각 을 DataSet에 추가합니다. 그런 다음, 메서드를 호출하는 프로시저에 채워진 DataSetCreateDataReader 전달하고 에 포함된 DataTableReader모든 결과 집합을 반복합니다. 이 예제에서는 콘솔 창에 결과를 표시합니다.

static void Main()
{
    DataSet dataSet = new DataSet();
    // Add some DataTables to the DataSet, including
    // an empty DataTable:
    dataSet.Tables.Add(GetCustomers());
    dataSet.Tables.Add(new DataTable());
    dataSet.Tables.Add(GetProducts());
    TestCreateDataReader(dataSet);

    Console.WriteLine("Press any key to continue.");
    Console.ReadKey();
}

private static void TestCreateDataReader(DataSet dataSet)
{
    // Given a DataSet, retrieve a DataTableReader
    // allowing access to all the DataSet's data:
    using (DataTableReader reader = dataSet.CreateDataReader())
    {
        do
        {
            if (!reader.HasRows)
            {
                Console.WriteLine("Empty DataTableReader");
            }
            else
            {
                PrintColumns(reader);
            }
            Console.WriteLine("========================");
        } while (reader.NextResult());
    }
}

private static DataTable GetCustomers()
{
    // Create sample Customers table, in order
    // to demonstrate the behavior of the DataTableReader.
    DataTable table = new DataTable();

    // Create two columns, ID and Name.
    DataColumn idColumn = table.Columns.Add("ID", typeof(int));
    table.Columns.Add("Name", typeof(string));

    // Set the ID column as the primary key column.
    table.PrimaryKey = new DataColumn[] { idColumn };

    table.Rows.Add(new object[] { 1, "Mary" });
    table.Rows.Add(new object[] { 2, "Andy" });
    table.Rows.Add(new object[] { 3, "Peter" });
    table.Rows.Add(new object[] { 4, "Russ" });
    return table;
}

private static DataTable GetProducts()
{
    // Create sample Products table, in order
    // to demonstrate the behavior of the DataTableReader.
    DataTable table = new DataTable();

    // Create two columns, ID and Name.
    DataColumn idColumn = table.Columns.Add("ID", typeof(int));
    table.Columns.Add("Name", typeof(string));

    // Set the ID column as the primary key column.
    table.PrimaryKey = new DataColumn[] { idColumn };

    table.Rows.Add(new object[] { 1, "Wireless Network Card" });
    table.Rows.Add(new object[] { 2, "Hard Drive" });
    table.Rows.Add(new object[] { 3, "Monitor" });
    table.Rows.Add(new object[] { 4, "CPU" });
    return table;
}

private static void PrintColumns(DataTableReader reader)
{
    // Loop through all the rows in the DataTableReader
    while (reader.Read())
    {
        for (int i = 0; i < reader.FieldCount; i++)
        {
            Console.Write(reader[i] + " ");
        }
        Console.WriteLine();
    }
}
Sub Main()
  Dim dataSet As New DataSet
  ' Add some DataTables to the DataSet, including
  ' an empty DataTable:
  dataSet.Tables.Add(GetCustomers())
  dataSet.Tables.Add(New DataTable())
  dataSet.Tables.Add(GetProducts())
  TestCreateDataReader(dataSet)

  Console.WriteLine("Press any key to continue.")
  Console.ReadKey()
End Sub

Private Sub TestCreateDataReader(ByVal dataSet As DataSet)
  ' Given a DataSet, retrieve a DataTableReader
  ' allowing access to all the DataSet's data:
  Using reader As DataTableReader = dataSet.CreateDataReader()
    Do
      If Not reader.HasRows Then
        Console.WriteLine("Empty DataTableReader")
      Else
        PrintColumns(reader)
      End If
      Console.WriteLine("========================")
    Loop While reader.NextResult()
  End Using
End Sub

Private Function GetCustomers() As DataTable
  ' Create sample Customers table, in order
  ' to demonstrate the behavior of the DataTableReader.
  Dim table As New DataTable

  ' Create two columns, ID and Name.
  Dim idColumn As DataColumn = table.Columns.Add("ID", GetType(Integer))
  table.Columns.Add("Name", GetType(String))

  ' Set the ID column as the primary key column.
  table.PrimaryKey = New DataColumn() {idColumn}

  table.Rows.Add(New Object() {1, "Mary"})
  table.Rows.Add(New Object() {2, "Andy"})
  table.Rows.Add(New Object() {3, "Peter"})
  table.Rows.Add(New Object() {4, "Russ"})
  Return table
End Function

Private Function GetProducts() As DataTable
  ' Create sample Products table, in order
  ' to demonstrate the behavior of the DataTableReader.
  Dim table As New DataTable

  ' Create two columns, ID and Name.
  Dim idColumn As DataColumn = table.Columns.Add("ID", GetType(Integer))
  table.Columns.Add("Name", GetType(String))

  ' Set the ID column as the primary key column.
  table.PrimaryKey = New DataColumn() {idColumn}

  table.Rows.Add(New Object() {1, "Wireless Network Card"})
  table.Rows.Add(New Object() {2, "Hard Drive"})
  table.Rows.Add(New Object() {3, "Monitor"})
  table.Rows.Add(New Object() {4, "CPU"})
  Return table
End Function

Private Sub PrintColumns( _
   ByVal reader As DataTableReader)

  ' Loop through all the rows in the DataTableReader.
  Do While reader.Read()
    For i As Integer = 0 To reader.FieldCount - 1
      Console.Write(reader(i).ToString() & " ")
    Next
    Console.WriteLine()
  Loop
End Sub

설명

반환 DataTableReader된 내에서 결과 집합의 순서를 확인하기 위해 내의 가 DataTableDataSet 비어 있으면 반환 DataTableReader된 내의 빈 결과 집합으로 표시됩니다.

추가 정보

적용 대상