应的AcceptChanges()被调用每一个新的行被添加的时间? [英] Should AcceptChanges() be called every time a new row is added?

查看:161
本文介绍了应的AcceptChanges()被调用每一个新的行被添加的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是推荐

 而(reader.Read())
{
    table.Rows.Add(
            新的对象[] {读者[0],读者[1],读者[2],读者[3]}
    );
    table.AcceptChanges();
}
 

 而(reader.Read())
{
    table.Rows.Add(
            新的对象[] {读者[0],读者[1],读者[2],读者[3]}
    );
}
table.AcceptChanges();
 

请注意其中table.AcceptChanges放置。


修改1

下面是code座:

 保护无效的Page_Load(对象发件人,EventArgs的)
{
    IDataReader的读卡器= cust.GetCustomerOrderSummary(99999);
    使用(DataSet的DS =新的DataSet())
    {
        使用(数据表表=
                新的DataTable {表名=OrderSummary})
        {
            DataColumn的idColumn = table.Columns.Add(数字的typeof(INT));
            table.Columns.Add(名,typeof运算(字符串));
            table.Columns.Add(量的typeof(INT));
            table.Columns.Add(prev_quantity的typeof(INT));
            table.PrimaryKey =新的DataColumn [] {idColumn};
            而(reader.Read())
            {
                table.Rows.Add(
                  新的对象[] {读者[0],读者[1],读者[2],读者[3]}
                );
                table.AcceptChanges();
            }
            ds.Tables.Add(表);
            rptCustomerOrder报告=
                    新rptCustomerOrder {数据源= DS};
            ReportViewer1.Report =报告;
        }
    }
}
 


编辑2
阅读MSDN文章后这里我决定放置的AcceptChanges()基于下面的语句外循环(从文章):

  

在数据表级调用的AcceptChanges导致AcceptChanges方法对每个DataRow中被调用。

解决方案

调用的AcceptChanges 添加新的行会实际上变成后 DataRowState 新添加的的DataRow 添加不变。如果你去用它,你可能的失去了跟踪的你新添加的行和持久性的时间。的 ADO.NET 的将不能够识别需要被在数据库中插入的行。因此,选择此选项明智的,你甚至可能不会需要它。

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.

解决方案

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.

这篇关于应的AcceptChanges()被调用每一个新的行被添加的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆