我知道之前已经问过这个问题,但我仍然没有得到关于为什么我的代码没有更新到数据库的答案。此代码没有错误,但它不会将更改数据更新到数据库中。有人请帮助我。这是我的代码:
private void button1_Click_1(object sender, EventArgs e)
{
con.Open();
bool exists2;
using (SqlCommand cmd2 = new SqlCommand("select count(*) from [m_emp_photo] where empno=@empno", con))
{
cmd2.Parameters.AddWithValue("@empno", textBoxEmpNo.Text);
cmd2.Parameters.AddWithValue("@path", textBoxImgPath.Text);
exists2 = (int)cmd2.ExecuteScalar() > 0;
}
if (exists2)
{
string query2 = "";
//query2 = "update m_emp_photo set path=@path where empno=@empno";
query2 = "UPDATE m_emp_photo set path=@path where empno=@empno";
SqlCommand cmd2;
cmd2 = new SqlCommand(query2, con);
cmd2.Parameters.AddWithValue("@path", textBoxImgPath.Text);
MessageBox.Show("Changes has been saved!");
}
else
{
MessageBox.Show("No record found");
}
con.Close();
}
答案 0 :(得分:1)
您只需要执行更新:
private void button1_Click_1(object sender, EventArgs e)
{
con.Open();
bool exists2;
using (SqlCommand cmd2 = new SqlCommand("select count(*) from [m_emp_photo] where empno=@empno", con))
{
cmd2.Parameters.AddWithValue("@empno", textBoxEmpNo.Text);
cmd2.Parameters.AddWithValue("@path", textBoxImgPath.Text);
exists2 = (int)cmd2.ExecuteScalar() > 0;
}
if (exists2)
{
string query2 = "";
//query2 = "update m_emp_photo set path=@path where empno=@empno";
query2 = "UPDATE m_emp_photo set path=@path where empno=@empno";
SqlCommand cmd2;
cmd2 = new SqlCommand(query2, con);
cmd2.Parameters.AddWithValue("@path", textBoxImgPath.Text);
cmd2.ExecuteNonQuery();
MessageBox.Show("Changes has been saved!");
}
else
{
MessageBox.Show("No record found");
}
con.Close();
}
答案 1 :(得分:0)
我猜你错过了在If语句中执行你的SQLCommand
使用cmd2.ExecuteNonQuery();
private void button1_Click_1(object sender, EventArgs e)
{
con.Open();
bool exists2;
using (SqlCommand cmd2 = new SqlCommand("select count(*) from [m_emp_photo] where empno=@empno", con))
{
cmd2.Parameters.AddWithValue("@empno", textBoxEmpNo.Text);
exists2 = (int)cmd2.ExecuteScalar() > 0;
}
if (exists2)
{
string query2 = "";
//query2 = "update m_emp_photo set path=@path where empno=@empno";
query2 = "UPDATE m_emp_photo set path=@path where empno=@empno";
SqlCommand cmd2;
cmd2 = new SqlCommand(query2, con);
cmd2.Parameters.AddWithValue("@path", textBoxImgPath.Text);
cmd2.Parameters.AddWithValue("@empno", textBoxEmpNo.Text);
cmd2.ExecuteNonQuery();
MessageBox.Show("Changes has been saved!");
}
else
{
MessageBox.Show("No record found");
}
con.Close();
}
答案 2 :(得分:0)
第一个cmd2不需要添加@path
参数。
第二个cmd2您必须添加@empno
参数并运行ExecuteNonQuery
。
我希望它对你有用。
private void button1_Click_1(object sender, EventArgs e)
{
con.Open();
bool exists2;
using (SqlCommand cmd2 = new SqlCommand("select count(*) from [m_emp_photo] where empno=@empno", con))
{
cmd2.Parameters.AddWithValue("@empno", textBoxEmpNo.Text);
//cmd2.Parameters.AddWithValue("@path", textBoxImgPath.Text);
exists2 = (int)cmd2.ExecuteScalar() > 0;
}
if (exists2)
{
string query2 = "";
//query2 = "update m_emp_photo set path=@path where empno=@empno";
query2 = "UPDATE m_emp_photo set path=@path where empno=@empno";
SqlCommand cmd2;
cmd2 = new SqlCommand(query2, con);
cmd2.Parameters.AddWithValue("@empno", textBoxEmpNo.Text);
cmd2.Parameters.AddWithValue("@path", textBoxImgPath.Text);
cmd2.ExecuteNonQuery();
MessageBox.Show("Changes has been saved!");
}
else
{
MessageBox.Show("No record found");
}
con.Close();
}
答案 3 :(得分:0)
您的代码中存在多个问题。
首先,您没有执行更新。添加以下行:
cmd2.ExecuteNonQuery();
其次,您的第一个命令没有@path
参数,那么为什么要添加它?
第三,你构造了一个新命令,所以你丢失了参数。您必须再次添加@empno
参数,就像使用@path
:
cmd2.Parameters.AddWithValue("@empno", textBoxEmpNo.Text);
第四,既然你构造了一个新命令,你需要另一个using
语句。
您的代码应该是这样的:
private void button1_Click_1(object sender, EventArgs e)
{
con.Open();
bool exists2;
using (SqlCommand cmd2 = new SqlCommand("select count(*) from [m_emp_photo] where empno=@empno", con))
{
cmd2.Parameters.AddWithValue("@empno", textBoxEmpNo.Text);
exists2 = (int)cmd2.ExecuteScalar() > 0;
}
if (exists2)
{
using (SqlCommand cmd2 = new SqlCommand("UPDATE m_emp_photo set path=@path where empno=@empno", con))
{
cmd2.Parameters.AddWithValue("@empno", textBoxEmpNo.Text);
cmd2.Parameters.AddWithValue("@path", textBoxImgPath.Text);
cmd2.ExecuteNonQuery();
MessageBox.Show("Changes has been saved!");
}
}
else
{
MessageBox.Show("No record found");
}
con.Close();
}
最后一件事。我不知道您正在使用哪个数据库,但几乎所有数据库中的update
语句都会返回更新的行数。您可以将其用作反馈,无论您的更新语句是否成功。像这样:
if(cmd2.ExecuteNonQuery() == 1)
{
MessageBox.Show("Changes has been saved!");
}
else
{
MessageBox.Show("Failed to save changes!");
}
然而,这还不够,强烈建议将整个代码包装在try...catch
语句中并显示友好错误。