简体   繁体   English

C#将二维数组转换为数据集/数据表

[英]C# Convert a 2-dimensional array into a dataset / datatable

does anyone know how to turn a 2-dimensional array into a dataset or datatable in c#? 有谁知道如何将二维数组转换为c#中的数据集或数据表?

Source: a range of values from excel (interop) in an object[,] array. 来源:object [,]数组中来自excel(互操作)的一系列值。

Thanks. 谢谢。

You can create a dataset / datatable in code: http://msdn.microsoft.com/en-us/library/system.data.datatable.aspx 您可以使用以下代码创建数据集/数据表: http : //msdn.microsoft.com/zh-cn/library/system.data.datatable.aspx

From there you would loop through your array and populate the rows and their columns with the array information. 从那里开始,您将遍历数组,并使用数组信息填充行及其列。

Option given by mr.phoenix should work. 凤凰先生给出的选项应该可以使用。 If you are stuck with dealing with arrays...here is some pseudocode. 如果您不愿意处理数组...这里有一些伪代码。

var sample = {{0, 1}, {2, 3}, {4, 5}, {6, 7}, {8, 9}};
var table = new DataTable("SampleTable");

// iteration logic/loops for the array
{
   var newRow = table.NewRow();
   newRow["Col1"] = sample[i,j0]; // like sample [0,0]
   newRow["Col2"] = sample[i,j1]; // like sample [0,1]
   table.Add(newRow);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM