读书人

用C#net 向ACCESS数据库插入一条数据出

发布时间: 2012-01-21 21:31:43 作者: rapoo

用C#.net 向ACCESS数据库插入一条数据出现的问题

C# code
    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上填妇产科 > 插入就失败
怎么回事?分没多少了~大家帮帮忙

[解决办法]
看来你比较新手

妇产科加上单引号就可以了'妇产科'

读书人网 >C#

热点推荐