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!
VBBRettAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
dt.Columns.Add(dc);
dc = new DataColumn("Description", typeof(System.String));
dt.Columns.Add(dc);
dc = new DataColumn("Date", typeof(System.DateTime));
dt.Columns.Add(dc);

// 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
    dt.Rows.Add(data);
}

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

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
VBBRettAuthor Commented:
A little confusing with the added text.  Can you leave out the comments and just post the code?  Thanks!
0
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.
0
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

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#.

/gustav
0
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!
0
Fernando SotoRetiredCommented:
Not a problem VBBRett, glad I was able to help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.