The difference between Datatable.importrow () and DATATABLE.ROWS.ADD ()

Source: Internet
Author: User

When writing code today, use ImportRow () to add records to the DataTable, with the following code:

DataTable dt = DataTable;
DataRow dr = dt. NewRow ();
dr["filename"] = filename;
dr["DbName"] = DbName;
dt. ImportRow (DR);


However, after execution, it was found that the records in Dtappendix did not increase. But replace the red color code with DT. Rows.Add (DR); it's ready. After the internet to check the information to find out why. Here is a look at the use and difference between the two and its application.

1, first of all, we first say Datatable.newrow () method, this method can create and table with the same framework of the DataRow (and must use this method to create the same schema as the original table), and this new row is added to the original table. But I found no such empty line in this DataTable. Why? The reason is this: the original DataTable rows are a RowState attribute, there are several:

1,detached The row has been created but does not belong to any datarowcollection. A DataRow is in this state immediately after it has been created, before it is added to the collection, or after it has been removed from the collection.
2,unchanged The row has not changed since the last call to AcceptChanges.
3,added The row has been added to DataRowCollection, AcceptChanges has not yet been called.
4,deleted The row has been deleted through the DataRow's Delete method.
5,modified The row has been modified, AcceptChanges has not been called.

In fact NewRow () creates a line whose rowstate is detached, and it is obvious that the new row is not visible.


2, ImportRow ()
Copy the Daterow into the DataTable, preserving any property settings, as well as the initial value and the current value. However, when the RowState property of the DataRow is detached, it cannot be copied, so the red code above is not possible. Some people on the internet say that it is not possible to copy because the new line is subordinate to the original table, which I think is incorrect because of the properties of the line. In fact, if we change the properties of the rows, the ImportRow () method is still feasible.
ImportRow () is typically used to copy data from one table to another table. The instance code is as follows:

DataTable dtnew = dt. Clone ();
foreach (DataRow dr in Dt. Rows)
{
Dtnew.importrow (DR);
If you really want to use ImportRow (), you can add a blank line first, so the operation: datatable dt = DataTable;
DataRow dr = dt. NewRow ();
dr["filename"] = filename;
dr["DbName"] = Dbname;dt. Row.add ();
Dt. ImportRow (DR); This kind of operation, a little superfluous, not recommended to use!

The difference between Datatable.importrow () and DATATABLE.ROWS.ADD ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.