البرمجة

تحويل DataTable إلى مصفوفة في C#

To convert a DataTable into a 2D array in C#, you can iterate over the rows of the DataTable and populate a 2D array with the values. Here’s an example:

csharp
using System; using System.Data; class Program { static void Main() { DataTable dt = new DataTable(); dt.Columns.Add("code", typeof(long)); dt.Columns.Add("Price", typeof(int)); // Add rows to the DataTable dt.Rows.Add(1146441600000, 34); dt.Rows.Add(1146528000000, 5); dt.Rows.Add(1146614400000, 10); dt.Rows.Add(1146700800000, 7); dt.Rows.Add(1146787200000, 12); dt.Rows.Add(1147046400000, 8); dt.Rows.Add(1147132800000, 9); // Convert DataTable to 2D array object[,] array = new object[dt.Rows.Count, dt.Columns.Count]; for (int i = 0; i < dt.Rows.Count; i++) { for (int j = 0; j < dt.Columns.Count; j++) { array[i, j] = dt.Rows[i][j]; } } // Print the 2D array for (int i = 0; i < array.GetLength(0); i++) { for (int j = 0; j < array.GetLength(1); j++) { Console.Write(array[i, j] + " "); } Console.WriteLine(); } } }

This code creates a DataTable with two columns (code and Price), adds rows to it, and then converts the DataTable into a 2D array. Finally, it prints the 2D array to the console.

المزيد من المعلومات

لتحويل DataTable إلى مصفوفة ثنائية الأبعاد في C#، يمكنك استخدام الكود التالي:

csharp
using System; using System.Data; class Program { static void Main() { // عيّن DataTable جديدة وأضف عمودين لها DataTable dt = new DataTable(); dt.Columns.Add("code", typeof(long)); dt.Columns.Add("Price", typeof(int)); // أضف صفوف إلى DataTable dt.Rows.Add(1146441600000, 34); dt.Rows.Add(1146528000000, 5); dt.Rows.Add(1146614400000, 10); dt.Rows.Add(1146700800000, 7); dt.Rows.Add(1146787200000, 12); dt.Rows.Add(1147046400000, 8); dt.Rows.Add(1147132800000, 9); // قم بتحويل DataTable إلى مصفوفة ثنائية الأبعاد object[,] array = new object[dt.Rows.Count, dt.Columns.Count]; for (int i = 0; i < dt.Rows.Count; i++) { for (int j = 0; j < dt.Columns.Count; j++) { array[i, j] = dt.Rows[i][j]; } } // قم بطباعة المصفوفة ثنائية الأبعاد for (int i = 0; i < array.GetLength(0); i++) { for (int j = 0; j < array.GetLength(1); j++) { Console.Write(array[i, j] + " "); } Console.WriteLine(); } } }

هذا الكود ينشئ DataTable بعمودين (code و Price)، يضيف صفوف إليها، ثم يقوم بتحويلها إلى مصفوفة ثنائية الأبعاد ويطبعها على الشاشة.

زر الذهاب إلى الأعلى