Note that there are some explanatory texts on larger screens.

plurals
  1. POC# list items to database SQL
    primarykey
    data
    text
    <p>Hello I have a simple question that regards inserting data into a MS MySql Database 2012 table. The table that I have is called COMPLETED and has 3 fields. </p> <ul> <li>student_ID (int, NOT allowed nulls)</li> <li>completed (bool, NOT allowed nulls)</li> <li>random_code (string, allowed nulls)</li> </ul> <p>In c# I have a list filled with unique random codes. I want all codes inserted into the database, so if I have 20 records I want 20 unique codes inserted into the random_code field. So the first records gets the first code, the seconds records gets the second code and so on. I think the best way to do this is using a foreach and, for each code in the list of codes insert that code into the random_code field in my database. The problem is I don't know how to do this. I have the following code that give's me an error at VALUE:</p> <p>Incorrect syntax near 'VALUE'.</p> <pre><code> foreach (string unRaCo in codes) { //insert database SqlCommand toDB = new SqlCommand("INSERT INTO COMPLETED (random_code) VALUE ( '"+ unRaCo +"' ) ", conn); SqlDataReader toDBR; toDBR = toDB.ExecuteReader(); } </code></pre> <p>Could anyone give me a dircetion here? Thanks in advance.</p> <p><strong>EDIT:</strong></p> <p>Okay I totally changed my query as I figured out it did not yet do what I wanted it to do. I now want to update my records instead of inserting records. I did that with the following code:</p> <pre><code> foreach (string unRaCo in codes) { //insert database SqlCommand naarDB = new SqlCommand("UPDATE VOLTOOID SET random_code = '"+ unRaCo +"' ", connectie); SqlDataReader naarDBR; naarDBR = naarDB.ExecuteReader(); naarDBR.Close(); } </code></pre> <p>The problem this time is that the update query updates ALL records with the first code, so the first record has the code 12345 for example but all other records also have that code. I want to update 12345 into record 1 and 54321 for example in number 2, how do I do that?</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.
 

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