private void button1_Click(object sender, EventArgs e)
{
string sqlConnection=null; if (textBox1.Text == "")
{ MessageBox.Show("请输入科室编号!"); return; }
if (textBox2.Text == "")
{ MessageBox.Show("请输入科室名称!"); return; } sqlConnection = "insert into 科室表 values(" + textBox1.Text + "," + textBox2.Text + ")";
if (ExeSQL(sqlConnection))
MessageBox.Show("插入成功!");
else MessageBox.Show("插入失败!");
}
public bool ExeSQL(string strSQL)
{
bool resultState = false;
oleDbConnection.Open();
System.Data.OleDb.OleDbTransaction myTrans = oleDbConnection.BeginTransaction(); OleDbCommand command = new OleDbCommand(strSQL, oleDbConnection, myTrans);
try
{
command.ExecuteNonQuery();
myTrans.Commit();
resultState = true;
}
catch
{
myTrans.Rollback();
resultState = false;
}
finally
{
oleDbConnection.Close();
}
return resultState;
}
上面这个是执行语句,其中科室表在Access中对应的两个Filed,分别为一个数字,一个文本。
现在描述问题,这样插入insert into 科室表 values(1001,1); <也就是控件textBox2上填1 > 插入成功
insert into 科室表 values(1001,妇产科); <也就是控件textBox2上填妇产科 > 插入就失败
怎么回事?分没多少了~大家帮帮忙
sqlConnection = "insert into 科室表 values(" + textBox1.Text + ",'" + textBox2.Text + "')";
//值都加上单引号,这样不会出错,
最好的是用参数化的方式插入