执行以下c#代码时出错

时间:2013-06-06 07:09:28

标签: c#

我尝试使用以下代码删除表中的行,但是我收到了错误。

{System.Data.OleDb.OleDbException: Missing ), ], or Item in query expression 
'[CompanyID] not in (Select [CompanyID] from EmployeeDetails 
where [CompanyID] = @cmpID'.

这是我的代码:

int companyID = _cmpDetailsList[i].CompanyID;        
OleDbCommand upcmd = new OleDbCommand(
    "delete * from CompanyDetails where [CompanyID] not in " + 
    "(Select [CompanyID] from EmployeeDetails where [CompanyID] = @cmpID", conn);
conn.Open(); 
upcmd.Parameters.AddWithValue("@cmpID", companyID);
upcmd.ExecuteNonQuery();
conn.Close();

我以这种格式修改了命令

OleDbCommand upcmd = new OleDbCommand("delete * from CompanyDetails where [CompanyID]= @cmpID and not in (Select [CompanyID] from EmployeeDetails where [CompanyID] = @cmpID)", conn);

我收到以下错误

System.Data.OleDb.OleDbException:查询表达式'[CompanyID] = @cmpID中的语法错误(缺少运算符),而不是(从EmployeeDetails中选择[CompanyID],其中[CompanyID] = @cmpID)'。

请解决它

1 个答案:

答案 0 :(得分:10)

如果您阅读错误,它会告诉您问题:

OleDbCommand upcmd = new OleDbCommand(
"delete from CompanyDetails where [CompanyID] not in " + 
"(Select [CompanyID] from EmployeeDetails where [CompanyID] = @cmpID)", conn);

您最后有一个(但没有匹配)