Skip to content Skip to sidebar Skip to footer

C# List Items To Database SQL

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. student_ID (int, NOT a

Solution 1:

The correct is Values not Value, even if you only provide one column.

About your edit. First of all beware of SQL Injection. You better use SQLParameter class. Check Configuring Parameters and Parameter Data Types for further info.

If you want to update a specific id then use a where clause like (in plain SQL):

UPDATE VOLTOOID SET random_code = @NewValue WHERE random_code = @OldValue

Now if you just want to add the random number in a specific row, then you would have to use some more advanced SQL functions. Again in plain SQL you would have:

;WITH MyCTE AS
(
    SELECT random_code,
           ROW_NUMBER() OVER(ORDER BY random_code) AS ROWSEQ -- This will give a unique row number to each row of your table
    FROM   VOLTOOID _code
)
UPDATE MyCTE 
SET    random_code = @NewValue 
WHERE  ROWSEQ = @YourRandomRow

As the above queries are for SQL script execution you will need to define the variable used.


Solution 2:

Your syntax is wrong, you are using 'value' where you should use 'values'. If you have SSMS you will able to easily figure out this kind of errors.

Usually I create the query in SQL Server Management Studio query editor, then use it in C#. Most of the times I use SQL Server stored procedures where it's possible. Because I think it cost some extra resources to execute a text query than executing a procedure


Post a Comment for "C# List Items To Database SQL"