Note that there are some explanatory texts on larger screens.

plurals
  1. POInserting data into multiple access tables at once in C# (OLEDB)
    primarykey
    data
    text
    <p>I have created a class that will manage connection and commands, I named it <strong>DbConfiguration</strong> and it uses a Singleton pattern(using <code>dbc</code> as an object name).</p> <p>My problem is this SQL syntax throws an error when I try to run <code>ExecuteNonQuery()</code></p> <p>Using this connection string:</p> <pre><code>"Provider=Microsoft.ACE.OLEDB.12.0;dbms.accdb;Persist Security Info=False" </code></pre> <p>I executed this first:</p> <pre><code>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"); </code></pre> <p>and followed by:</p> <pre><code>dbc.GetCommand().ExecuteNonQuery(); </code></pre> <p>Next is I executed this code:</p> <pre><code>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);"); </code></pre> <p>and also followed by:</p> <pre><code>dbc.GetCommand().ExecuteNonQuery(); </code></pre> <p>Here are the parameters that I have used for your reference:</p> <pre><code>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; </code></pre> <p>Here is the screenshot of my stack trace(I can't attach it because I only have 1 rep points) <a href="http://i44.tinypic.com/2w49t84.png" rel="nofollow">http://i44.tinypic.com/2w49t84.png</a></p> <p>What should I do to make the syntax work? And also do I need to close the connection first before executing another query?</p> <p><em>Update 1</em></p> <p>I used <code>DMax(\"[ID]\", \"inventory\")</code> to retrieve the last <code>ID</code> but it returns all the fields of <code>inventory</code> and writes it all on the fields that it can be written to, in my C# code but if I write the query on MS Access 2010 it doesn't do what it does on a C# code. What seems to be the problem?</p> <p><em>Update 2</em></p> <p>Problem solved with <code>dbc.GetCommand().Parameters.Clear()</code></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload