2015-08-21 90 views
-3

更新根本不起作用,行保持不变。我得到没有错误。有谁知道为什么它不起作用? 我想首先知道问题的原因,但如果您有任何想法,关于如何改进我的代码的建议,我们会受到欢迎。sql简单的更新语句不起作用

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 [email protected] WHERE [email protected] AND [email protected]",conn)) 
     { 
      conn.Open(); 
      cmd.Parameters.AddWithValue("@Password", textBox4.Text); 
      cmd.Parameters.AddWithValue("@Id", textBox3.Text); 
      cmd.Parameters.AddWithValue("@Password1", textBox2.Text); 
     } 
    } 
+5

你需要执行查询 – user1666620

+3

使用:'cmd.ExecuteNonQuery()' – Stefan

+0

你缺少'cmd.ExecuteNonQuery()' – TFrost

回答

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 
+0

thx很多,你知道,在这个应用程序中,我有另一个查询,我选择了一些东西,该声明没有执行任何查询,这就是为什么我相信它在这里也不需要。 –

+0

当UPDATE和DELETE需要显式执行语句时,Select被隐式执行。 –

3

你是不是在执行查询。您需要使用:

cmd.ExcecuteNonQuery(); 

添加参数后。

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 [email protected] WHERE [email protected] AND [email protected]",conn)) 
    { 
     conn.Open(); 
     cmd.Parameters.AddWithValue("@Password", textBox4.Text); 
     cmd.Parameters.AddWithValue("@Id", textBox3.Text); 
     cmd.Parameters.AddWithValue("@Password1", textBox2.Text); 
     cmd.ExecuteNonQuery(); 

    } 
} 
+0

你不需要'conn.close()'部分。 '使用'语句处理。 –