Rocshy
Rocshy

Reputation: 3509

String array to datatable

In my resources I have a string that looks like this: One,Two;Three,Four;Five,Six etc.

And I need that string to be converted to a DataTable that would look like this:

| Col 1 | Col 2 |

| One   | Two   |

| Three | Four  |

| Five  | Six   |

Is there an elegant(not having to write many lines of code) way to convert that string into a DataTable without having to use 2 splits and a for loop to accomplish it?

Edit I've found the following code: Array.ForEach(input, c => dataTable.Rows.Add()[0] = c); but from what I see it can only be used if you want the DataTable to have just one column.

Upvotes: 1

Views: 19563

Answers (4)

void
void

Reputation: 7880

if you don't want to split it two times with using for loop then one way is to extract the string into a 2D array and then:

DataTable table = new DataTable();
table.Columns.Add("col1", typeof(string));
table.Columns.Add("col2", typeof(string));

String val="One,Two;Three,Four;Five,Six";
string[][] values=val.Split(';').Select(x => x.Split(',')).ToArray();

for (int i= 0; i< values.Length; i++)
   {
       DataRow newRow = table.NewRow();
       for (int j= 0; j< 2; j++)
        {
           newRow[i] = values[i,j];
        }
       table.Rows.Add(newRow);
    }

Upvotes: 0

Habib
Habib

Reputation: 223207

You will need multiple split and a loop.


You can use DataTable.LoadDataRow method like:

string str = "One,Two;Three,Four;Five,Six";
DataTable dt = new DataTable();
dt.Columns.Add("Col1", typeof(string));
dt.Columns.Add("Col2", typeof(string));
str.Split(';').ForEach(r => dt.LoadDataRow(
                                r.Split(',').Select(t => (object)t).ToArray()
                                , LoadOption.OverwriteChanges));

That will populate DataTable with the string values as you want.

Now, if this is elegant enough, that depends!.

Upvotes: 0

mybirthname
mybirthname

Reputation: 18127

            DataTable tbl = new DataTable();
            tbl.Columns.Add("Col1");
            tbl.Columns.Add("Col2");

            string numbers ="One,Two;Three,Four;Five,Six";

            string[] array = numbers.Split(';');

            foreach(string s in array)
            {
                DataRow row = tbl.NewRow();
                string[] numb = s.Split(',');
                row["Col1"] = numb[0];
                row["Col2"] = numb[1];

                tbl.Rows.Add(row);
            }

Here is the code which you need. Next time do something from your side first and show your effort.

Upvotes: 0

A. Abramov
A. Abramov

Reputation: 1865

You pretty much named it; what you need to do is split the strings, and insert them in couples. If you have 1 extra string (The amount of substrings is not-odd), insert the last one in the end of the database in it's own row.

string yourString = "insert your string"
string[] Splits = yourString.split(",");// Dividing your string by "," chars.
int i=0;// declaring it here for using it later to check if the number is odd
for(int i=0; i < Splits.Length-1; i+=2)
{
InsertIntoDatabase(Splits[i,i+1]); //Inserting two strings into the database
}
//if i is odd, i will be equal exacly to the Length here. Otherwise...
if(i<Splits.Length)
{
InsertIntoDatabase(Splits[i]); // Insert the last string in it's own row.
}

InsertIntoDatabase has to insert 2 strings ( i can't implement it myself for you since i dont really know what DB& Settings you are using) and go one line down. Overload it with a function that recieves a single string, and you got yourself a good way to go. :)

EDIT: Elegant.. Just saw from the comments. I am not sure what you really mean by that, But i guess you could override the string.Split and send the string to the DB each time you split it - might make it slightly more "elegant", but that's up to your defintion. Good luck

Upvotes: 1

Related Questions