执行下列c#代码时出错

本文关键字:出错 代码 执行 | 更新日期: 2023-09-27 18:17:04

我曾尝试使用以下代码删除表中的一行,但我得到了一个错误。

{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。oledbexcexception:查询表达式'[CompanyID]= @cmpID '中语法错误(缺少操作符),而不在(Select [CompanyID] from EmployeeDetails where [CompanyID]= @cmpID)'.

请解决

执行下列c#代码时出错

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

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

你有一个(,但没有匹配的)结尾