数据表ImportRow数据表、ImportRow

2023-09-06 06:14:03 作者:一根有原则的黄瓜

DataSet dsUdpated = new DataSet()
DataTable dtUpdated = dsUpdated.Tables.Add();
dtUpdated = dsOriginal.Tables[0].Clone();   // Guarenteed ds.Tables[0] has rows
dsUpdate.Tables[0].ImportRow(row); // row is one of the filtered row in 

但ImportRow似乎没有添加的行! dsUpdate.Tables [0]犯规包含的行。但行添加。请帮帮忙!

But the ImportRow doesnt seem to add the rows!. dsUpdate.Tables[0] doesnt contain the row. But a row is added. Please help!

推荐答案

下面是你在做什么错了。

Here's what you're doing wrong.

DataTable dtUpdated = dsUpdated.Tables.Add();
dtUpdated = dsOriginal.Tables[0].Clone();

在第一行,你可以声明你的对象,并把它初始化到数据集的Tables集合的添加操作的结果。在第二行,要设置到别的东西完全!

In the first line, you are declaring your object and initializing it to the result of an Add operation on the DataSet's Tables collection. In the second line, you are setting to to something else entirely!

dtUpdated 可以后的第一个很好的参考内存中的同一对象为 dsUpdated.Tables [0] 线code,但变量本身不是一个常数引用的对象。事实上,在第二行,你把它设置成不同的对象,另一台的克隆()方法的结果。如果变量曾经指向同一个对象 dsUpdated.Tables [0] ,它没有了。它类似于说明

dtUpdated may very well refer to the same object in memory as dsUpdated.Tables[0] after the first line of code, but the variable itself is not a constant reference to that object. Indeed, in the second line, you have set it to a different object, the result of another table's Clone() method. If the variable ever pointed to the same object as dsUpdated.Tables[0], it does not anymore. It's similar to stating

int x = 5;
x = 6;

在第一线, x的值 5。在第二个,你已经取代了价值6。同样,你的表变量,值是一个参考。你把它换成一个参考另一个。有意义吗?

In the first line, the value of x is 5. In the second, you've replaced that value with 6. Similarly, with your table variable, the value is a reference. You've replaced one reference with another. Make sense?

克隆表第一的然后的那个表添加到数据集。现在 dsUpdated.Tables [0] 将包含克隆结构 dsOriginal.Tables [0] 键,您的导入应工作符合市场预期。

Clone the table first, then add that table to the dataset. Now dsUpdated.Tables[0] will contain the cloned structure of dsOriginal.Tables[0] and your import should work as expected.