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

一个很小的winform程序 insert为什么会报错??
private void buttonX1_Click(object sender, EventArgs e)
  {
  string state = string.Empty;
  if (this.radioButton1.Checked == true)
  {
  state = "1";
  }
  else if (this.radioButton2.Checked == true)
  {
  state = "0";
  }
  else
  {
  MessageBox.Show("请选择一个开关");
  return;
  }
  int rs = 0;
  string sql = string.Format("insert into MyTable (Id,PARA_TYPE,PARA_NAME,PARA_STATES,NOTE) values ('{0}','2','{1}','{2}','{3}')",textBoxX1.Text,textBoxX2.Text,state,textBoxX3.Text);
  OleDbConnection connection = new OleDbConnection(strcon);
  OleDbCommand cmd = new OleDbCommand(sql, connection);
  connection.Open();
  rs = cmd.ExecuteNonQuery();
  connection.Close();
  if (rs == 1)
  {
  MessageBox.Show("成功");
  }
  else
  {
  MessageBox.Show("失败");
  }
   
  }




提示insert 语法有错误

------解决方案--------------------
string sql = string.Format("insert into MyTable ([Id],[PARA_TYPE],[PARA_NAME],[PARA_STATES],[NOTE]) values ('{0}','2','{1}','{2}','{3}')",textBoxX1.Text,textBoxX2.Text,state,textBoxX3.Text);
红色部分注意,也许有关键字