【问题标题】:The parameterized query '(@username nvarchar(4000))DELETE FROM Staff WHERE Username=@user' expects the parameter '@username', which was not supplied参数化查询 '(@username nvarchar(4000))DELETE FROM Staff WHERE Username=@user' 需要参数 '@username',但未提供
【发布时间】:2016-12-21 03:58:51
【问题描述】:

我的代码有问题:

public partial class DeleteStaff : System.Web.UI.Page
{
    string username = null;
    SqlConnection conn = null;
    SqlCommand cmd = null;
    string connectionString = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        // if loading the page for the first time
        if (!IsPostBack)
        {
            // Check that there is an id passed in with the QueryString
            if (Request.Params["username"] != null)
            {
                // retrieve the id
                username = Convert.ToString(Request.Params["username"]);
            }

            // retrieve connectionString from Web.Config file
            connectionString = ConfigurationManager.ConnectionStrings["LMSCS"].ConnectionString;

            // create connection with database specified in the connectionString
            conn = new SqlConnection(connectionString);

            // prepare sql statement
            string sql = "DELETE FROM Staff WHERE Username=@username";

            try
            {    
                // create SqlCommand object with sql statement and connection
                cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@username", username);

                // Open database connection
                conn.Open();

                // Execute SqlCommand
                int rows = cmd.ExecuteNonQuery();

                if (rows > 0)
                {
                    lblOutput.Text = "Record deleted successfully.";
                }
                else
                {
                    lblOutput.Text = "Record cannot be deleted.";
                }
            }
            catch (Exception ex)
            {
                lblOutput.Text = "Error Message: " + ex.Message;
            }
            finally
            {
                if (conn != null)
                    conn.Close();
            }
        }
    }
}

一旦我按下删除,但对于特定项目,我会收到此错误:

参数化查询“(@username nvarchar(4000))DELETE FROM Staff WHERE Username=@user”需要参数“@username”,但未提供该参数。

【问题讨论】:

  • 您确定此代码会产生该错误吗?查询文本包含一个名为 @username 的参数占位符,但错误显示带有占位符 @user 的不同查询

标签: c# sql sql-server visual-studio


【解决方案1】:

代替 AddWithValue 方法,使用 Add 方法并定义参数类型。看来 AddWithValue 是在猜测类型 (nvarchar(4000))。 AddWithValue 只接受一个对象,因此参数数据类型未正确映射。

例如from MSDN:

    command.Parameters.Add("@ID", SqlDbType.Int);
    command.Parameters["@ID"].Value = customerID;

【讨论】:

  • 什么是 sqldbtype.int?
  • 这是您传递给数据库的值的类型。 SQL Server 中的大多数数据类型都应该有类型。我建议您寻找字符串或 nvarchar 类型。
  • 那么如果我想传入字符串,会是什么?
【解决方案2】:

我认为当用户名 == null 时会发生错误。在这种情况下,不会添加参数(DBNULL.Value 和 null 不同)。 最好先检查输入参数

if (Request.Params["username"] == null) throw ...

【讨论】:

  • 很好的标注。我对 DBnull.Value 变得懒惰,因为我使用的 ORM 会自动转换值。
  • 我不明白
  • 您始终应该在所有程序中检查您的请求参数。在这种情况下,用户名不能为空,如果发生这种情况,您应该抛出异常
  • 所以你的意思是我必须放 ="";而不是 null?
  • 但我的数据库中有名字
【解决方案3】:

用户名是您应该传递到数据库中的字符串。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多