How do I call a stored procedure from my application using ADO.Net?
Using stored procedures with ADO.Net in C# is extremely simple, especially when we have developed the application with SQL commands. All we need is:
- Create a command object (SqlCommand, etc) and specify the stored procedure name
- Set the CommandType property of the command object to the CommandType.StoredProcedure enumeration value. This tells the runtime that the command used here is a stored procedure.
That’s it! The sample code to use with data adapter is:
C# Version
// Preparing Insert SQL Command
SqlCommand insertCommand = new SqlCommand("InsertProc", conn);
insertCommand.CommandType = CommandType.StoredProcedure;
dataAdapter.InsertCommand = insertCommand;
insertCommand.UpdatedRowSource = UpdateRowSource.None;
...
VB.Net Version
' Preparing Insert SQL Command
Dim insertCommand = New SqlCommand("InsertProc", conn)
insertCommand.CommandType = CommandType.StoredProcedure
dataAdapter.InsertCommand = insertCommand
insertCommand.UpdatedRowSource = UpdateRowSource.None
Back