Home > Code > C# > Execute stored procedure using ADO.NET (C#)

Execute stored procedure using ADO.NET (C#)

by Narain Siddharth   on Feb 28, 2011   Category: C#   |  Views: 19786    |  Points: 25   |  Silver 



Namespace used for execute the stored procedure,
using System;
using System.Data;
using System.Data.SqlClient;

See the below sample SelectEmployee() method to execute SP using C#.NET
public void SelectEmployee()
{
SqlConnection con = null;
SqlDataReader rd = null;
try
{
// Create and Open the SQL server connection object
con = new SqlConnection("Database Connection string");
con.Open();
// Create a command object and specify the Stored Procedure name and connection as well
SqlCommand cmd = new SqlCommand("ProcEmployeeDetails", con);
// Set the command object
cmd.CommandType = CommandType.StoredProcedure;
// Add parameter and value
cmd.Parameters.Add(new SqlParameter("@EmployeeID", 550));
// Execute the command
rd = cmd.ExecuteReader();
while (rd.Read())
{
Response.Write(rd["Name"].ToString(),rd["Age"].ToString(), rd["Designation"].ToString());
}
}
catch (Exception e)
{
Response.Write(e);
}
finally
{
if (con != null)
{
con.Close();
}
if (rd != null)
{
rd.Close();
}
}
}





Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts