简体   繁体   English

存储过程代码错误

[英]Error With Stored Procedure Code

I have this C# code calling a stored procedure: 我有此C#代码调用存储过程:

using (SqlConnection myConnection = new SqlConnection(Utility.ConnString))
{
    myConnection.Open();

    using (SqlCommand myCommand = new SqlCommand(AddScanLocation_Proc, myConnection))
    {
        myCommand.Parameters.AddWithValue("@LocationQr", string.IsNullOrEmpty(LocationQr) ? "" : LocationQr);
        myCommand.Parameters.AddWithValue("@LocationNfc", string.IsNullOrEmpty(LocationNfc) ? "" : LocationNfc);
        myCommand.Parameters.AddWithValue("@LocationMachineName", LocationMachineName);
        myCommand.Parameters.AddWithValue("@LocationName", LocationName);
        myCommand.Parameters.AddWithValue("@IsLastStop", IsLastStop);
        myCommand.Parameters.AddWithValue("@Active", Active);

        IAsyncResult res = myCommand.BeginExecuteNonQuery();

        while (!res.IsCompleted) { }

        int response = myCommand.EndExecuteNonQuery(res);

        if (response > 0)
            toReturn = true;
   }
}

Every time I execute the code, I get an error telling me 每次执行代码时,都会告诉我一个错误

Procedure or function 'AddScanLocation' expects parameter '@LocationMachineName', which was not supplied. 过程或函数“ AddScanLocation”需要未提供的参数“ @LocationMachineName”。

I have debugged the code for hours and everything is typed correctly, and the variable is not null or empty, but it keeps throwing an error 我已经调试了几个小时的代码,并且所有内容都正确键入,并且变量不是null或为空,但它始终引发错误

any advice would be greatly appreciated! 任何建议将不胜感激!

Try check first if all your variables (including LocalMachineName ) has values before call block code for execute your stored procedure. 请先检查所有变量(包括LocalMachineName )是否具有值,然后再调用块代码执行存储过程。

Checking you code, you posted: 检查代码后,您发布了:

myCommand.Parameters.AddWithValue("@LocationQr", string.IsNullOrEmpty(LocationQr) ? "" : LocationQr);

Why not do the same with LocalMachineName too? 为什么不对LocalMachineName做同样的事情

PS: Try use test values if the variable LocalMachineName doesn't allow null values. PS:如果变量LocalMachineName不允许空值,请尝试使用测试值。

Plain and simply: you haven't told ADO.NET that you're executing a stored procedure ! 简单明了:您没有告诉ADO.NET您正在执行存储过程

Add this line: 添加此行:

using (SqlCommand myCommand = new SqlCommand(AddScanLocation_Proc, myConnection))
{
    myCommand.CommandType = CommandType.StoredProcedure;

    ...... 

and then everything should work just fine. 然后一切都会正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM