SoFunction
Updated on 2025-04-11

Net insert into syntax error detailed explanation

Problem description:
I write data to access data in the oledb method. The sample source code is as follows:
Copy the codeThe code is as follows:

sql="select * from multitable";
oledbdataadapter olesub=new oledbdataadapter(sql,olecn);
oledbcommandbuilder cb1=new oledbcommandbuilder(olesub);
dataset ds=new dataset();
(ds."multitable");
datatable dt=["multitable"];
datarow dr=();
dr["prserv"]="ws"+().padleft(6,''''0'''');
dr["number"]="00063";
(dr);
(ds,"mulittable");

There is no problem when compiling this code, but when running, a runtime error will be reported: "Syntax error of the insert into statement". When using oledbadapter, I did not specify the insert statement, but used oledbcommandbuilder to automatically generate the insert statement. After thinking about it carefully, why did this error occur? My conclusion is that the field names in this table may use reserved words from the access system. So I created a query in access and wrote an insert sql myself to confirm that my conclusion is correct. Number is a reserved word in the system. How to modify it?
Generally speaking, the easiest way is to change the field name and replace it with a name that is not reserved by the system. However, the structure of the library is provided by the customer and is not allowed to be modified. Only other ways are needed. Considering previous experience, when operating access and sql server, if the table fields contain reserved words from the system, we just need to add square brackets outside the field, such as insert into tblmultitable(prserv,[number]) values(.........). But from the above code we see that there is no place we can specify the insert statement. I think the oledbcommandbuilder should automatically generate the insert statement based on the select statement used by the adapter, so just add square brackets to the fields in the select statement, so I made the following modifications:
string sql="select prserv,[number],priorref,grantor,grantee from multitable";
After the modification is completed, after the test, the previous "insert into statement syntax error" still occurs; what will be the problem? I think it should be on the oledbcommanbuilder. Generally speaking, you only need to use the oledbcommanbuilder class like this:
oledbdataadapter olesub=new oledbdataadapter(sql,olecn);
oledbcommandbuilder cb1=new oledbcommandbuilder(olesub);
Open msdn and look at the class members of the oledbcommanbuilder. I found two very critical attributes: quoteprefix, quotesuffix; if you think about it carefully, there are many types of data that oledb can access, so the processing methods of prefix and suffix of key fields must be different. For example, when accessing excel, it is stated that it should be written as [sheet1$], so it is quite flexible to provide such a method. Next I modify the code again and assign values ​​to these two properties:
Copy the codeThe code is as follows:

ataadapter olesub=new oledbdataadapter(sql,olecn);
oledbcommandbuilder cb1=new oledbcommandbuilder(olesub);
="[";
="]";