过程或函数需要未提供的参数“@OperatorName”

Procedure or function expects parameter '@OperatorName', which was not supplied

我收到一些意外错误, 我有两种方法,一种 是 INSERTUPDATE,另一种是 GETDATA

for INSERT UPDATE SP is -- >> spInsert_WorkEntry
for GETDATA SP is       -- >> spGetWorkEntryData

因为两个 SP 参数与图像中显示的相同。

我正在动态生成参数。如CreateParams所示。 INSERTUPDATE 和 GETDATA.

两种方法的创建参数相同

插入更新方法工作正常并按预期工作。

但是当我尝试执行其他方法时,即 GETDATA 它给我错误提示 @OperatorName 参数是预期的。 第一个参数是 @AutoID 该参数没有给出错误。 我已经检查了所有可能导致问题的可能情况,但现在得到了任何东西。

public int InsertOrUpdate(WorkEntry t, string callingPage, string operation)
{ // Start the Insert Update Method
    IDbDataParameter[] param = null;
    int returnValue = 0;
    try
    {
        param = CreateParams(t, callingPage, operation);
        param[0].Value = t.AutoID;
        param[1].Value = t.OperatorName;
        param[2].Value = t.Date;
        param[3].Value = t.StartDate;
        param[4].Value = t.EndDate;

        SqlConnection sqlConnection = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"].ToString());
        sqlConnection.Open();
        SqlCommand command = sqlConnection.CreateCommand();
        command.CommandType = System.Data.CommandType.StoredProcedure;
        command.CommandText = "spInsert_WorkEntry";
        command.Parameters.AddRange(param);

        returnValue = command.ExecuteNonQuery();
        sqlConnection.Close();
        return returnValue;

    }
    catch (Exception ex)
    {
        return returnValue;
        throw ex;
    }
    finally
    {
        param = null;
    }
}// End the Insert Update Method
// Get-----------------------------------------------------------------------------------------
public DataSet Get(WorkEntry t, string callingPage, string operation)
{ // Start the Insert Update Method
    IDbDataParameter[] param = null;
    DataSet returnValue = new DataSet();
    try
    {
        param = CreateParams(t, callingPage, operation);
        param[0].Value = t.AutoID;
        param[1].Value = t.OperatorName;
        param[2].Value = t.Date;
        param[3].Value = t.StartDate;
        param[4].Value = t.EndDate;

        SqlConnection sqlConnection = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"].ToString());
        sqlConnection.Open();
        SqlCommand command = sqlConnection.CreateCommand();
        SqlDataAdapter da = new SqlDataAdapter(command);
        command.CommandType = System.Data.CommandType.StoredProcedure;
        command.CommandText = "spGetWorkEntryData";
        command.Parameters.AddRange(param);
        da.Fill(ds);

        sqlConnection.Close();
        return returnValue;

    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        param = null;
        command.Parameters.Clear();

    }
}// End the Insert Update Method
// Get End -----------------------------------------------------------------------------------------
public IDbDataParameter[] CreateParams(WorkEntry workEntry, string CallingPage, string Operation)
{
    IDbDataParameter[] param = new IDbDataParameter[5];

    param[0] = new SqlParameter();
    param[0].DbType = DbType.Int32;
    param[0].Size = 50;
    param[0].ParameterName = "@AutoID";
    param[0].Direction = ParameterDirection.Input;

    param[1] = new SqlParameter();
    param[1].DbType = DbType.String;
    param[1].Size = 50;
    param[1].ParameterName = "@OperatorName";
    param[1].Direction = ParameterDirection.Input;

    param[2] = new SqlParameter();
    param[2].DbType = DbType.DateTime;
    param[2].Size = 50;
    param[2].ParameterName = "@Date";
    param[2].Direction = ParameterDirection.Input;

    param[3] = new SqlParameter();
    param[3].DbType = DbType.DateTime;
    param[3].Size = 50;
    param[3].ParameterName = "@StartDate";
    param[3].Direction = ParameterDirection.Input;

    param[4] = new SqlParameter();
    param[4].DbType = DbType.DateTime;
    param[4].Size = 50;
    param[4].ParameterName = "@EndDate";
    param[4].Direction = ParameterDirection.Input;
    return param;
}

下图为 SP

从 select 查询 SP spGetWorkEntryData 中删除所有参数,它不需要这个, 你的 sp 将是

create proc spGetWorkEntryData 
as 
begin
select operatorname, date, startdate ,enddate from workentry
end

尝试将 command.Parameters.AddRange(param); 移动到 SqlDataAdapter da = new SqlDataAdapter(command); 语句之前

 SqlConnection sqlConnection = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"].ToString());
        sqlConnection.Open();
        SqlCommand command = sqlConnection.CreateCommand();
   command.Parameters.AddRange(param);
        SqlDataAdapter da = new SqlDataAdapter(command);
        command.CommandType = System.Data.CommandType.StoredProcedure;
        command.CommandText = "spGetWorkEntryData";

        da.Fill(ds);

也适用于 select 语句,如果您不使用 Where 子句,则可能不需要传递参数。

更新:我测试了它,它在我的示例中运行良好

我对你的 Get 方法做了一些改动

public DataSet Get(string callingPage, string operation)
    { // Start the Insert Update Method

        // new instance of dataset and sqlcommand
        DataSet ds = new DataSet();
        SqlCommand command = new SqlCommand();

        IDbDataParameter[] param = null;
        DataSet returnValue = new DataSet();
        try
        {
            param = CreateParams(callingPage, operation);
            param[0].Value = 1;
            param[1].Value = "";
            param[2].Value = DateTime.Now;
            param[3].Value = DateTime.Now;
            param[4].Value = DateTime.Now;

            // using connectin string property insted ConfigurationManager.AppSettings
            SqlConnection sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString);
            sqlConnection.Open();
            command = sqlConnection.CreateCommand();
            SqlDataAdapter da = new SqlDataAdapter(command);
            command.CommandType = System.Data.CommandType.StoredProcedure;
            command.CommandText = "spGetWorkEntryData";
            command.Parameters.AddRange(param);
            da.Fill(ds);

            sqlConnection.Close();
            return returnValue;

        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            param = null;
            command.Parameters.Clear();

        }
    }// End

我的Demo存储过程

Create proc [dbo].[spGetWorkEntryData]
@autoId int,
@operatorName varchar(100),
@date datetime,
@startDate datetime,
@enddate datetime

as

begin
select * from dbo.tblBinaryUsers
end

结果: