sql简单更新语句不起作用

时间:2015-08-21 12:47:49

标签: c# sql sql-update

更新根本不起作用,行保持不变。我没有错误。有人知道它不起作用的原因吗? 我首先想知道问题的原因,但如果您有任何想法,建议如何改进我的代码,我们将受到欢迎。

 private void button2_Click(object sender, EventArgs e)
    {
        using (SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=E:\C #\InsertDeleteUpdate-Login\InsertDeleteUpdate-Login\Database1.mdf;Integrated Security=True"))
        using (SqlCommand cmd = new SqlCommand("UPDATE info SET Password=@Password WHERE Id=@Id AND Password=@Password1",conn))
        {
            conn.Open();
            cmd.Parameters.AddWithValue("@Password", textBox4.Text);
            cmd.Parameters.AddWithValue("@Id", textBox3.Text);
            cmd.Parameters.AddWithValue("@Password1", textBox2.Text);
        }
    }

3 个答案:

答案 0 :(得分:3)

您需要执行查询

cmd.Parameters.AddWithValue("@Password", textBox4.Text);
cmd.Parameters.AddWithValue("@Id", textBox3.Text);
cmd.Parameters.AddWithValue("@Password1", textBox2.Text);

cmd.ExecuteNonQuery(); // this is what was missing

答案 1 :(得分:3)

您没有执行查询。你需要使用:

cmd.ExcecuteNonQuery();
添加参数后

答案 2 :(得分:2)

private void button2_Click(object sender, EventArgs e)
{
    using (SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=E:\C #\InsertDeleteUpdate-Login\InsertDeleteUpdate-Login\Database1.mdf;Integrated Security=True"))
    using (SqlCommand cmd = new SqlCommand("UPDATE info SET Password=@Password WHERE Id=@Id AND Password=@Password1",conn))
    {
        conn.Open();
        cmd.Parameters.AddWithValue("@Password", textBox4.Text);
        cmd.Parameters.AddWithValue("@Id", textBox3.Text);
        cmd.Parameters.AddWithValue("@Password1", textBox2.Text);
        cmd.ExecuteNonQuery();

    }
}