Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute Parameterized SQL StoredProcedure via ODBC

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.

like image 407
dhh Avatar asked Aug 27 '10 09:08

dhh


People also ask

How do I execute a SQL stored procedure with parameters?

Expand the database that you want, expand Programmability, and then expand Stored Procedures. Right-click the user-defined stored procedure that you want and select Execute Stored Procedure. In the Execute Procedure dialog box, specify a value for each parameter and whether it should pass a null value.

Can we pass parameters to stored procedure in SQL?

You can also pass parameters to a stored procedure, so that the stored procedure can act based on the parameter value(s) that is passed.


1 Answers

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:

OdbcCommand ODBCCommand = new OdbcCommand("{call getDetailsFromEmail (?)}", ODBCConnection);
ODBCCommand.CommandType = CommandType.StoredProcedure;
ODBCCommand.Parameters.AddWithValue("@KundenEmail", KundenEmail);

Nevertheless - thanks for your help Thorsten.

like image 110
dhh Avatar answered Oct 13 '22 04:10

dhh