【问题标题】:Insert not working插入不工作
【发布时间】:2012-09-08 10:44:49
【问题描述】:

我搜索了 evreywhere 并尝试了所有建议,但在运行以下代码时仍然没有运气。请注意,某些代码已被注释掉。那只是我在尝试不同的东西。

SqlConnection connection = new SqlConnection("Data Source=URB900-PC\SQLEXPRESS;Initial Catalog=usersSQL;Integrated Security=True");

        string password  = PasswordTextBox.Text;
        string email     = EmailTextBox.Text;
        string firstname = FirstNameTextBox.Text;
        string lastname  = SurnameTextBox.Text;

        //command.Parameters.AddWithValue("@UserName", username);
        //command.Parameters.AddWithValue("@Password", password);
        //command.Parameters.AddWithValue("@Email", email);
        //command.Parameters.AddWithValue("@FirstName", firstname);
        //command.Parameters.AddWithValue("@LastName", lastname);

        command.Parameters.Add("@UserName", SqlDbType.VarChar);
        command.Parameters["@UserName"].Value = username;

        command.Parameters.Add("@Password", SqlDbType.VarChar);
        command.Parameters["@Password"].Value = password;

        command.Parameters.Add("@Email", SqlDbType.VarChar);
        command.Parameters["@Email"].Value = email;

        command.Parameters.Add("@FirstName", SqlDbType.VarChar);
        command.Parameters["@FirstName"].Value = firstname;

        command.Parameters.Add("@LasttName", SqlDbType.VarChar);
        command.Parameters["@LasttName"].Value = lastname;

        SqlCommand command2 = new SqlCommand("INSERT INTO users (UserName, Password, UserEmail, FirstName, LastName)" +
        "values (@UserName, @Password, @Email, @FirstName, @LastName)", connection);

        connection.Open();
        command2.ExecuteNonQuery();
        //command2.ExecuteScalar();
        connection.Close();

当我运行它时,填写文本框并点击我得到的按钮......

必须声明标量变量“@UserName”。

任何帮助将不胜感激。谢谢。

【问题讨论】:

    标签: sql parameters sqlcommand scalar


    【解决方案1】:

    这是因为您将参数附加到command,然后执行不同的command2

    【讨论】:

    • 哎呀。感谢您的回复podiluska。我已经为此苦苦挣扎了几个小时。有时,答案就在眼前。
    猜你喜欢
    • 1970-01-01
    • 2011-11-14
    • 2015-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-06
    • 1970-01-01
    相关资源
    最近更新 更多