Executing Stored Procedure with Data Reader
Data Reader use to read data from Sql server stored procedure

Introduction
This example shows how to execute a stored procedure. We will be using the CustOrdersDetail stored procedure that comes with the SQL Server Northwind database. The stored procedure is shown below; it takes an OrderID and returns the details for that order. The example shows the following core features:
Background
|
|
|
|
|
Stored Procedure Code
CREATE PROCEDURE CustOrdersDetail @OrderID int
AS
SELECT ProductName,
UnitPrice=ROUND(Od.UnitPrice, 2),
Quantity,
Discount=CONVERT(int, Discount * 100),
ExtendedPrice=ROUND(CONVERT(money,
Quantity * (1 - Discount) * Od.UnitPrice), 2)
FROM Products P, [Order Details] Od
WHERE Od.ProductID = P.ProductID and Od.OrderID = @OrderID
Code Behind File
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace Akadia
{
public class SqlBrowser : System.Windows.Forms.Form
{
private System.Windows.Forms.ListBox orderIDsList;
private System.Windows.Forms.ListView orderDetailsList;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.ComponentModel.Container components = null;
private System.Data.SqlClient.SqlConnection con;
private System.Windows.Forms.Button btnStart;
private bool _columnsSet = false;
public SqlBrowser()
{
// Create Controls
InitializeComponent();
// Setup Connection
string strCon = "server=xeon;uid=sa;" +
"pwd=manager; database=northwind";
con = new SqlConnection(strCon);
}
.....
.....
// The main entry point for the application.
static void Main()
{
Application.Run(new SqlBrowser());
}
// Fill orderIDsList with OrderID's
private void btnStart_Click(object sender, System.EventArgs e)
{
int iOrderID;
// Create a command to select the Order IDs from the ORDERS table
SqlCommand command = new SqlCommand
("SELECT OrderID from ORDERS", con);
// Open the connection
con.Open();
// Get the data reader
SqlDataReader reader = command.ExecuteReader();
// Process each result
while (reader.Read())
{
// Add each order ID in the result to the list
// view containing the orders IDs. We have only
// selected a single column in this code so we
// can be pretty save in using reader.GetInit32(0)
// there are no more columns in the data reader.
iOrderID = reader.GetOrdinal("OrderID");
orderIDsList.Items.Add (reader.GetInt32(iOrderID));
}
// Close the reader and the connection
reader.Close();
this.con.Close();
}
// Populate the list view with the order details
//
// Used Stored Procedure:
// ----------------------
// CREATE PROCEDURE CustOrdersDetail @OrderID int
// AS
// SELECT ProductName,
// UnitPrice=ROUND(Od.UnitPrice, 2),
// Quantity,
// Discount=CONVERT(int, Discount * 100),
// ExtendedPrice=ROUND(CONVERT(money, Quantity *
// (1 - Discount) * Od.UnitPrice), 2)
// FROM Products P, [Order Details] Od
// WHERE Od.ProductID = P.ProductID and Od.OrderID = @OrderID
//
private void orderIDsList_SelectedIndexChanged
(object sender, System.EventArgs e)
{
// Get the selected item and convert it to the correct order ID
int orderID = Convert.ToInt32(orderIDsList.SelectedItem);
// Create a command to execute a stored procedure
SqlCommand storedProcCommand =
new SqlCommand ("CustOrdersDetail", con);
// Let the command know it will be executed a stored procedure
storedProcCommand.CommandType = CommandType.StoredProcedure;
// Add a parameter that's passed to the stored proc,
// this is the order ID we selected
storedProcCommand.Parameters.Add("@OrderID",orderID);
// Open the connection
con.Open ();
// Create an ArrayList to hold the results
ArrayList rowList = new ArrayList();
// Get the reader
SqlDataReader reader = storedProcCommand.ExecuteReader();
// Process each result in the result set
while (reader.Read())
{
// Create an array big enough to hold the column values
object[] values = new object[reader.FieldCount];
// Get the column values into the array
reader.GetValues(values);
// Add the array to the ArrayList
rowList.Add(values);
}
// Have the columns already being added to the list view?
if (_columnsSet == false)
{
// No, so get the schema for this result set
DataTable schema = reader.GetSchemaTable();
// And set the list view to reflect the
// contents of the schema
SetColumnHeaders(schema);
}
// Close the reader and the connection
reader.Close ();
con.Close ();
// Now populate the list view with the order details
PopulateOrderDetails(rowList);
}
// Populate the list view with the order details
private void PopulateOrderDetails (ArrayList rowList)
{
// Clear any exsisting items from the orders view
orderDetailsList.Items.Clear();
// Now process each array in the arraylist
foreach (object[] row in rowList)
{
// Create a string array large enough to hold all
// the column values in this array
string[] orderDetails = new string[row.Length];
// Create a column index into the array
int columnIndex = 0;
// Now process each column value
foreach (object column in row)
{
// Convert the value to a string and stick
// it in the string array
orderDetails[columnIndex++] = Convert.ToString (column);
}
// Now use the string array to create a new item
// to go in the list view
ListViewItem newItem = new ListViewItem (orderDetails);
// Finally add the new item to the view
orderDetailsList.Items.Add (newItem);
}
}
// Set the list view to reflect the contents of the schema
private void SetColumnHeaders (DataTable schema)
{
// Process each row in the schema table
foreach (DataRow row in schema.Rows)
{
// For each column add a new column to the list view
orderDetailsList.Columns.Add((string)row["ColumnName"],
50, HorizontalAlignment.Left);
}
// Set this flag to stop the method being called again
_columnsSet = true;
}
}
}
Enjoy!!!!!!!!!!!!!