插入记录到ms访问时出错

时间:2013-06-06 09:10:30

标签: c#

我已执行上述查询,但收到错误:

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

我的示例代码如下:

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

为什么我收到此错误?

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

另一种方法:

尝试将以下查询的结果保存在各自数据类型的变量中。

Select [UserID] from UserDetails
Select @@identity

然后,您只需在主查询字符串

中使用这些变量即可