简体   繁体   English

通过ODBC执行参数化SQL StoredProcedure

[英]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. 在C#WinForms应用程序中,我必须在MS SQL Express服务器上执行参数化存储过程。 The Database Connection works, the Procedure works either, but I get an Error Message: 数据库连接工作,过程工作,但我收到一条错误消息:

42000: Missing Parameter '@KundenEmail' 42000:缺少参数'@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. 错误[4200] [Microsoft] [ODBC SQL Server]过程或方法'getDetailsFromEmail'需要'@ KundenEmail'参数,该参数未提供。

Ah, I missed the connection string 啊,我错过了连接字符串

private static String ODBCConnectionString = "Driver={SQL Server};Server=TESTSRV\\SQLEXPRESS;Database=TestDatabase;";

Any ideas? 有任何想法吗? Thanks in advance. 提前致谢。

Well - I now managed to solve the problem on my own, with some help from the MSDN-documentation. 好吧 - 我现在设法自己解决这个问题,在MSDN文档的帮助下。

The correct statement to execute a stored procedure via ODBC is as follows: 通过ODBC执行存储过程的正确语句如下:

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

Nevertheless - thanks for your help Thorsten. 不过 - 谢谢你的帮助Thorsten。

How To Execute SQL Parameterized Stored Procedures by Using the ODBC .NET Provider and Visual C# .NET 如何使用ODBC .NET提供程序和Visual C#.NET执行SQL参数化存储过程

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. 使用ODBC .NET提供程序执行参数化存储过程与使用SQL或OLE DB提供程序执行相同过程几乎没有区别,但有一个重要区别:必须使用ODBC CALL语法而不是名称来调用存储过程存储过程。

Call Syntax Examples 调用语法示例

Here is an example of the call syntax for an stored procedure that expects one input parameter: 以下是需要一个输入参数的存储过程的调用语法示例:

{CALL CustOrderHist (?)} {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 (?, ?) = 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(); }
    }

Anyway it's better your code would look like this: 无论如何,你的代码看起来会更好:

using (OdbcConnection connection = new OdbcConnection(connectionString) )
using (OdbcCommand command = connection.CreateCommand())
{
    command.CommandText = commandText;
    command.CommandType = CommandType.StoredProcedure;

    command.Parameters.Add("@KundenEmail", OdbcType.NChar, 50).Value = KundenEmail

    DataTable dataTable = new DataTable();

    connection.Open();

    using (OdbcDataAdapter adapter = new OdbcDataAdapter(command))
    {
        adapter.Fill(dataTable);
    }
}

But rather better to use SqlConnection /SqlCommand/SqlDataAdapter instead of ODBC types. 但更好的是使用SqlConnection / SqlCommand / SqlDataAdapter而不是ODBC类型。 Syntax will be still the same. 语法仍然是一样的。

 private void button1_Click(object sender, EventArgs e)
 {
    OdbcCommand cmd = new OdbcCommand(" call prc_st(?)",con);
    cmd.Parameters.Add(new OdbcParameter("@id", "1"));
    cmd.CommandType = CommandType.StoredProcedure;
    OdbcDataAdapter adp = new OdbcDataAdapter(cmd);
    DataSet ds = new DataSet();
    adp.Fill(ds);

 }

Don't use ODBCConnection to connect to a SQL Server. 不要使用ODBCConnection连接到SQL Server。 Use the "normal" SqlConnection , SqlCommand , etc. These are the ones made to work with SQL Server. 使用“普通” SqlConnectionSqlCommand等。这些是与SQL Server一起使用的。

EDIT: Also, you should use the SqlConnectionStringBuilder to assembly the connection string. 编辑:此外,您应该使用SqlConnectionStringBuilder来组装连接字符串。 This is far less error prone than putting the entire connection string into a configuration file or creating it by hand. 这比将整个连接字符串放入配置文件或手动创建它更不容易出错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM