通过ODBC执行SQL参数化StoredProcedure的 [英] Execute Parameterized SQL StoredProcedure via ODBC
问题描述
从C#WinForms应用程序,我必须执行一个参数化存储过程MS SQL防爆preSS服务器上。数据库连接的工作原理,程序仍然可以正常工作,但我得到一个错误信息:
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:
错误[4200] [微软] [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屋!