日期:2014-05-18  浏览次数:20857 次

这是图书馆管理系统的读者管理,该代码段是为了实现读者的信息修改,在窗口中没有错,可是一运行修改读者信息就出错了,我找了半天也不知道哪错了,谁可以帮我找一下错
if (txtreaderID.Text.Trim() == "")
  {
  MessageBox.Show("请输入借书证号!", "警告", 0);
  return;
  }
  SqlConnection conn = new SqlConnection(sqlcon);
  string sqlstr = "update [TReader] set";
  if (txtname.Text.Trim().ToString() != "")
  {
  sqlstr += "[Name]='" + txtname.Text.Trim() + "',";
  }
  if (txtspec.Text.Trim() != "")
  {
  sqlstr += "[Spec]='" + txtspec.Text.Trim() + "',";
  }
  if (txtborn.Text.Trim() != "")
  {
  sqlstr += "[Born]='" + txtborn.Text.Trim() + "',";
  }
  if (txtsex.Text.Trim() != "")
  {
  sqlstr += "[Sex]='" + txtsex.Text.Trim() + "',";
  }
  if (txtphoto.Text.Trim() != "")
  {
  sqlstr += "[Photo]= @Photo";
   
  }
  sqlstr += "where ReaderID='" + txtreaderID.Text.Trim() + "'";
   
  SqlCommand cmd = new SqlCommand(sqlstr, conn);
  if (txtphoto.Text.Trim() != "")
  {
  FileStream fs = null;
  fs = new FileStream(txtphoto.Text.Trim(), FileMode.Open, FileAccess.Read);
  MemoryStream mem = new MemoryStream();
  byte[] data1 = new byte[fs.Length];
  fs.Read(data1,0,(int)fs.Length);
  cmd.Parameters.Add("@Photo", SqlDbType.Image);
  cmd.Parameters["@Photo"].Value = data1;
  }
  try
  {
  conn.Open();
  int yxh = cmd.ExecuteNonQuery();
  if (yxh == 1)
  {
  MessageBox.Show("修改成功!", "提示", 0);
  }
  else
  {
  MessageBox.Show("数据库中没有此读者!", "警告", 0);
  }
  this.tReaderTableAdapter.Fill(this.mBOOKDataSet.TReader);
  }
  catch (Exception ex)
  {
  MessageBox.Show("出错,没有完成读者的修改!" + ex.Message, "提示", 0);
  }
  finally
  {
  conn.Close();
  }

------解决方案--------------------
假如那些文本框中的信息都有,那
sqlstr += "[Name]='" + txtname.Text.Trim() + "',"; [Name]前面少空格;
sqlstr += "where ReaderID='" + txtreaderID.Text.Trim() + "'"; where 前面少空格,
假如 txtphoto.Text.Trim() != "" 判断不成立,还会多出逗号来。