DataSet.Tables : DataSet « System.Data « C# / C Sharp by API






DataSet.Tables

  

using System;
using System.Data;
using System.Data.SqlClient;

class MultipleDataTables
{
  public static void Main()
  {
    SqlConnection mySqlConnection =new SqlConnection("server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;");

    SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
    mySqlCommand.CommandText =
      "SELECT TOP 2 ID, FirstName, LastName " +
      "FROM Employee " +
      "ORDER BY ID;" +
      "SELECT ID, LastName " +
      "FROM MyEmployee " +
      "WHERE ID = 9;";

    SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
    mySqlDataAdapter.SelectCommand = mySqlCommand;
    DataSet myDataSet = new DataSet();
    mySqlConnection.Open();
    int numberOfRows = mySqlDataAdapter.Fill(myDataSet);
    Console.WriteLine("numberOfRows = " + numberOfRows);
    mySqlConnection.Close();

    myDataSet.Tables["Table"].TableName = "Employee";
    myDataSet.Tables["Table1"].TableName = "MyEmployee";

    foreach (DataTable myDataTable in myDataSet.Tables) {
      Console.WriteLine("\nReading from the " +
        myDataTable.TableName + "DataTable");
      foreach (DataRow myDataRow in myDataTable.Rows)
      {
        foreach (DataColumn myDataColumn in myDataTable.Columns)
        {
          Console.WriteLine(myDataColumn + "= " +
            myDataRow[myDataColumn]);
        }
      }
    }
  }
}
      

   
    
  








Related examples in the same category

1.new DataSet(String data)
2.DataSet.Delete
3.DataSet.Merge
4.DataSet.MergeFailed
5.DataSet.Read
6.DataSet.ReadXml
7.DataSet.Relations.Add
8.DataSet.WriteXml