How to perform a SELECT statement and store the returned rows in a DataSet object : DataSet : Database C# Source Code


Custom Search

C# Source Code » Database » DataSet »

 

How to perform a SELECT statement and store the returned rows in a DataSet object









    


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

class SelectIntoDataSet {
  public static void Main() {
    string connectionString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";

    SqlConnection mySqlConnection = new SqlConnection(connectionString);

    string selectString = "SELECT TOP 10 ID, FirstName, LastName FROM Employee ORDER BY ID";

    SqlCommand mySqlCommand = mySqlConnection.CreateCommand();

    mySqlCommand.CommandText = selectString;

    SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();

    mySqlDataAdapter.SelectCommand = mySqlCommand;
    DataSet myDataSet = new DataSet();

    mySqlConnection.Open();

    Console.WriteLine("Retrieving rows from the Employee table");
    mySqlDataAdapter.Fill(myDataSet, "Employee");

    mySqlConnection.Close();

    DataTable myDataTable = myDataSet.Tables["Employee"];

    foreach (DataRow myDataRow in myDataTable.Rows)
    {
      Console.WriteLine("ID = "+ myDataRow["ID"]);
      Console.WriteLine("FirstName = "+ myDataRow["FirstName"]);
      Console.WriteLine("LastName = "+ myDataRow["LastName"]);
    }
  }
}
           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Database
» DataSet