Click here to Skip to main content
16,017,238 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Helloo,

I try to update ALL records from my database, i use the folowing code:

C#
try
                {
                    Program.Connection.CommandText = "SELECT * FROM NirProduseAmanet";
                    DataTable TableIntrareProduse = new DataTable();
                    Program.Connection.FillDataTable(TableIntrareProduse, true);

                    string[] ProduseIntare = new string[TableIntrareProduse.Rows.Count];
                    string[] GrProdusIesire = new string[TableIntrareProduse.Rows.Count];

                    for (int i = 0; i < TableIntrareProduse.Rows.Count; i++)
                    {
                        ProduseIntare[i] = TableIntrareProduse.Rows[i]["PretPropusVanzare"].ToString();
                        GrProdusIesire[i] = TableIntrareProduse.Rows[i]["Cantitatea"].ToString();

                        Program.Connection.CommandText = "UPDATE NirProduseAmanet SET PretVanzare=@PretVanzare, GrProdusIesire=@GrProdusIesire";
                        Program.Connection.AddParameter("@PretVanzare", Convert.ToDecimal(ProduseIntare[i]));
                        Program.Connection.AddParameter("@GrProdusIesire", Convert.ToDecimal(GrProdusIesire[i]));
                        Program.Connection.ExecuteNonQuery();
                    }
                }
                catch (OleDbException ex)
                {

                }



the code is ok, i retive al recod from database

BUT on UPDATE he update ALL records with my last value from database.
any sugestions?
Posted

Well...it will.
When you write an UPDATE you have to specify exactly which records should be affected - or SQL will assume that you mean all of them, just as it does when you do a SELECT:
SQL
SELECT * FROM MyTable
Will return all records.
SQL
UPDATE MyTable SET MyColumn=6
Will change all records.

If you want to limit the rows you return, then you use a WHERE clause:
SQL
SELECT * FROM MyTable WHERE MyColumn=6
Similarly, if you want to alter only specific records, you use a WHERE clause on your UPDATE:
SQL
UPDATE MyTable SET MyColumn=6 WHERE MyOtherColumn=1

I have no idea what column of your table is significant here, so I leave it up to you to decide which column determines the update criteria!
 
Share this answer
 
Comments
Maciej Los 28-Feb-14 3:36am    
5ed!
You forgot to add WHERE statement to limit/restrict the range of changes ;)

SQL
UPDATE TableName SET FieldName=NewValue 
WHERE FieldName = OldValue
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900