Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As @marc_s said using <strong>Database managed keys</strong> is more viable. But in cases there is no much load on the database, for example because there are few users who work simultanously, I will use another easier method. That's I get the last id, I try to add new record, and if I encountered error for duplicate, I will try again. I limited this to 3 trials for my application and there's a 300 ms timeout between each trial. <em>Dont forget that this approach has <strong>serious limitations</strong></em>. In my application, there are very <em>few users</em>, the work load is <em>very low</em>, and the connection is a <em>local</em> one so this will do job well. Perhaps in other applications you need to adjust the delay, and in some cases, the approach <strong>might completely fail</strong>. Here's the code, I have two tables, <code>Invoices</code> and <code>Invoices_Items</code> the column which relates them is <code>invoice_id</code>:</p> <pre><code> byte attempts = 0; tryagain: //Find last invoice no OleDbCommand command = new OleDbCommand("SELECT MAX(invoice_id) FROM Invoices" , myconnection); int last_invoice_id = 0; try { last_invoice_id = (int)command.ExecuteScalar(); } catch (InvalidCastException) { }; // text_invoice_number.Text = Convert.ToString(last_invoice_id + 1); try { command = new OleDbCommand(@"INSERT INTO Invoices (invoice_id,patient_id,visit_id,issue_date,invoice_to,doctor_id,assistant_id) VALUES(?,?,?,?,?,?,?)",myconnection); // We use last_invoice_id+1 as primary key command.Parameters.AddWithValue("@invoice_id",last_invoice_id+1); // I will add other parameters here (with the exact order in query) command.ExecuteNonQuery(); } catch (Exception ex){ attempts++; if (attempts &lt;= 3) // 3 attempts { System.Threading.Thread.Sleep(300); // 300 ms second delay goto tryagain; } else { MessageBox.Show("Can not add invoice to database, " + ex.Message, "Unexpected error!" , MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } for (int i = 0; i &lt;= listInvoiceItems.Count-1; i++) { command = new OleDbCommand(@"INSERT INTO Invoices_Items (invoice_id,quantity,product,price,amount,item_type) VALUES(?,?,?,?,?,?)",myconnection); // Now we use our stored last_invoice_id+1 as foreign key command.Parameters.AddWithValue("@invoice_id",last_invoice_id+1); // Add other Invoice Items parameters here (with the exact order in query) command.ExecuteNonQuery(); } </code></pre>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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