c#提示“insert into”语法错误

来源:百度知道 编辑:UC知道 时间:2024/06/01 02:51:09
c#连接access数据库,向表中插入记录,总是提示出错,盯了半天代码,没看出问题来,请各位指点。代码如下:
string insert = "insert into 图书管理( 书名,书号,ISBN,图书分类,出版日期,发行数) values ('";
insert += this.textBox1.Text + "','";
insert += this.textBox2.Text + "','";
insert += this.textBox3.Text + "','";
insert += this.textBox4.Text + "',#";
insert += this.textBox5.Text + "#,";
insert += this.textBox6.Text + ")";
其中出版日期为日期型,发行数为数字,其他均为文本型。
语法没有错误了,不过我的“出版日期”字段是可以为空的,如果不添加该字段,还是会报错,错误信息:该字符串未被识别为有效的DataTime。

string insert = "insert into 图书管理( 书名,书号,ISBN,图书分类,出版日期,发行数) values (";
insert += "'"+this.textBox1.Text + "',";
insert += "'"+this.textBox2.Text + "',";
insert += "'"+this.textBox3.Text + "',";
insert += "'"+this.textBox4.Text + "',";
insert += "'"+Convert.ToDataTime(this.textBox5.Text) + "',";
insert += ""+Convert.ToInt16(this.textBox6.Text) + ")";

string insert = "insert into 图书管理( 书名,书号,ISBN,图书分类,出版日期,发行数) values ('";
insert += this.textBox1.Text + "','";
insert += this.textBox2.Text + "','";
insert += this.textBox3.Text + "','";
insert += this.textBox4.Text + "',";
if (this.textBox5.Text == "")
{