【问题标题】:Execute Parameterized SQL StoredProcedure via ODBC通过 ODBC 执行参数化 SQL 存储过程
【发布时间】:2010-08-27 09:57:06
【问题描述】:

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

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

这是我收到的错误消息:

ERROR [4200][Microsoft][ODBC SQL Server]过程或方法 'getDetailsFromEmail' 需要 '@KundenEmail' 参数,它 未提供。

啊,我错过了连接字符串

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

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

【问题讨论】:

    标签: c# winforms stored-procedures ado.net odbc


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 所以不要忘记接受其中一个答案——你的或其他人的
    • 感谢您的提示 - 我已经尝试接受我自己的答案,但由于我只是这个网站的新手,我只能在两天内接受我自己的答案 - “你可以接受两天后你自己的答案”。尽管如此 - 感谢您的所有帮助和想法。
    • @dhh 刚遇到同样的问题,但是有一个可以为空的参数,所以我没有收到错误代码。你的答案刚刚挽救了一天。非常感谢。
    【解决方案2】:

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

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

    调用语法示例

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

    {CALL CustOrderHist (?)}

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

    {? = CALL Procedure1 (?, ?)

    示例代码

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

    【讨论】:

      【解决方案3】:

      不管怎样,你的代码最好是这样的:

      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 类型。语法还是一样的。

      【讨论】:

        【解决方案4】:

        不要使用ODBCConnection 连接到 SQL Server。使用“正常的”SqlConnectionSqlCommand 等。这些是用于 SQL Server 的。

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

        【讨论】:

        • 您好,Thorsten,感谢您的回复。我添加了 OdbcConnectionStringBuilder - 我无法使用直接 SQL 连接,因为我被告知使用 ODBC 连接。出于测试目的,我之前使用过 Sql-things(因为我已经使用过它),然后将其切换到 ODBC。不知怎的,我无法让它工作......
        • 试试ODBCCommand.Parameters.AddWithValue(...)怎么样? OdbcCommands 存在吗?如果直接连接到 SQL Server,则使用 ODBC 连接的要求没有意义。只是出于好奇:当您使用各自的 Sql... 类时它是否有效?
        • 是的,当我使用Sql...-Classes 时它确实有效。嗯,要使用 ODBC 连接,因为不确定使用哪个数据库系统。
        【解决方案5】:
         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);
        
         }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-04-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-01-31
          • 2016-07-17
          • 1970-01-01
          相关资源
          最近更新 更多