我创建了一个查询,在c#中一次插入两个ms访问表。我得到了例外
{System.Data.OleDb.OleDbException:在SQL结束后找到的字符 声明。在 System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams,Object& executeResult)at System.Data.OleDb.OleDbCommand.ExecuteCommandText(对象&安培; executeResult)at System.Data.OleDb.OleDbCommand.ExecuteCommand(的CommandBehavior 行为,对象& executeResult)at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(的CommandBehavior 行为,String方法)at System.Data.OleDb.OleDbCommand.ExecuteNonQuery()at CompanyDetails.Model.CompanyDetailsModel.setCompanyDetailsToDB(CompanyDetailsDataList _cmpDetailsList中的E:\ Project \ PBAttendence \ ModifyPrivileage \ CompanyDetails \ Model \ CompanyDetailsModel.cs:line 62}
我的示例代码如下,请解决我的问题。抱歉我的英语不好。
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();
答案 0 :(得分: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 :(得分:0)
你不能拥有;在Access中的查询中。请参阅http://office.microsoft.com/en-us/access-help/HV080760224.aspx您必须按照@juanreyesv
的建议单独执行两个插入您必须执行3次查询,
"Insert into CompanyDetails([CompanyName],[CompanyAddress],[ContactID]) values ('" + companyName + "','" + companyAddress + "','" + contactID + "')
Select @@identity
并将其存储在变量idnt
"Insert into UserCompanyDetails([UserID],[CompanyID])
Select UserID, " + idnt.ToString() + " from UserDetails"
请参阅http://msdn.microsoft.com/en-us/library/ks9f57t0%28VS.71%29.aspx
答案 2 :(得分: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;