我是C#和SQL的新手。现在从表单中我可以访问类中的函数。
我的代码是
public void updateSupplierInformation(string id, string name, string balance, string place, string address, string phone, string bankname, string bankbranch, string accountno)
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
SqlCommand NewCmd = conn.CreateCommand();
NewCmd.Connection = conn;
NewCmd.CommandType = CommandType.Text;
NewCmd.CommandText = " update supplier set " + " ID = " + "'" + id + "'" + " , NAME = " + "'" + name + "'" + " , BALANCE = " + "'" + balance + "'" + " , PLACE = " + "'" + place + "'" + " , LOCATION = " + "'" + address + "'" + ", PHONE = " + "'" + phone + "'" + " , BANK_NAME = " + "'" + bankname + "'" + " , BANK_BRANCH = " + "'" + bankbranch + "'" + ", ACCOUNT_NO = " + "'" + accountno + "'" + " where ID = " + "@id";
NewCmd.Parameters.AddWithValue("@id",id);
NewCmd.ExecuteNonQuery();
conn.Close();
}
现在,如果具有给定id
的数据库中不存在记录,则应用程序立即停止。我怎么处理这个?我想显示一条消息,表明输入的数据错误,并要求用户输入其他数据
答案 0 :(得分:21)
ExecuteNonQuery()返回受INSERT,UPDATE或DELETE语句影响的行数。如果需要检查sql异常,则必须在函数中包含try catch语句。
public void updateSupplierInformation(string id, string name, string balance, string place, string address, string phone, string bankname, string bankbranch, string accountno)
{
try
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
SqlCommand NewCmd = conn.CreateCommand();
NewCmd.Connection = conn;
NewCmd.CommandType = CommandType.Text;
NewCmd.CommandText = " update supplier set " + " ID = " + "'" + id + "'" + " , NAME = " + "'" + name + "'" + " , BALANCE = " + "'" + balance + "'" + " , PLACE = " + "'" + place + "'" + " , LOCATION = " + "'" + address + "'" + ", PHONE = " + "'" + phone + "'" + " , BANK_NAME = " + "'" + bankname + "'" + " , BANK_BRANCH = " + "'" + bankbranch + "'" + ", ACCOUNT_NO = " + "'" + accountno + "'" + " where ID = " + "@id";
NewCmd.Parameters.AddWithValue("@id",id);
int a=NewCmd.ExecuteNonQuery();
conn.Close();
if(a==0)
//Not updated.
else
//Updated.
}
catch(Exception ex)
{
// Not updated
}
}
答案 1 :(得分:18)
ExecuteNonQuery
返回受影响的行数 - 如果为0,则表示没有匹配的行要更新。当然,只有当更新实际上“工作”时才会抛出异常...而我怀疑它会在你的情况下抛出一个异常,这可能不是与行有关不存在于数据库中。 (可能有一些代码没有显示, 依赖于现有的行,请注意。)
此外:
using
语句可靠地处理资源。using
部署)新连接,并让连接池处理效率我的猜测(在随意的检查中)问题是你的更新语句试图更新ID,这可能是只读的。但是当你修复异常处理时,你会发现它。
答案 2 :(得分:0)
我知道已经发布了一个答案,但我们会尝试别的东西:
SqlConnection SQLConn = new SqlConnection("datahere");
SqlCommand SQLComm = new SqlCommand();
SQLcomm.Connection = SQLConn;
SQLConn.Open();
SQLComm.CommandText = "SQL statement goes here"
SqlDataReader dataReader = SQLComm.ExecuteReader();
dataReader.Read();
if(dataReader.HasRows == true){ //if it has rows do code
//do code
}
else{
// do other code
}
HasRows将返回一个bool值
答案 3 :(得分:0)
只需检查条件
int a=NewCmd.ExecuteNonQuery();
if(a==0)
//Not updated.
else
//Updated.
ExecuteNonQuery()->此函数返回整数值。
谢谢
答案 4 :(得分:0)
让 sql = UPDATE TABLE SET column = '${data}' WHERE column = "value
db.query(sql, (err, result)=> {
console.log(result.affectedRows);
if(err) {
console.log(err);
return res.send({
message: "Error occured. please retry",
});
} else if(result.affectedRows != 0) {
console.log(result)
return res.send({
success:true,
message: "updated!"
});
}else if(result.affectedRows == 0) {
console.log(result)
return res.send({
success:false,
message: "not updated!"
});
} else {
console.log(result)
}
})
//this is a nodejs code