How to convert a 2 dimensional array into a data set using C#

How do you convert or create a dataset from a 2 dimensional array?  Thanks!
Who is Participating?
Fernando SotoRetiredCommented:
Hi  VBBRett;

The following code snippet shows how it can be converted into a DataSet object.

// Test 2D array of Objects so that different data types 
// can be in each element
Object[,] myData = new object[3, 3];
// Test Data placed in array
myData[0, 0] = 1;
myData[0, 1] = "Test Data 1";
myData[0, 2] = DateTime.Now;
myData[1, 0] = 2;
myData[1, 1] = "Test Data 2";
myData[1, 2] = DateTime.Now.AddDays(3);
myData[2, 0] = 3;
myData[2, 1] = "Test Data 3";
myData[2, 2] = DateTime.Now.AddDays(5);

// 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("Index", typeof(System.Int32));
// Add this column to the columns collection of the data table
dc = new DataColumn("Description", typeof(System.String));
dc = new DataColumn("Date", typeof(System.DateTime));

// Loop through the 2D array and fill columns and rows
// outer loop creates the rows and inner loop the columns
for (int i = 0; i < myData.GetLength(0); i++ )
    // This array holds the row data
    Object[] data = new Object[myData.GetLength(0)];
    for( int j = 0; j < myData.GetLength(0); j++)
        // fill the row data
        data[j] = myData[i, j]; 
    // Add the row to the DataTable

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

Open in new window

VBBRettAuthor Commented:
A little confusing with the added text.  Can you leave out the comments and just post the code?  Thanks!
Fernando SotoRetiredCommented:
You can copy the code from the post and past it into a text editor, then remove  all lines that starts with // and you will have what you want.
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Gustav BrockCIOCommented:
>  Can you leave out the comments and just post the code?  

Really? How lazy can one be?

Fernando is the good example adding useful comments as you didn't know how to do this.
If you really can't remove some comment lines in a snap, I would strongly recommend you to take some intro level courses in C#.

VBBRettAuthor Commented:
When I read through the code, it all made sense.  I'm not lazy, it was a late night and I had a hard time reading it all because of exhaustion.  Thank you very much Fernando!
Fernando SotoRetiredCommented:
Not a problem VBBRett, glad I was able to help.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.