Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand net insert into syntax errors

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "how to understand net insert into grammatical errors". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand net insert into grammatical errors.

Problem description:

I use oledb to write data to access data. The example source code is as follows:

The copy code is as follows:

Sql= "select * from multitable"

Oledbdataadapter olesub=new oledbdataadapter (sql,olecn)

Oledbcommandbuilder cb1=new oledbcommandbuilder (olesub)

Dataset ds=new dataset ()

Olesub.fill (ds. "multitable")

Datatable dt=ds.tables ["multitable"]

Datarow dr=dt.newrow ()

Dr ["prserv"] = "ws" + index.tostring () .padleft (6)

Dr ["number"] = "00063"

Dt.rows.add (dr)

Olesub.update (ds, "mulittable")

There is no problem with this code at compile time, but at run time, a run-time error is reported: "syntax error in the insert into statement." When I use oledbadapter, I do not specify the insert statement, but use oledbcommandbuilder to automatically generate the insert statement. After thinking about it carefully, why did this error occur? My conclusion is that maybe the field names in this table use the reserved words of 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 of the system, so how to modify it?

Generally speaking, the easiest way is to change the name of this field to the name of a non-system reserved word, but the structure of the library is provided by the customer and is not allowed to be modified. Considering previous experience, when operating access,sql server, if the field of the table contains the reserved word of the system, we can add parentheses to the field, such as insert into tblmultitable (prserv, [number]) values (.) Just do it. However, we can see from the above code that there is no place where we can specify the insert statement. I think oledbcommandbuilder automatically generates the insert statement based on the select statement used by adapter, so just add square brackets to the fields in the select statement, so I made the following changes:

After the modification, and after the test, the previous "syntax error of the insert into statement" still occurs; what will the problem be? I think it should be on oledbcommanbuilder, generally speaking, just use the oledbcommanbuilder class like this:

Oledbdataadapter olesub=new oledbdataadapter (sql,olecn)

Oledbcommandbuilder cb1=new oledbcommandbuilder (olesub)

Open msdn and see the class members of oledbcommanbuilder. Find two very critical attributes: quoteprefix,quotesuffix; think carefully, oledb can access a lot of data types, so the prefixes and suffixes of key fields must be handled differently, for example, when accessing excel, it should be written as [sheet1 $], so providing such a way is quite flexible. Next, I modify the code again to assign values to these two attributes:

The copy code is as follows:

Ataadapter olesub=new oledbdataadapter (sql,olecn)

Oledbcommandbuilder cb1=new oledbcommandbuilder (olesub)

Cb1.quoteprefix= "["

Cb1.quotesuffix= "]"

At this point, I believe you have a deeper understanding of "how to understand net insert into syntax errors". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report