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 get an Error Message:
42000: Missing Parameter '@KundenEmail'
although I'm sure I added the parameter correctly. Maybe some of you could have a look - I don't know what to search for any more...
OdbcConnection ODBCConnection = new OdbcConnection();
try
{
ODBCConnection.ConnectionString = ODBCConnectionString;
ODBCConnection.Open();
}
catch (Exception DatabaseConnectionEx)
{
if (ODBCConnection != null)
ODBCConnection.Dispose();
// Error Message
return null;
}
OdbcParameter ODBCParameter = new OdbcParameter("@KundenEmail", OdbcType.NChar, 50);
ODBCParameter.Value = KundenEmail;
OdbcCommand ODBCCommand = new OdbcCommand("getDetailsFromEmail", ODBCConnection);
ODBCCommand.CommandType = CommandType.StoredProcedure;
ODBCCommand.Parameters.Add(ODBCParameter);
DataTable DataTable = new DataTable();
OdbcDataAdapter ODBCDatadapter = new OdbcDataAdapter(ODBCCommand);
ODBCDatadapter.Fill(DataTable);
ODBCDatadapter.Dispose();
ODBCConnection.Close();
ODBCConnection.Dispose();
This is the error message I get:
ERROR [4200][Microsoft][ODBC SQL Server]The Procedure or method 'getDetailsFromEmail' expects the '@KundenEmail'-parameter, which was not supplied.
Ah, I missed the connection string
private static String ODBCConnectionString = "Driver={SQL Server};Server=TESTSRV\\SQLEXPRESS;Database=TestDatabase;";
Any ideas? Thanks in advance.
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:
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:
Sample Code
Well - I now managed to solve the problem on my own, with some help from the MSDN-documentation.
The correct statement to execute a stored procedure via ODBC is as follows:
Nevertheless - thanks for your help Thorsten.
Anyway it's better your code would look like this:
But rather better to use SqlConnection/SqlCommand/SqlDataAdapter instead of ODBC types. Syntax will be still the same.
Don't use
ODBCConnection
to connect to a SQL Server. Use the "normal"SqlConnection
,SqlCommand
, etc. These are the ones made to work with SQL Server.EDIT: Also, you should use the
SqlConnectionStringBuilder
to assembly the connection string. This is far less error prone than putting the entire connection string into a configuration file or creating it by hand.