简体   繁体   中英

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#?

Source: a range of values from excel (interop) in an object[,] array.

Thanks.

You can create a dataset / datatable in code: http://msdn.microsoft.com/en-us/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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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