求助帖,vc#sql编程出问题了,求助各位大神
private void button3_Click(object sender, EventArgs e)
{
if (textBox1.Text.Trim() == "")
{
MessageBox.Show("请输入学号!");
return;
}
SqlConnection conn = new SqlConnection(strcon);
string sqlStr = "update [chengjiguanli] set";
if (textBox2.Text.Trim().ToString() != "")//如果姓名有输入
{ sqlStr += "[xm]='" + textBox2.Text.Trim() + "',"; }
if (textBox3.Text.Trim() != "")//如果教师名有输入
{ sqlStr += "[jsm]='" + textBox3.Text.Trim() + "',"; }
if (textBox4.Text.Trim() != "")//如果课程名称有输入
{ sqlStr += "[kcmc]='" + textBox4.Text.Trim() + "',"; }
if (textBox5.Text.Trim() != "")//如果分数有输入
{ sqlStr += "[fs]='" + textBox5.Text.Trim() + "',"; }
if (textBox6.Text.Trim() != "")//如果状态有输入
{ sqlStr += "[zt]='" + textBox6.Text.Trim() + "',"; }
if (FileNamePath != "")//如果选择了照片
{ sqlStr += "[zp]=@zp,"; }
sqlStr += "[kclx]='" + comboBox1.Text + "',";
sqlStr += " where xh='" + textBox1.Text.Trim() + "'";
SqlCommand cmd = new SqlCommand(sqlStr, conn);
if (FileNamePath != "") //如果选择了照片
{
FileStream fs = null;
fs = new FileStream(FileNamePath, FileMode.Open, FileAccess.Read);
MemoryStream mem = new MemoryStream();
byte[] data1 = new byte[fs.Length];
fs.Read(data1, 0, (int)fs.Length);
cmd.Parameters.Add("@zp", SqlDbType.VarBinary); //这里选择VarBinary类型
cmd.Parameters["@zp"].Value = data1; //把照片变化成字节数组
}
try
{
conn.Open();
int yxh = cmd.ExecuteNonQuery();
if (yxh == 1) //如果受影响的行数为1则修改成功
{ MessageBox.Show("修改成功"); }
else
{ MessageBox.Show("数据库中没有此学生!"); }
this.chengjiguanliTableAdapter.Fill(this.fATEDataSet.chengjiguanli);
}
catch (Exception ex)
{ MessageBox.Show("出错,没有完成成绩的修改!" + ex.Message); }
finally
{
conn.Close();
FileNamePath = "";
}
}
程序运行后会出现“出错,没有完成成绩的修改!关键字‘where’附近有语法错误“的提示
我检查不出来错误,求助各位大神。。。
[解决办法]
sqlStr += "[kclx]='" + comboBox1.Text + "',";
改为
sqlStr += "[kclx]='" + comboBox1.Text + "' ";