问题
Which is recommended
while (reader.Read())
{
table.Rows.Add(
new object[] { reader[0], reader[1], reader[2], reader[3] }
);
table.AcceptChanges();
}
or
while (reader.Read())
{
table.Rows.Add(
new object[] { reader[0], reader[1], reader[2], reader[3] }
);
}
table.AcceptChanges();
Note where the table.AcceptChanges is placed.
EDIT 1
Here is the code block:
protected void Page_Load(object sender, EventArgs e)
{
IDataReader reader = cust.GetCustomerOrderSummary("99999");
using (DataSet ds = new DataSet())
{
using (DataTable table =
new DataTable { TableName = "OrderSummary" })
{
DataColumn idColumn = table.Columns.Add("number", typeof(int));
table.Columns.Add("name", typeof(string));
table.Columns.Add("quantity", typeof(int));
table.Columns.Add("prev_quantity", typeof(int));
table.PrimaryKey = new DataColumn[] { idColumn };
while (reader.Read())
{
table.Rows.Add(
new object[]{ reader[0], reader[1], reader[2], reader[3] }
);
table.AcceptChanges();
}
ds.Tables.Add(table);
rptCustomerOrder report =
new rptCustomerOrder { DataSource = ds };
ReportViewer1.Report = report;
}
}
}
EDIT 2
After reading the MSDN article here I decided to place the AcceptChanges() outside the loop based on the following statement (from the article):
Calling AcceptChanges at the DataTable level causes the AcceptChanges method for each DataRow to be called.
回答1:
Calling AcceptChanges
after adding new row will actually turn the DataRowState
of your newly added DataRow
from Added
to Unchanged
. If you go with it you might lose the tracking of your newly added rows and at the time of persistance. ADO.NET would not be able to identify the rows which needs to be inserted in the database. So choose this option wisely you might not even require it.
回答2:
Are you sure you need to be calling the AcceptChanges method at all? I've done similar and I've never had to use it.
回答3:
This really depends on what your table object is and what AcceptChanges is doing. In general I would say the secodn approach is more efficent if you can call AcceptChanges only once and have it do its work.
Again this is in general because I do not know what object you actually dealing with. But doing things inside loops is usually more expensive because you do it more often but your table object might require you to accept changes between each row in which case you have no choice.
回答4:
I'm assuming AcceptChanges is when you actually commit your data?
The first way is less efficient in this case than the second, but is safer since there is less chance of you losing data if your read is interrupted at some point.
回答5:
You are calling accept changes on the table but be advised you can call acceptchanges on every individual row thus only effecting that row.
dsDataset.Tables["OrderSummary"].Rows[currentrow].acceptchanges();
It really depends on what you are trying to do. You can update all at the end on the table or individually on each row.
Also keep in mind that calling table acceptchanges it will change the datarowstate to unchanged. Thus if you are trying to update a database (SQL,Access, ect...) then that could interfere with the database updates.
It appears from your example that you are only using this table for a report therefore not sure why you would need to update the datarowstate at all. Not sure the report cares. As far as I know the acceptchanges/datarowstate only concerns updating the backend.
来源:https://stackoverflow.com/questions/821507/should-acceptchanges-be-called-every-time-a-new-row-is-added