try
{
using( SqlConnection connection = new SqlConnection("some connection string" ) )
{
using( SqlCommand command = new SqlCommand( "stored procedure name", connection ) )
{
command.CommandType = CommandType.StoredProcedure;
// this line needs to be added for every parameter the SP expects
command.Parameters.AddWithValue( "parametere value", paramValue );
connection.Open();
// based on the what the SP does, call the execute non query, execute scalar, or execute reader method
command.ExecuteNonQuery();
}
}
}
catch( Exception ex )
{
Debug.WriteLine( ex.ToString() );
}
I'm a nerd, yep I admit it freely. I read slashdot and my job title is Software Engineer. These are my rambling on .NET, C++, Programming, and Technology overall.
Sunday, July 29, 2012
C# execute a stored procedure using SQL Server
Below we have some code that opens a connection to a database using a connection string then executes a stored procedure. This obviously needs to be placed in some method or class. You will need to add your own connection string and parameter values.
Labels:
C#,
databases,
sql server
Subscribe to:
Post Comments (Atom)
1 comment:
command.Parameters.AddWithValue( "@parametereName", paramValue );
its correct way....
Post a Comment