简体   繁体   中英

error while insert a record to ms access

I have executed the above query, and I got an error :

{System.Data.OleDb.OleDbException: Syntax error. in query expression 
'Select [UserID] from UserDetails'

and my sample code is below:

OleDbCommand cmd1 = new OleDbCommand("Insert into UserCompanyDetails([UserID],[CompanyID]) values (" + "Select [UserID] from UserDetails" + "," + "Select @@identity" + ");", conn);
                        conn.Open();
                        cmd1.ExecuteNonQuery();
                        conn.Close();

Why am I getting this error?

您应该将子查询放在括号之间:

"Insert into UserCompanyDetails([UserID],[CompanyID]) values ((Select [UserID] from UserDetails),(Select @@identity))"

Another approach:

Try to save the results of the following queries in variables of their respective datatypes.

Select [UserID] from UserDetails
Select @@identity

Then you can simply use those variables in your Main Query String

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM