How can I update rows in a table using OLEDB in C#?
-
I just don't get it why is it so complex to make a simple update... I want trough a procedure to modify all the rows in a table like so: suppose I have a table with colums a, b, and c. c is identified by a and b. The procedure has the parameters a,b and c. a,b,c Example: UpdateazaCantCont(1,3,5) must add 5 to the old value of c. So if one row in the database table values are 1,3,8 the new values must be 1,3,13. Please help me, because I'm in an death stop in my project! The procedure i've wrote gives me no error, but it doesnt do anything. Here's the code I have written: private void UpdateazaCantCont(string aVal, string bVal, int cVal) { DataSet mds = new DataSet(); OleDbDataAdapter mda = new OleDbDataAdapter("select * from table where a='" + aVal + "'and b='" + bVal + "'", sirConex); mda.Fill(mds,"continut_locatie"); DataTable t = mds.Tables[0]; DataRow[] r = t.Select("a='"+aVal+"b='"+ bVal + "'"); for (int i=0; i<r.length;> string sirUpdate = "Update table set c=c+'"+cVal+"'where a='" + aVal + " and b='" + bVal + "'"; OleDbConnection myConex = new OleDbConnection(sirConex); OleDbCommand cmd = new OleDbCommand(sirUpdate, myConex); cmd.CommandText = sirUpdate; mda.UpdateCommand = cmd; try { mda.Update(mds, "table"); } catch (Exception ex) { MessageBox.Show("Eroare: " + ex); } }
-
I just don't get it why is it so complex to make a simple update... I want trough a procedure to modify all the rows in a table like so: suppose I have a table with colums a, b, and c. c is identified by a and b. The procedure has the parameters a,b and c. a,b,c Example: UpdateazaCantCont(1,3,5) must add 5 to the old value of c. So if one row in the database table values are 1,3,8 the new values must be 1,3,13. Please help me, because I'm in an death stop in my project! The procedure i've wrote gives me no error, but it doesnt do anything. Here's the code I have written: private void UpdateazaCantCont(string aVal, string bVal, int cVal) { DataSet mds = new DataSet(); OleDbDataAdapter mda = new OleDbDataAdapter("select * from table where a='" + aVal + "'and b='" + bVal + "'", sirConex); mda.Fill(mds,"continut_locatie"); DataTable t = mds.Tables[0]; DataRow[] r = t.Select("a='"+aVal+"b='"+ bVal + "'"); for (int i=0; i<r.length;> string sirUpdate = "Update table set c=c+'"+cVal+"'where a='" + aVal + " and b='" + bVal + "'"; OleDbConnection myConex = new OleDbConnection(sirConex); OleDbCommand cmd = new OleDbCommand(sirUpdate, myConex); cmd.CommandText = sirUpdate; mda.UpdateCommand = cmd; try { mda.Update(mds, "table"); } catch (Exception ex) { MessageBox.Show("Eroare: " + ex); } }
First of all, because aVal and bVal are strings, it's possible I can erase your database. Do you know about SQL Injection ? Second, why use a dataset to get back a single value ? Third, NEVER assume that an array has any values in it, without checking. Fourth, what's the point of doing a select when you already have only those values in the table ? you seem to be some of the way along, although you could do all of this in a stored proc and with 3 lines of code on the C# side.
Christian Graus Driven to the arms of OSX by Vista.
-
First of all, because aVal and bVal are strings, it's possible I can erase your database. Do you know about SQL Injection ? Second, why use a dataset to get back a single value ? Third, NEVER assume that an array has any values in it, without checking. Fourth, what's the point of doing a select when you already have only those values in the table ? you seem to be some of the way along, although you could do all of this in a stored proc and with 3 lines of code on the C# side.
Christian Graus Driven to the arms of OSX by Vista.
-
http://www.codeproject.com/KB/cs/simplecodeasp.aspx [^] Probably more than 3 lines, given you need one line for each of your three params. but still, a lot more readable. Of course, you also need to write the proc...
Christian Graus Driven to the arms of OSX by Vista.