Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a Object [,] array into a dataset / datatable in C#

C# Convert a Multi dimensional Object[,]array into a dataset / datatable

I have Object[,] and I am passing that to function and I need to build Datatable

Here is my code I am trying this way

 public static DataTable ArraytoDatatable(Object[,] numbers)
{                 
    DataTable dt = new DataTable();

Console.WriteLine(numbers.Rank);
Console.WriteLine(numbers.Length);

for (int dimension = 0; dimension < numbers.Rank; dimension++)
{
    dt.Columns.Add("Column"+(dimension+1));
}

Console.WriteLine("Array");
for (int element = 0; element < (numbers.Length / numbers.Rank); element++)
{
    DataRow row = dt.NewRow();
    for (int dimension = 0; dimension < numbers.Rank; dimension++)
    {
        Console.Write("{0} ", numbers[element,dimension]);                    
        row["Column" + (dimension + 1)] = numbers[element, dimension];
    }
    dt.Rows.Add(row);
    Console.WriteLine();
}

Console.WriteLine("DataTable");
foreach (DataRow row in dt.Rows)
{
    foreach (DataColumn column in dt.Columns)
    {
        Console.Write("{0} ", row[column]);
    }
    Console.WriteLine();
}

return dt;

}

Please help me if there is any other kind of approach

Error is here please take a look enter image description here


Another approach I tried is

-`

public DataSet ToDataSet(Object[,] myData)
     {
     DataSet ds = new DataSet();

     // Test 2D array of Objects so that different data types 
     // can be in each element


     // Create a DataTable object. Each row in the table is one
     // row in the array
     DataTable dt = new DataTable();
     // Create DataColumns for each column in the row
     // each column is a element in the array param 1 is the name
     // of the column and param 2 is its data type
     DataColumn dc = new DataColumn("block", typeof(System.String));
     // Add this column to the columns collection of the data table
     dt.Columns.Add(dc);
     dc = new DataColumn("mode", typeof(System.String));
     dt.Columns.Add(dc);


     dt.Columns.Add(dc);










     for (var i = 0; i < myData.GetLength(0); i++)
         for (var j = 0; j < myData.GetLength(1); j++)
             dt.Rows[i][j] = myData[i, j];


     // Add the row to the DataTable
     // dt.Rows.Add(data);


     // If you need to add the DataTable to a DataSet
     // then execute the next two lines
     DataSet ds1 = new DataSet();
     ds.Tables.Add(dt);



     return ds1;
 }

`

like image 290
Ranju Avatar asked Nov 28 '25 14:11

Ranju


2 Answers

How about this:

public static DataTable ArraytoDatatable(Object[,] numbers)
{                 
    DataTable dt = new DataTable();
    for (int i = 0; i < numbers.GetLength(1); i++)
    {
        dt.Columns.Add("Column" + (i + 1));
    }

    for (var i = 0; i < numbers.GetLength(0); ++i)
    {
        DataRow row = dt.NewRow();
        for (var j = 0; j < numbers.GetLength(1); ++j)
        {
            row[j] = numbers[i, j];
        }
        dt.Rows.Add(row);
    }
    return dt;
}

Usage:

var table = ArraytoDatatable(new object[2, 3] {
    { 1, 2, 3 },
    { 4, 5, 6 },
});
like image 110
Kvam Avatar answered Nov 30 '25 05:11

Kvam


You can use Array.GetLength to get the length of the dimensions of a multi dimensional array:

int width = numbers.GetLength(0);
int height = numbers.GetLength(1);

for (int w = 0; w < width; w++)
{
    dt.Columns.Add("Column" + (w + 1));
}

for (int h = 0; h < height; h++)
{
    DataRow row = dt.NewRow();
    for (int w = 0; w < width; w++)
    {
        Console.Write("{0} ", numbers[w, h]);
        row["Column" + (w + 1)] = numbers[w, h];
    }
    dt.Rows.Add(row);
    Console.WriteLine();
}

Output:

Cell 0|0 Cell 1|0 
Cell 0|1 Cell 1|1 
Cell 0|2 Cell 1|2 

With this sample data:

object[,] objects = new object[2, 3];
int width = objects.GetLength(0);
int height = objects.GetLength(1);
for (int w = 0; w < width; w++)
    for (int h = 0; h < height; h++)
        objects[w, h] = string.Format("Cell {0}|{1}", w, h);
like image 24
Tim Schmelter Avatar answered Nov 30 '25 03:11

Tim Schmelter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!