J
Jon Skeet [C# MVP]
I've got a pretty simple situation: a very simple table with an ID
(which has been marked as the primary key) in SQL Server.
I then have a SqlDataConnection to the appropriate database, and a
simple select statement: select * from Articles
(I know selecting * is generally bad practice. This is just a test
database where I often muck around with the columns. If using select *
is relevant to why I'm not seeing the behaviour I'd expected, please
let me know!)
I create a new DataSet, and a DataTable within it called Articles. I
pre-create this so that I can add handlers for row
changing/changed/deleted/deleting and column changing/changed. I set
the missing schema action for the adapter to be AddWithKey. I then call
DataAdapter.Fill, passing the DataTable as the parameter.
Lo and behold, I get the appropriate data in the table, with a series
of Add and then Commit operations.
Now, in the background (after the Fill has completed) I remove one row
from the database, and change another one (not the key column). I then
call Fill again.
I had expected to see one row being deleted and the other being
changed, amd that's all - but instead, I see *all* the existing (in the
database) rows being changed (whether or not the data has changed) and
no deletions - the now obsolete row still remains in my data table.
I see this behaviour whether or not I call DataTable.AcceptChanges.
Am I doing something fundamentally wrong? The bulk of the code is:
SqlConnection conn = new SqlConnection
("Persist Security Info=False;"+
"Integrated Security=SSPI;"+
"database=News;server=treebeard");
SqlDataAdapter sda = new SqlDataAdapter
("select * from Articles", conn);
sda.MissingSchemaAction = MissingSchemaAction.AddWithKey;
DataSet ds = new DataSet();
DataTable articles = new DataTable("Articles");
ds.Tables.Add(articles);
articles.ColumnChanged+=new DataColumnChangeEventHandler
(ColumnChanged);
articles.ColumnChanging+=new DataColumnChangeEventHandler
(ColumnChanging);
articles.RowChanged+=new DataRowChangeEventHandler(RowChanged);
articles.RowChanging+=new DataRowChangeEventHandler(RowChanging);
articles.RowDeleted+=new DataRowChangeEventHandler(RowDeleted);
articles.RowDeleting+=new DataRowChangeEventHandler(RowDeleting);
while (true)
{
sda.Fill(articles);
Console.WriteLine ("Accepting changes");
ds.AcceptChanges();
Console.WriteLine ("Number of entries: {0}", articles.Rows.Count);
Console.WriteLine ("Change some data");
Console.ReadLine();
}
The handlers just print out some diagnostics.
(which has been marked as the primary key) in SQL Server.
I then have a SqlDataConnection to the appropriate database, and a
simple select statement: select * from Articles
(I know selecting * is generally bad practice. This is just a test
database where I often muck around with the columns. If using select *
is relevant to why I'm not seeing the behaviour I'd expected, please
let me know!)
I create a new DataSet, and a DataTable within it called Articles. I
pre-create this so that I can add handlers for row
changing/changed/deleted/deleting and column changing/changed. I set
the missing schema action for the adapter to be AddWithKey. I then call
DataAdapter.Fill, passing the DataTable as the parameter.
Lo and behold, I get the appropriate data in the table, with a series
of Add and then Commit operations.
Now, in the background (after the Fill has completed) I remove one row
from the database, and change another one (not the key column). I then
call Fill again.
I had expected to see one row being deleted and the other being
changed, amd that's all - but instead, I see *all* the existing (in the
database) rows being changed (whether or not the data has changed) and
no deletions - the now obsolete row still remains in my data table.
I see this behaviour whether or not I call DataTable.AcceptChanges.
Am I doing something fundamentally wrong? The bulk of the code is:
SqlConnection conn = new SqlConnection
("Persist Security Info=False;"+
"Integrated Security=SSPI;"+
"database=News;server=treebeard");
SqlDataAdapter sda = new SqlDataAdapter
("select * from Articles", conn);
sda.MissingSchemaAction = MissingSchemaAction.AddWithKey;
DataSet ds = new DataSet();
DataTable articles = new DataTable("Articles");
ds.Tables.Add(articles);
articles.ColumnChanged+=new DataColumnChangeEventHandler
(ColumnChanged);
articles.ColumnChanging+=new DataColumnChangeEventHandler
(ColumnChanging);
articles.RowChanged+=new DataRowChangeEventHandler(RowChanged);
articles.RowChanging+=new DataRowChangeEventHandler(RowChanging);
articles.RowDeleted+=new DataRowChangeEventHandler(RowDeleted);
articles.RowDeleting+=new DataRowChangeEventHandler(RowDeleting);
while (true)
{
sda.Fill(articles);
Console.WriteLine ("Accepting changes");
ds.AcceptChanges();
Console.WriteLine ("Number of entries: {0}", articles.Rows.Count);
Console.WriteLine ("Change some data");
Console.ReadLine();
}
The handlers just print out some diagnostics.