How to replace a DataBase Table by a DataTable
I am trying to replace a Table (FooTable) in my DataSource (an SQL CE Database) by a DataTable
(which bind to a DataGrid
). The reason I want to do this is: after I populate from a Database to a DataSet
(which show all the rows on the DataGrid
), I might edit, delete, and add multiple rows to the DataTable
. Instead of updating my changes to DataSource (SQL CE
) each time I modified the DataTable, I want to do it collectively at the end of the session.
My approach is straight-forward:
- `DELETE` all data from my DataSource table (FooTable) (I'm using **SQL CE**, so `TRUNCATE` is not available)
- INSERT the `DataTable's` data in to the emptied DataSource Table
Following is my C# code
/* Truncate the DataSource Table */
SqlCeCommand delCmd = new SqlCeCommand("DELETE FROM FooTable", conn)
conn.Open();
delCmd.ExecuteNonQuery();
conn.Close();
/* Insert DataTable into an empty DataSource Table */
string ins = @"INSERT INTO FooTable (FooName) values (@fooName)";
SqlCeCommand cmd = new SqlCeCommand(ins, conn);
da.InsertCommand = cmd;
da.Update(ds, "FooTable");
The codes work if I add or delete rows on the DataTable
, but when I edit a specific row on the DataTable
and Insert the table to my DataSource, I get the following error
"Update requires a valid UpdateCommand when passed DataRow collection with modified rows."
I do not understand why I get this error. I have already empty all rows in my DataSource table and my DataSource shouldn't not know there are modified rows but insert all DataTable Rows as new rows.
Or, is there a simple way to "REPLACE" a DataSource table by a DataTable?
[EDITED]
I tried setting the RowState manually like below code
foreach (DataRow row in dt.Rows)
{
row.RowState = DataRowState.Added;
}
but RowState is only read only and cannot be written.
[EDITED - 2nd] I tried setting it using SetAdded()
foreach (DataRow row in dt.Rows)
{
row.SetAdded();
}
Then I get the following error: "SetAdded and SetModified can only be called on DataRows with Unchanged DataRowState."
Still not manage to get it to work...
[EDITED - 3rd] So finally get it to work with the following code:
using (SqlCeCommand insCmd = new SqlCeCommand(@"INSERT INTO FooTable (FooName) VALUES (@fooName)", conn))
{
insCmd.Parameters.Add("@fooName", SqlDbType.NVarChar,开发者_开发百科 10, "FooName");
dt.AcceptChanges();
foreach (DataRow row in dt.Rows)
{
row.SetAdded();
}
da.InsertCommand = insCmd;
da.Update(ds, "FooTable");
}
The DataTable
tracks RowState
s internally.
When you modify an existing row, its RowState
becomes Modified
, so the DataAdapter
tries to run an UPDATE
command.
You need to call the SetAdded()
method on the modified rows to set their RowState
s to Added
rather than Modified
.
精彩评论