D
davepkz
I know this is really basic but I am baffled trying to get the
DataAdapter Update to work.
Let's say I simply want to change all names "Smith" to "Jones" and I
want to use the DataAdapter and DataTable to do it.
So I try this:
// assume the OleDbConnection conn is already open
OleDbCommand cmd = new OleDbCommand("select lastname from employee",
conn);
OleDbDataAdapter data = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
data.Fill(ds, "mydata");
DataTable dt = ds.Tables[0];
DataRow[] rows = dt.Select();
for (int i = 0; i < rows.Length; i++)
{
DataRow row = rows;
string curVal = row["lastname"].ToString();
if (curVal == "Smith")
{
row["lastname"] = "Jones";
rows = row;
}
}
data.Update(rows);
This doesn't work because it "requires a valid UpdateCommand"...
But I don't follow this at all. Sure, I could explicitly write an
update command like so...
OleDbCommand command = new OleDbCommand("UPDATE employee SET lastname
= 'Jones' WHERE lastname = 'Smith'", conn);
data.UpdateCommand = command;
....but then what's the point of allowing me to edit the DataRow and
having an Update() command in the first place?
I know I can do this simply and more efficiently with the
connection's ExecuteNonQuery, but for reasons that I can go into if
necessary I really need an approach like this where I iterate through
each record and programmatically review it before updating it.
Thanks
Dave
DataAdapter Update to work.
Let's say I simply want to change all names "Smith" to "Jones" and I
want to use the DataAdapter and DataTable to do it.
So I try this:
// assume the OleDbConnection conn is already open
OleDbCommand cmd = new OleDbCommand("select lastname from employee",
conn);
OleDbDataAdapter data = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
data.Fill(ds, "mydata");
DataTable dt = ds.Tables[0];
DataRow[] rows = dt.Select();
for (int i = 0; i < rows.Length; i++)
{
DataRow row = rows;
string curVal = row["lastname"].ToString();
if (curVal == "Smith")
{
row["lastname"] = "Jones";
rows = row;
}
}
data.Update(rows);
This doesn't work because it "requires a valid UpdateCommand"...
But I don't follow this at all. Sure, I could explicitly write an
update command like so...
OleDbCommand command = new OleDbCommand("UPDATE employee SET lastname
= 'Jones' WHERE lastname = 'Smith'", conn);
data.UpdateCommand = command;
....but then what's the point of allowing me to edit the DataRow and
having an Update() command in the first place?
I know I can do this simply and more efficiently with the
connection's ExecuteNonQuery, but for reasons that I can go into if
necessary I really need an approach like this where I iterate through
each record and programmatically review it before updating it.
Thanks
Dave