c# 怎么对数据库插入和更新后,然后举行判断,成功与否!
string connString = "server=.;database=Cyglxt;uid=sa;pwd=123456;";
SqlConnection conn = new SqlConnection(connString);
conn.Open();
SqlCommand cmd = new SqlCommand("insert into tb_people(ID,Name,Sex,Affiliation,Education,Professional,Telephone,Email,Specialty,Address,Resume,Workp) values('" + tbID.Text.Trim() + "','" + tbName.Text.Trim() + "','" + cbSex.SelectedItem.ToString() + "','" + textBox2.Text.Trim() + "','" + textBox3.Text.Trim() + "','" + textBox4.Text.Trim() + "','" + textBox5.Text.Trim() + "','" + textBox6.Text.Trim() + "','" + textBox7.Text.Trim() + "','" + textBox8.Text.Trim() + "','" + textBox9.Text.Trim() + "','" + textBox10.Text.Trim() + "')", conn);
cmd.ExecuteNonQuery();
conn.Close();
if (tbID.Text == "")
{
MessageBox.Show("员工编号不能为空!", "提示对话框", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
if () {
MessageBox.Show("数据添加成功!");
}
else
{
MessageBox.Show("数据添加失败!");
}
}
请问在if()括号里写入什么代码?来判断成功与否?求助!!!!
------解决方案--------------------
cmd.ExecuteNonQuery();
返回的是int类型,这个返回值代表了其影响的数据库的记录数。
string connString = "server=.;database=Cyglxt;uid=sa;pwd=123456;";
SqlConnection conn = new SqlConnection(connString);
conn.Open();
SqlCommand cmd = new SqlCommand("insert into tb_people(ID,Name,Sex,Affiliation,Education,Professional,Telephone,Email,Specialty,Address,Resume,Workp) values('" + tbID.Text.Trim() + "','" + tbName.Text.Trim() + "','" + cbSex.SelectedItem.ToString() + "','" + textBox2.Text.Trim() + "','" + textBox3.Text.Trim() + "','" + textBox4.Text.Trim() + "','" + textBox5.Text.Trim() + "','" + textBox6.Text.Trim() + "','" + textBox7.Text.Trim() + "','" + textBox8.Text.Trim() + "','" + textBox9.Text.Trim() + "','" + textBox10.Text.Trim() + "')", conn);
int ret = (int)cmd.ExecuteNonQuery(); conn.Close();
if (tbID.Text == "")
{
MessageBox.Show("员工编号不能为空!", "提示对话框", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
if (ret>0) {
MessageBox.Show("数据添加成功!");
}
else
{
MessageBox.Show("数据添加失败!");
}
}