2015-10-29 25 views
0

C#轉換一多維對象[,]陣列分成數據集/數據表轉換一個對象[,]陣列分成數據集/ C#中的數據表

我有對象[,]和我傳遞,爲了功能和我需要建立數據表

這裏是我的代碼,我想這樣

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

`

+0

你已經證明你已經嘗試過,它的工作?它有什麼問題? –

+0

@tim請參閱附圖 – Ranju

+0

您可以使用XML格式的數字,還是必須將它們放在數組中?如果你可以使用XML,它就像調用DataTable.ReadXml()函數一樣簡單。 –

回答

0

如何:

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 }, 
}); 
0

可以使用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); 
+0

與此示例數據其工作正常,但如果我通過我的對象[,]不幸的是它不顯示在網格視圖或我們創建的Datatable無法查看它,但正如你顯示的小數據是!不適用於大數據 – Ranju

+0

@Ranju:你的陣列有多大?你確定這不是簡單的大嗎? DataTable創建成功了嗎?那麼這個任務就解決了,這可能是一個不同的問題。 –

0

如果你想知道如何做到這一點的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