1

我曾尝试使用以下代码删除表中的一行,但出现错误。

{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)”。

请解决

4

1 回答 1

10

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

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

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

于 2013-06-06T07:11:11.677 回答