[英]ADO.NET get column header
I am using ADO.NET. 我正在使用ADO.NET。
I have 2 DataTables: 我有2个数据表:
1) dt1
2) dt2
What I like to do is to copy just the header columns from dt1 and populate dt2 with it. 我想做的只是从dt1复制标题列,并用它填充dt2。 Again I just need the column header.
同样,我只需要列标题。
What is the best approach to do this? 最好的方法是什么?
You need to Clone
the DataTable. 您需要
Clone
数据表。 Use DataTable.Clone
method. 使用
DataTable.Clone
方法。
DataTable dt = dt1.Clone();
If you just want to copy the column names and create columns with same name in the other DataTable you can do: 如果只想复制列名并在另一个DataTable中创建具有相同名称的列,则可以执行以下操作:
foreach (DataColumn dc in dt1.Columns)
{
dt2.Columns.Add(dc.ColumnName);
}
If you just need column names then you can get a List<string>
instead of a new DataTable. 如果只需要列名,则可以获取
List<string>
而不是新的DataTable。 Like: 喜欢:
List<string> columnNames = dt1.Columns
.Cast<DataColumn>()
.Select(r => r.ColumnName)
.ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.