From within a C# WinForms app I must execute a parameterized Stored Procedure on a MS SQL Express Server. The Database Connection works, the Procedure works either, but I ge
How To Execute SQL Parameterized Stored Procedures by Using the ODBC .NET Provider and Visual C# .NET
While executing a parameterized stored procedure using the ODBC .NET Provider is little different from executing the same procedure using the SQL or the OLE DB Provider, there is one important difference: the stored procedure must be called using the ODBC CALL syntax rather than the name of the stored procedure.
Call Syntax Examples
Here is an example of the call syntax for an stored procedure that expects one input parameter:
{CALL CustOrderHist (?)}
Here is an example of the call syntax for a stored procedure that expects one input parameter and returns one output parameter and a return value. The first placeholder represents the return value:
{? = CALL Procedure1 (?, ?)
Sample Code
public static void SheduleDocuments(int siteid, int docid)
{
DataTable objDt = new DataTable();
OdbcConnection odbccon = new OdbcConnection();
try
{
odbccon.ConnectionString =
"Dsn=Dsn;" +
"Uid=databaseuserid;" +
"Pwd=databasepassword;";
odbccon.Open();
OdbcCommand cmd = new OdbcCommand("{call usp_GetEmpDetailsByIDanddepid(?,?)", odbccon);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@siteid", siteid);
cmd.Parameters.AddWithValue("@DocumentIDs", docid);
cmd.ExecuteNonQuery();
}
catch (OdbcException objEx)
{
string str = objEx.Message;
}
finally { odbccon.Close(); }
}