MS Access DB插入查询不起作用

时间:2015-03-11 04:49:00

标签: c# ms-access

我正在尝试将数据插入MS ACCESS DB。一切都好。连接,DB路径等 其中有一个表 CIT 。 我正在使用此Insert into查询

string query = "INSERT INTO CIT (GRNO:, Name, FName, CNIC, Address, ContactNO, Gender, Qualification, DOB, RegDate, Photo) VALUES ('" + txtGRNO.Text + "','" + txtName.Text + "','" + txtFName.Text + "','1234','" + txtAddress.Text + "','" + txtContact.Text + "','" + cBoxGender.Text + "','" + cBoxQual.Text + "','" + dteDOB.Text + "','" + dteReg.Text + "','" + path + "');";

我尝试了一切,但似乎无法找到这里的错误。字段的数据类型是DB中的Text,&当我执行查询时,它会给出错误 Syntax Error in MS Access

2 个答案:

答案 0 :(得分:1)

您的表格包含两个名称有问题的字段:GRNO:;和Name

由于GRNO:包含冒号,您可以将其括在方括号中,以便db引擎接受它:[GRNO:]

由于Name是一个reserved word,所以也将其包含在方括号中。

"INSERT INTO CIT ([GRNO:], [Name], ... 

除了这些字段名称问题之外,标准建议是对INSERT使用参数查询。请注意,您仍需要在参数查询中包含这些问题名称。

此外,如果您愿意,Access将允许您使用反向标记而不是方括号...

"INSERT INTO CIT (`GRNO:`, `Name`, ... 

答案 1 :(得分:0)

尝试:

string query = "INSERT INTO CIT (GRNO, Name, FName, CNIC, Address, ContactNO, Gender, Qualification, DOB, RegDate, Photo) VALUES ('" + txtGRNO.Text + "','" + txtName.Text + "','" + txtFName.Text + "','1234','" + txtAddress.Text + "','" + txtContact.Text + "','" + cBoxGender.Text + "','" + cBoxQual.Text + "','" + dteDOB.Text + "','" + dteReg.Text + "','" + path + "');";

;字段后删除GRNO

相关问题