C# 4.0 在C#(OLEDB)中一次将数据插入多址表

C# 4.0 在C#(OLEDB)中一次将数据插入多址表,c#-4.0,oledb,ms-access-2010,insert-into,ms-access,C# 4.0,Oledb,Ms Access 2010,Insert Into,Ms Access,我创建了一个管理连接和命令的类,我将其命名为DbConfiguration,它使用单例模式(使用dbc作为对象名) 我的问题是,当我尝试运行ExecuteNonQuery() 使用此连接字符串: "Provider=Microsoft.ACE.OLEDB.12.0;dbms.accdb;Persist Security Info=False" 我首先执行了以下命令: dbc.SetCommand("INSERT INTO inventory ([CallNumber], [Title], [I

我创建了一个管理连接和命令的类,我将其命名为DbConfiguration,它使用单例模式(使用
dbc
作为对象名)

我的问题是,当我尝试运行
ExecuteNonQuery()

使用此连接字符串:

"Provider=Microsoft.ACE.OLEDB.12.0;dbms.accdb;Persist Security Info=False"
我首先执行了以下命令:

dbc.SetCommand("INSERT INTO inventory ([CallNumber], [Title], [ImageLocation]) VALUES (@CALLNUMBER, @TITLE, @IMAGELOC);");
dbc.GetCommand().Parameters.Add("@CALLNUMBER", System.Data.OleDb.OleDbType.VarChar, 255);
dbc.GetCommand().Parameters.Add("@TITLE", System.Data.OleDb.OleDbType.VarChar, 255);
dbc.GetCommand().Parameters.Add("@IMAGELOC", System.Data.OleDb.OleDbType.VarChar, 255);

dbc.GetCommand().Parameters["@CALLNUMBER"].Value = txtCallNumber.Text;
dbc.GetCommand().Parameters["@TITLE"].Value = txtTitle.Text;
dbc.GetCommand().Parameters["@IMAGELOC"].Value = (!moveto.Equals("db_storage\\") ? moveto : "db_storage\\no_img.png");
然后是:

dbc.GetCommand().ExecuteNonQuery();
dbc.GetCommand().ExecuteNonQuery();
接下来是我执行的代码:

dbc.SetCommand("INSERT INTO publishing_info ([ID], [Author], [DateTime], [Publisher], [Pages], [ISBN_NO]) " +
               "VALUES (" +
               "SELECT([ID] FROM inventory " +
               "WHERE inventory.CallNumber=@CALLNUMBER AND inventory.Title=@TITLE AND inventory.ImageLocation=@IMAGELOC), " +
               "@AUTHOR, @DATETIME, @PUBLISHER, @PAGES, @ISBN);");
其次是:

dbc.GetCommand().ExecuteNonQuery();
dbc.GetCommand().ExecuteNonQuery();
以下是我用于参考的参数:

dbc.GetCommand().Parameters.Add("@AUTHOR", System.Data.OleDb.OleDbType.VarChar, 255);
dbc.GetCommand().Parameters.Add("@DATETIME", System.Data.OleDb.OleDbType.VarChar, 255);
dbc.GetCommand().Parameters.Add("@PUBLISHER", System.Data.OleDb.OleDbType.VarChar, 255);
dbc.GetCommand().Parameters.Add("@PAGES", System.Data.OleDb.OleDbType.UnsignedInt, 4);
dbc.GetCommand().Parameters.Add("@ISBN", System.Data.OleDb.OleDbType.VarChar, 255);

dbc.GetCommand().Parameters["@AUTHOR"].Value = txtAuthor.Text;
dbc.GetCommand().Parameters["@DATETIME"].Value = txtYear.Text;
dbc.GetCommand().Parameters["@PUBLISHER"].Value = txtPublisher.Text;
dbc.GetCommand().Parameters["@PAGES"].Value = uint.Parse(txtPages.Text);
dbc.GetCommand().Parameters["@ISBN"].Value = txtISBN.Text;
这是我的堆栈跟踪的屏幕截图(我不能附加它,因为我只有1个重复点)

我应该怎么做才能使语法起作用? 在执行另一个查询之前,我是否需要先关闭连接

更新1

我使用
DMax(\“[ID]\”,“inventory\”)
检索最后一个
ID
,但它返回
inventory
的所有字段,并将其全部写入可以写入的字段,在我的C代码中,但如果我在MS Access 2010中写入查询,它不会像在C代码中那样执行。有什么问题吗

更新2


使用
dbc.GetCommand().Parameters.Clear()
解决了问题您不需要关闭连接,但需要在子选择中插入左括号:

dbc.SetCommand("INSERT INTO publishing_info ([ID], [Author], [DateTime], [Publisher], [Pages], [ISBN_NO]) " +
           "VALUES (" +
           "(SELECT([ID] FROM inventory " +
           "WHERE inventory.CallNumber=@CALLNUMBER AND inventory.Title=@TITLE AND inventory.ImageLocation=@IMAGELOC), " +
           "@AUTHOR, @DATETIME, @PUBLISHER, @PAGES, @ISBN);");

谢谢你的更正。我发现问题访问不理解
选择
内部
。有什么方法可以检索最后插入的ID吗?我使用了
DMax(\“[ID]\”,\“inventory\”)
,但它返回
inventory
的所有字段,并将其全部写入可以写入的字段中,在我的C代码中,但如果我在MS Access 2010中写入查询,它不会像在C代码中那样执行。有什么问题吗?