一段修改程序,运行后老显示“出错,where附近出错”
一段修改程序,运行后老显示“出错,where附近出错” 求大神教育啊。。。
private void button3_Click(object sender, EventArgs e)
{
if (textBox1.Text.Trim() == "")
{
MessageBox.Show("请输入班级编号!");
return;
}
SqlConnection conn = new SqlConnection(strcon);
string sqlStr = "update [banjiguanli] set";
if (textBox2.Text.Trim().ToString() != "")
{ sqlStr += "[bjmc]='" + textBox2.Text.Trim() + "',"; }
if (textBox3.Text.Trim() != "")
{ sqlStr += "[bzr]='" + textBox3.Text.Trim() + "',"; }
if (textBox4.Text.Trim() != "")
{ sqlStr += "[zrs]='" + textBox4.Text.Trim() + "',"; }
if (textBox5.Text.Trim() != "")
{ sqlStr += "[zydm]='" + textBox5.Text.Trim() + "',"; }
sqlStr += " where bjbh='" + textBox1.Text.Trim() + "'";
SqlCommand cmd = new SqlCommand(sqlStr, conn);
try
{
conn.Open();
int cnm = cmd.ExecuteNonQuery();
if (cnm == 1) //如果受影响的行数为1则修改成功
{ MessageBox.Show("修改成功"); }
else
{ MessageBox.Show("数据库中没有此班级!"); }
this.banjiguanliTableAdapter.Fill(this.FATADataSet.banjiguanli);
}
catch (Exception ex)
{ MessageBox.Show("出错,没有完成班级信息的修改!" + ex.Message); }
finally
{
conn.Close();
}
}
------解决方案--------------------