为什么我的更新查询不起作用

时间:2014-04-26 09:46:17

标签: c# ado.net

当我点击更新按钮时,它显示成功,但我的数据库没有更新。 我的代码有什么问题:

private void CmdUpdate_Click(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection(@"Data Source......");
    conn.Open();
    SqlCommand comm = new SqlCommand("update Leaves_Type set Leaves='"+txtltype.Text+"' where Leaves='"+txtltype.Text+"'");        
    comm.Connection = conn;
    comm.ExecuteNonQuery();
    MessageBox.Show("Successfully Updated");
    conn.Close(); 
}

2 个答案:

答案 0 :(得分:1)

update Leaves_Type set Leaves='"+txtltype.Text+"' where Leaves='"+txtltype.Text+"'"

您想看到更新的内容?您可以使用相同的值更新字段。

答案 1 :(得分:0)

我认为在您的情况下,关于错误,您违反了PRIMARY KEY constraint。您可以先尝试使用Primary Key进行更新。并且还使用参数化SQL。

而不是:

update Leaves_Type set Leaves='"+txtltype.Text+"' where Leaves='"+txtltype.Text+"'"

你可以这样做:

update Leaves_Type set Leaves='"+txtltype.Text+"' where LeavesID= someValue " //And you can retrieve
                 //   someValue by appropriate Request if LeavesID is your PrimaryKey
相关问题