【问题标题】:Inserting data into multiple access tables at once in C# (OLEDB)在 C# (OLEDB) 中一次将数据插入多个访问表
【发布时间】:2012-03-23 02:40:39
【问题描述】:

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

我的问题是,当我尝试运行 ExecuteNonQuery() 时,此 SQL 语法会引发错误

使用这个连接字符串:

"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.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().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 个代表点) http://i44.tinypic.com/2w49t84.png

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

更新 1

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

更新 2

dbc.GetCommand().Parameters.Clear() 解决的问题

【问题讨论】:

    标签: c#-4.0 oledb ms-access-2010 insert-into ms-access


    【解决方案1】:

    您不需要关闭连接,但您需要在子选择中插入左括号:

    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);");
    

    【讨论】:

    • 感谢指正。我发现问题访问不理解 SELECT 内部 VALUES。有没有办法可以检索最后插入的 ID?
    • 我使用了DMax(\"[ID]\", \"inventory\"),但它返回inventory 的所有字段并将其全部写入可以写入的字段中,在我的C# 代码中,但如果我在MS Access 上编写查询2010 它没有做它在 C# 代码上所做的事情。似乎是什么问题?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    相关资源
    最近更新 更多