通过 ODBC 执行参数化 SQL 存储过程

发布于 2024-09-16 09:10:59 字数 1395 浏览 6 评论 0原文

在 C# WinForms 应用程序中,我必须在 MS SQL Express Server 上执行参数化存储过程。数据库连接有效,过程也有效,但我收到一条错误消息:

42000:缺少参数“@KundenEmail”

尽管我确定我正确添加了参数。也许你们中的一些人可以看看 - 我不知道要再搜索什么...

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();

这是我收到的错误消息:

错误 [4200][Microsoft][ODBC SQL Server]过程或方法 “getDetailsFromEmail”需要“@KundenEmail”参数,该参数 未提供。

啊,我错过了连接字符串

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

有什么想法吗?提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夕色琉璃 2024-09-23 09:11:00

好吧 - 在 MSDN 文档的一些帮助下,我现在设法自己解决了这个问题。

通过 ODBC 执行存储过程的正确语句如下:

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

尽管如此 - 感谢您的帮助 Thorsten。

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.

热鲨 2024-09-23 09:11:00

如何使用 ODBC .NET 提供程序和 Visual C# .NET 执行 SQL 参数化存储过程

使用 ODBC .NET 提供程序执行参数化存储过程与使用 SQL 或 Visual C# 执行相同的过程几乎没有什么不同。与 OLE DB Provider 相比,有一个重要的区别:必须使用 ODBC CALL 语法而不是存储过程的名称来调用存储过程。

调用语法示例

以下是需要一个输入参数的存储过程的调用语法示例:

{调用 CustOrderHist (?)}

下面是存储过程的调用语法示例,该存储过程需要一个输入参数并返回一个输出参数和一个返回值。第一个占位符代表返回值:

{? = 调用过程1 (?, ?)

示例代码

    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(); }
    }

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(); }
    }
伪心 2024-09-23 09:11:00

无论如何,您的代码最好如下所示:

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);
    }
}

但最好使用 SqlConnection/SqlCommand/SqlDataAdapter 而不是 ODBC 类型。语法仍然相同。

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. Syntax will be still the same.

风追烟花雨 2024-09-23 09:11:00

不要使用ODBCConnection 连接到 SQL Server。使用“普通”SqlConnectionSqlCommand 等。这些是为与 SQL Server 一起使用而设计的。

编辑:此外,您应该使用 SqlConnectionStringBuilder 来组装连接字符串。这比将整个连接字符串放入配置文件或手动创建它更不容易出错。

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.

送君千里 2024-09-23 09:11:00
 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);

 }
 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);

 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文