我有这个Update方法,如果正确与否,请你纠正我的sql语句吗?因为我尝试更新IDnumber并将其更改为我的数据库中的现有IDnumber,所以它只是更新,甚至没有尝试错误捕获它。
我的代码现在是:
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();
}
}
你能告诉我在哪里以及我做错了什么?
(适用EDIT)
答案 0 :(得分:1)
UPDATE
命令不会将任何记录返回Read
,ExecuteReader
仅用于SELECT
命令。您可以使用ExecuteNonQuery
正常更新,并使用try-catch
了解是否有错误:
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);
cmd.ExecuteNonQuery();// <-- this is what you want
MessageBox.Show("Data Successfully Updated!");
FAddVoters._cleardata = cleardata;
FAddVoters._checkID = "0";
}
} catch (SqlException ex) {
if(ex.Number == 2627)//duplicated primary key
{
MessageBox.Show("ID number already exist!");
FAddVoters._cleardata = "0";
FAddVoters._checkID = checkID;
} else {
MessageBox.Show("There was some error while attempting to update!\nTry again later.");
}
}
finally {
sc.Close();
}
}
如果您无法将idnum
作为主键(由于某些设计原因),您必须执行一些查询来检查存在,如下所示:
public bool ExistsKey(string keyField, string table, string value, SqlConnection con){
try {
if(con.State != ConnectionState.Open) con.Open();
using(SqlCommand com = new SqlCommand(
string.Format("IF EXISTS(SELECT * FROM {0} WHERE {1}='{2}') SELECT 1 ELSE SELECT 0",
table, keyField, value), con)){
var result = com.ExecuteScalar();
return result != null && (int)result == 1;
}
} catch {
return false;
}
finally {
con.Close();
}
}
//Then use that method in your code like this:
if(ExistsKey("idnum", "TableVotersInfo", _idnum.ToString(), sc)){
MessageBox.Show("ID number already exist!");
FAddVoters._cleardata = "0";
FAddVoters._checkID = checkID;
} else {
//perform your update here ...
}