通过 ODBC 执行参数化 SQL StoredProcedure [英] Execute Parameterized SQL StoredProcedure via ODBC

查看:23
本文介绍了通过 ODBC 执行参数化 SQL StoredProcedure的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:缺少参数@KundenEmail"

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]过程或方法'getDetailsFromEmail' 需要 '@KundenEmail'-参数,它未提供.

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.

推荐答案

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

Well - I now managed to solve the problem on my own, with some help from the MSDN-documentation.

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

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

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

Nevertheless - thanks for your help Thorsten.

这篇关于通过 ODBC 执行参数化 SQL StoredProcedure的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆