DataTable.ImportRow问题 [英] DataTable.ImportRow question

查看:119
本文介绍了DataTable.ImportRow问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对.NET DataTable.ImportRow方法有疑问。如果我将DataRow导入到一个空的DataTable中,那么空的DataTable会选择我试图放入DataRow的架构吗?例如,I
代码如下所示:


DataTable tableWithData;
DataTable emptyTable = new DataTable();结果,emptyTable.ImportRow(tableWithData.Rows [0]);


如果我调用tableWithData.Columns.Count,我得到5.
如果我在调用ImportRow之前调用emptyTable.Columns.Count,我得到0,
这是我对DataTable构造函数的期望,没有参数...
但是当我在调用ImportRow后调用emptyTable.Columns.Count时,我仍然得到0.
而且只是确保导入DataRow,在ImportRow语句获取后调用
emptyTable.Rows.Count 1.


有没有人知道我是否有某种方式可以通过编程方式导入DataTable的架构和数据吗?谢谢。


- Jay

解决方案

如果可能,尝试创建DataSet,添加然后克隆整个DataSet。


如果先执行此操作,则可以在尝试导入行之前克隆整个DataSet。


DataSet setWithTable;


DataTable tableWithData;


//填充tableWithData


// ...


setWithTable.Tables.Add( tableWithData);


DataSet emptyDataSet  = setWithTable.Clone();



emptyDataSet.Tables ["tableWithData"]。ImportRow(tableWithData.Rows [0]);


这篇文章可能会有所帮助:


http: //dotnetjunkies.com/WebLog/hussein/archive/2004/12/31/40152.aspx


Hi,

I have a question about the .NET DataTable.ImportRow method. If I
import a DataRow into an empty DataTable, will the empty DataTable pick
up the schema of the DataRow I'm trying to put into it? For example, I
have code that looks like this:

DataTable tableWithData;
DataTable emptyTable = new DataTable();
emptyTable.ImportRow(tableWithData.Rows[0]);

If I call tableWithData.Columns.Count, I get 5.
If I call emptyTable.Columns.Count before calling ImportRow, I get 0,
which is what I expect from the DataTable constructor with no
arguments...
But when I call emptyTable.Columns.Count after calling ImportRow, I
still get 0.
And just to make sure that a DataRow is being imported, calling
emptyTable.Rows.Count after the ImportRow statement gets me 1.

Does anybody know if there's some way that I can programmatically
import a DataTable's schema along with the data? Thanks.

--Jay

解决方案

If possible, try creating the DataSet, adding the table, then cloning the whole DataSet.

If you do this first, then you can clone the entire DataSet before attempting to import the rows.

DataSet setWithTable;

DataTable tableWithData;

//populate the tableWithData

//...

setWithTable.Tables.Add(tableWithData);

DataSet emptyDataSet = setWithTable.Clone();


emptyDataSet.Tables["tableWithData"].ImportRow(tableWithData.Rows[0]);

This article may be helpful:

http://dotnetjunkies.com/WebLog/hussein/archive/2004/12/31/40152.aspx


这篇关于DataTable.ImportRow问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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