通过 ODBC 执行参数化 SQL 存储过程
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好吧 - 在 MSDN 文档的一些帮助下,我现在设法自己解决了这个问题。
通过 ODBC 执行存储过程的正确语句如下:
尽管如此 - 感谢您的帮助 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:
Nevertheless - thanks for your help Thorsten.
如何使用 ODBC .NET 提供程序和 Visual C# .NET 执行 SQL 参数化存储过程
使用 ODBC .NET 提供程序执行参数化存储过程与使用 SQL 或 Visual C# 执行相同的过程几乎没有什么不同。与 OLE DB Provider 相比,有一个重要的区别:必须使用 ODBC CALL 语法而不是存储过程的名称来调用存储过程。
调用语法示例
以下是需要一个输入参数的存储过程的调用语法示例:
下面是存储过程的调用语法示例,该存储过程需要一个输入参数并返回一个输出参数和一个返回值。第一个占位符代表返回值:
示例代码
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
无论如何,您的代码最好如下所示:
但最好使用 SqlConnection/SqlCommand/SqlDataAdapter 而不是 ODBC 类型。语法仍然相同。
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.
不要使用
ODBCConnection
连接到 SQL Server。使用“普通”SqlConnection
、SqlCommand
等。这些是为与 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.