gpt4 book ai didi

c# - 如何从其他数据表创建具有列结构的新数据表?

转载 作者:可可西里 更新时间:2023-11-01 08:03:29 25 4
gpt4 key购买 nike

如标题 - 问题是:

如何从其他数据表创建具有列结构的新数据表?

我需要空 DataTable 才能在其中使用 .Rows.Add() 方法。

代码:

DataTable dtFirst = new DataTable();
dtFirst.Columns.Add("column1");
dtFirst.Columns.Add("column2");
dtFirst.Columns.Add("column3");

FillDataTableFirst(); // before I create second DataTable - dtFirst is filled

// here I need dtSecond DataTable with same column structure
// i cant just copy first data table, because it contains data

DataTable dtSecond = ???;

最佳答案

您正在寻找 DataTable.Clone() 方法(自框架版本 1.1 起可用)。

来自documentation :

Clone creates a new DataTable with the same structure as the original DataTable, but does not copy any data (the new DataTable will not contain any DataRows). To copy both the structure and data into a new DataTable, use Copy.

在你的例子中:

DataTable dtFirst = new DataTable();
dtFirst.Columns.Add("column1");
dtFirst.Columns.Add("column2");
dtFirst.Columns.Add("column3");

FillDataTableFirst(); // before I create second DataTable - dtFirst is filled

DataTable dtSecond = dtFirst.Clone();

关于c# - 如何从其他数据表创建具有列结构的新数据表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19009436/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com