【问题标题】:"The parameterized query expects a parameter which was not supplied" error“参数化查询需要一个未提供的参数”错误
【发布时间】:2019-12-10 07:04:33
【问题描述】:

我正在编写代码来实现应用程序。错误显示

参数化查询'@original_controllerIP nvchar(19), @IsNull_ControllerName int' 需要未提供的参数@IsNull_ControllerName。

我尝试为控制器添加Original_ControllerIPName 和所有其他参数,但没有奏效。

public virtual int Delete(string Original_ControllerIP) {
    if ((Original_ControllerIP == null)) {
        throw new global::System.ArgumentNullException("Original_ControllerIP");
    }
    else {
        this.Adapter.DeleteCommand.Parameters[0].Value = ((string)(Original_ControllerIP));
    }

    global::System.Data.ConnectionState previousConnectionState = this.Adapter.DeleteCommand.Connection.State;

    if (((this.Adapter.DeleteCommand.Connection.State & global::System.Data.ConnectionState.Open) 
                != global::System.Data.ConnectionState.Open)) {
        this.Adapter.DeleteCommand.Connection.Open();
    }
    try {
        int returnValue = this.Adapter.DeleteCommand.ExecuteNonQuery();
        return returnValue;
    }
    finally {
        if ((previousConnectionState == global::System.Data.ConnectionState.Closed)) {
            this.Adapter.DeleteCommand.Connection.Close();
        }
    }
}

参数如下:

this._adapter.DeleteCommand = new global::System.Data.SqlClient.SqlCommand();
        this._adapter.DeleteCommand.Connection = this.Connection;
        this._adapter.DeleteCommand.CommandText = @"DELETE FROM [ControllersData] WHERE (([ControllerIP] = @Original_ControllerIP) AND ((@IsNull_ControllerName = 1 AND [ControllerName] IS NULL) OR ([ControllerName] = @Original_ControllerName)) AND ((@IsNull_ControllerMac = 1 AND [ControllerMac] IS NULL) OR ([ControllerMac] = @Original_ControllerMac)) AND ((@IsNull_ControllerStatus = 1 AND [ControllerStatus] IS NULL) OR ([ControllerStatus] = @Original_ControllerStatus)))";
        this._adapter.DeleteCommand.CommandType = global::System.Data.CommandType.Text;
        this._adapter.DeleteCommand.Parameters.Add(new global::System.Data.SqlClient.SqlParameter("@Original_ControllerIP", global::System.Data.SqlDbType.NVarChar, 0, global::System.Data.ParameterDirection.Input, 0, 0, "ControllerIP", global::System.Data.DataRowVersion.Original, false, null, "", "", ""));
        this._adapter.DeleteCommand.Parameters.Add(new global::System.Data.SqlClient.SqlParameter("@IsNull_ControllerName", global::System.Data.SqlDbType.Int, 0, global::System.Data.ParameterDirection.Input, 0, 0, "ControllerName", global::System.Data.DataRowVersion.Original, true, null, "", "", ""));
        this._adapter.DeleteCommand.Parameters.Add(new global::System.Data.SqlClient.SqlParameter("@Original_ControllerName", global::System.Data.SqlDbType.NVarChar, 0, global::System.Data.ParameterDirection.Input, 0, 0, "ControllerName", global::System.Data.DataRowVersion.Original, false, null, "", "", ""));
        this._adapter.DeleteCommand.Parameters.Add(new global::System.Data.SqlClient.SqlParameter("@IsNull_ControllerMac", global::System.Data.SqlDbType.Int, 0, global::System.Data.ParameterDirection.Input, 0, 0, "ControllerMac", global::System.Data.DataRowVersion.Original, true, null, "", "", ""));
        this._adapter.DeleteCommand.Parameters.Add(new global::System.Data.SqlClient.SqlParameter("@Original_ControllerMac", global::System.Data.SqlDbType.NVarChar, 0, global::System.Data.ParameterDirection.Input, 0, 0, "ControllerMac", global::System.Data.DataRowVersion.Original, false, null, "", "", ""));
        this._adapter.DeleteCommand.Parameters.Add(new global::System.Data.SqlClient.SqlParameter("@IsNull_ControllerStatus", global::System.Data.SqlDbType.Int, 0, global::System.Data.ParameterDirection.Input, 0, 0, "ControllerStatus", global::System.Data.DataRowVersion.Original, true, null, "", "", ""));
        this._adapter.DeleteCommand.Parameters.Add(new global::System.Data.SqlClient.SqlParameter("@Original_ControllerStatus", global::System.Data.SqlDbType.NVarChar, 0, global::System.Data.ParameterDirection.Input, 0, 0, "ControllerStatus", global::System.Data.DataRowVersion.Original, false, null, "", "", ""));

【问题讨论】:

  • 你能显示 DeleteCommand.Parameters 的设置位置吗?
  • 会不会是您的查询需要 2 个参数,但您只设置了一个?我之所以问,是因为我只看到你的 DeleteCommand 参数的一个分配
  • 这看起来像是生成的代码(global:: 前缀和多余的括号是指示符)。 @IsNull_ControllerName 参数可能是稍后添加的,需要重新生成此代码。
  • @sr28 刚刚在问题中添加了参数设置
  • 正如 Jonathan 和 madreflection 所提到的,需要 2 个参数; Original_ControllerIP 和 IsNull_ControllerName。两者都需要提供。

标签: c# sql dataadapter


【解决方案1】:

您的查询需要 2 个参数:original_controllerIPIsNull_ControllerName,但您只提供了一个:

this.Adapter.DeleteCommand.Parameters[0].Value = ((string)(Original_ControllerIP));

您应该提供这两个参数:

this.Adapter.DeleteCommand.Parameters[0].Value = ((string)(Original_ControllerIP));
this.Adapter.DeleteCommand.Parameters[1].Value = VALUE IN HERE;

如果您的查询应该只接受一个参数,那么您可能需要刷新/更新您的数据适配器

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-20
    • 2016-04-16
    相关资源
    最近更新 更多