【问题标题】:Convert a Object [,] array into a dataset / datatable in C#在 C# 中将 Object [,] 数组转换为数据集/数据表
【发布时间】:2015-10-29 15:00:40
【问题描述】:

C# 将多维 Object[,]array 转换为数据集/数据表

我有 Object[,] 我将它传递给函数,我需要构建 Datatable

这是我正在尝试的代码

 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;

}

如果有其他方法请帮助我

这里有错误请看一下 enter image description here


我尝试的另一种方法是

-`

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;
 }

`

【问题讨论】:

  • 你已经展示了你尝试过的东西,它有效吗?它有什么问题?
  • @tim 请看附图
  • 你能把数字放在 XML 中,还是必须把它们放在一个数组中?如果您可以将它们保存在 XML 中,那么就像调用 DataTable.ReadXml() 函数一样简单。
  • @RacilHilan .... 抱歉,我试过了,但它适用于单维而不适用于多维对象 [,] 数组
  • 为什么? XML适用于几乎任何类型的数据,包括非常容易的多维数据。试试&lt;col1&gt;value1&lt;/col1&gt;&lt;col2&gt;value2&lt;/col2&gt; 之类的东西。如果您愿意,我可以将其发布为答案

标签: c# arrays multidimensional-array datatable dataset


【解决方案1】:

这个怎么样:

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;
}

用法:

var table = ArraytoDatatable(new object[2, 3] {
    { 1, 2, 3 },
    { 4, 5, 6 },
});

【讨论】:

    【解决方案2】:

    可以使用Array.GetLength获取多维数组的维度长度:

    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();
    }
    

    输出:

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

    有了这个样本数据:

    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);
    

    【讨论】:

    • 使用此示例数据它工作正常,但如果我通过我的 Object[,] 不幸的是它不会显示在网格视图上,或者我们创建的 Datatable 无法查看它,但正如你为小数据显示的那样是的!不适用于大数据
    • @Ranju:你的阵列有多大?你确定它不只是大吗? DataTable 创建成功了吗?那么这可能是一个不同的问题,因为这个任务已经解决了。
    【解决方案3】:

    如果您想知道如何在 XML 中执行此操作,您的功能将非常简单:

    public static DataTable ArraytoDatatable(string numbers)
    {
        DataSet ds = new DataSet();
        ds.ReadXml(new StringReader(numbers));
        return ds.Tables[0];
    }
    

    下面是您如何编写 xml 并对其进行测试:

    string xml = "<?xml version=\"1.0\"?><numbers>"
        + "<row><col1>1</col1><col2>2</col2></row>"
        + "<row><col1>3</col1><col2>4</col2></row>"
        + "<row><col1>5</col1><col2>6</col2></row>"
        + "<row><col1>7</col1><col2>8</col2></row>"
        + "</numbers>";
    
    DataTable dt = ArraytoDatatable(xml);
    Console.WriteLine("DataTable (" + dt.Rows.Count + " rows in " + dt.Columns.Count + " Columns)");
    foreach (DataRow row in dt.Rows)
    {
        foreach (DataColumn column in dt.Columns)
        {
            Console.Write("{0} ", row[column]);
        }
    
        Console.WriteLine();
    }
    

    这是fiddle

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-17
      • 1970-01-01
      • 2021-05-16
      • 1970-01-01
      • 2012-02-17
      • 2017-08-05
      • 1970-01-01
      相关资源
      最近更新 更多