Populate a DataSet object using a store procedure : Store Procedure « Database ADO.net « C# / C Sharp






Populate a DataSet object using a store procedure


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

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

    SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
    mySqlCommand.CommandText = "EXECUTE CustOrderHist @CustomerID";
    mySqlCommand.Parameters.Add("@CustomerID", SqlDbType.NVarChar, 5).Value = "ALFKI";

    SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
    mySqlDataAdapter.SelectCommand = mySqlCommand;
    DataSet myDataSet = new DataSet();
    mySqlConnection.Open();
    Console.WriteLine("Retrieving rows from the CustOrderHist() Procedure");
    int numberOfRows = mySqlDataAdapter.Fill(myDataSet, "CustOrderHist");
    Console.WriteLine("numberOfRows = " + numberOfRows);
    mySqlConnection.Close();

    DataTable myDataTable = myDataSet.Tables["CustOrderHist"];
    foreach (DataRow myDataRow in myDataTable.Rows)
    {
      Console.WriteLine("ProductName = " + myDataRow["ProductName"]);
      Console.WriteLine("Total = " + myDataRow["Total"]);
    }
  }
}
           
       








Related examples in the same category

1.Add parameters to SqlCommand to call stored procedure
2.Call the SQL Server AddProduct() stored procedure with SqlCommand
3.Illustrates simple stored procedures with unnamed parameters in the queryIllustrates simple stored procedures with unnamed parameters in the query
4.Call the SQL Server AddProduct() store procedure
5.Get Return from SQL Server store procedure
6.Call Simple Store Procedure
7.illustrates how to call a SQL Server stored procedure
8.Call a store procedure