0

我创建了一个查询以在 c# 中一次插入两个 ms 访问表。我得到了例外

{System.Data.OleDb.OleDbException:在 SQL 语句结束后发现字符。在 System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr) 在 System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) 在 System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) 在 System.Data。 OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) at System.Data.OleDb.OleDbCommand.ExecuteNonQuery() at CompanyDetails.Model.CompanyDetailsModel.setCompanyDetailsToDB( E:\Project\PBAttendence\ModifyPrivilage\CompanyDetails\Model\CompanyDetailsModel.cs 中的 CompanyDetailsDataList _cmpDetailsList):

我的示例代码如下,请解决我的问题。对不起,我的英语不好。

int companyID = _cmpDetailsList[0].CompanyID;
                    string companyName = _cmpDetailsList[0].CompanyName;
                    string contactID = _cmpDetailsList[0].ContactID;
                    string companyAddress = _cmpDetailsList[0].CompanyAddress;

                    if (companyID == -1)
                    {
                        OleDbCommand cmd = new OleDbCommand("Insert into CompanyDetails([CompanyName],[CompanyAddress],[ContactID]) values ('" + companyName + "','" + companyAddress + "','" + contactID + "');Insert into UserCompanyDetails([UserID],[CompanyID]) values (" + "Select [UserID] from UserDetails;" + "," + "Select @@identity;" + "); ", conn);
                        conn.Open();
                        cmd.ExecuteNonQuery();
                        conn.Close();
                    }
                    else
                    {
                        OleDbCommand upcmd = new OleDbCommand("update CompanyDetails set [CompanyName] = '" + companyName + "',[CompanyAddress] = '" + companyAddress + "',[ContactID] = '" + contactID + "' where [CompanyID] = @cmpID;", conn);
                        conn.Open();
                        upcmd.Parameters.AddWithValue("@cmpID", companyID);
                        upcmd.ExecuteNonQuery();
                        conn.Close();
                    }

现在我分成两个插入命令,但我收到错误 {System.Data.OleDb.OleDbException: 语法错误。在查询表达式'从 UserDetails 中选择 [UserID];

OleDbCommand cmd = new OleDbCommand("Insert into CompanyDetails([CompanyName],[CompanyAddress],[ContactID]) values ('" + companyName + "','" + companyAddress + "','" + contactID + "');", conn);
                        conn.Open();
                        cmd.ExecuteNonQuery();
                        conn.Close();
                        OleDbCommand cmd1 = new OleDbCommand("Insert into UserCompanyDetails([UserID],[CompanyID]) values (" + "Select [UserID] from UserDetails;" + "," + "Select @@identity" + ");", conn);
                        conn.Open();
                        cmd1.ExecuteNonQuery();
                        conn.Close();
4

3 回答 3

2

问题是这行代码:

OleDbCommand cmd = new OleDbCommand("Insert into CompanyDetails([CompanyName],[CompanyAddress],[ContactID]) values ('" + companyName + "','" + companyAddress + "','" + contactID + "');Insert into UserCompanyDetails([UserID],[CompanyID]) values (" + "Select [UserID] from UserDetails;" + "," + "Select @@identity;" + "); ", conn);

你有两个insert相同的语句OleDbCommand。尝试将其分为两个不同的步骤:

  1. 插入 CompanyDetails 表
  2. 插入 UserCompanyDetails 表

希望这可以帮助你

于 2013-06-06T06:15:38.453 回答
0

首先,使用 raw sql 命令会比生成 sql 的代码更容易。
您可能会考虑创建一个存储过程,因为您的命令变得有点复杂
如果我是正确的,那么您目前正在尝试做的是:

Insert into table1(x,y,z) values a,b,c;
Insert into table2(x,y) values select * from table3; , @@identity

第二个 sql 命令在语法和逻辑上都无效,您的 @@identity 不会是静态的,因为您在命令期间插入了新记录。
我的建议是做这样的事情:

Insert into table1(x,y,z) values a,b,c;
declare @table1Id int = @@identity
Insert into table2(x,y) select colA, @table1Id from table3;
于 2013-06-06T06:22:24.220 回答
0

你不能有;在 Access 中的查询中。请参阅http://office.microsoft.com/en-us/access-help/HV080760224.aspx您必须按照@juanreyesv 的建议分别进行两次插入

你将不得不做3个查询,

  1. 使用您的 sql 进行插入:"Insert into CompanyDetails([CompanyName],[CompanyAddress],[ContactID]) values ('" + companyName + "','" + companyAddress + "','" + contactID + "')
  2. 使用获取@@identity Select @@identity并将其存储在变量中idnt
  3. 使用2.中得到的identity值进行第三次插入: "Insert into UserCompanyDetails([UserID],[CompanyID]) Select UserID, " + idnt.ToString() + " from UserDetails"

参考http://msdn.microsoft.com/en-us/library/ks9f57t0%28VS.71%29.aspx

于 2013-06-06T06:20:03.760 回答