Saturday, August 11, 2007

ADO.net > Executing a Stored Procedure

STEPS IN EXECUTING A STORED PROCEDURE

• To execute a stored procedure in .net, Command object’s CommandType is set to CommandType.StoredProcedure

1. Objects required: SqlConnection, SqlCommand, SqlDataReader

2. Initialize the SqlConnection object passing it the Connection String
SqlConnection sqlConnection1 = new SqlConnection(“connectionString”);

3. Initialize the SqlCommand and SqlReader objects
SqlCommand cmd = new SqlCommand;
SqlDataReader reader;


4. Initialize the CommandText, CommandType and Connection properties of SqlCommand object
cmd.CommandText = “StoredProcedureName”;
cmd.Connection = sqlConnection1;
cmd.CommandType = CommandType.StoredProcedure;


5. Open the connection
sqlConnection1.Open();

6. Initialize the SqlDataReader object by calling ExecuteReader method on SqlCommand object
reader = cmd.ExecuteReader();

7. Close the connection
sqlConnection1.Close();

No comments:

Post a Comment