我有这个更新方法,请问我的sql语句是否正确?因为我尝试更新 ID 号并将其更改为数据库中现有的 ID 号,所以它无论如何都会更新,甚至不会尝试错误捕获它。
我现在的代码是:
public void Update()
{
sc.Open();
try
{
using (cmd = new SqlCommand(@"UPDATE TableVotersInfo SET Education=@ed, idnum=@idnum, FirstName=@firstname, MiddleName=@middlename, LastName=@lastname, SchoolYear=@schoolyear, ControlNum=@controlnum WHERE id=@id
SELECT @ed, @idnum, @firstname, @middlename, @lastname, @schoolyear, @controlnum
WHERE @id NOT IN (SELECT idNum FROM TableVotersInfo);", sc))
{
cmd.Parameters.AddWithValue("@id", _id);
cmd.Parameters.AddWithValue("@ed", _ed);
cmd.Parameters.AddWithValue("@idnum", _idnum);
cmd.Parameters.AddWithValue("@firstname", _firstname);
cmd.Parameters.AddWithValue("@middlename", _middlename);
cmd.Parameters.AddWithValue("@lastname", _lastname);
cmd.Parameters.AddWithValue("@schoolyear", _schoolyear);
cmd.Parameters.AddWithValue("@controlnum", _controlnum);
SqlDataReader reader = cmd.ExecuteReader();
if(reader.Read())
{
MessageBox.Show("ID number already exist!");
FAddVoters._cleardata = "0";
FAddVoters._checkID = checkID;
}
else
{
MessageBox.Show("Data Successfully Updated!");
FAddVoters._cleardata = cleardata;
FAddVoters._checkID = "0";
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
sc.Close();
}
}
你能告诉我我在哪里做错了吗?
(编辑)
(来源:akamaihd.net)