0

I have a DataRow, I need convert it to a string of array! Everyone help me, please! Ex: MyDataRow include many column(Index type of int, Name type of string, Age type of int.... ) => to array array[0]: 1 array[1]: Henry array[2]: 23 ....

1
  • Welcome to Stack Overflow. This is not a good way to ask a question here. Did you try anything so far to solve your problem? Show your effort first so people might show theirs. Please read FAQ, How to Ask and help center as a start. Commented Sep 17, 2014 at 10:15

3 Answers 3

3

DataRow itself has property ItemArray, you can use it. Try this code

StringBuilder sb=new StringBuilder();
foreach(DataRow dr in dt.Rows)
{
    object[] arr = dr.ItemArray;
    for (int i = 0; i < arr.Length; i++)
    {
         sb.Append(Convert.ToString(arr[i]));
         sb.Append("|");
    }
 }
Response.Write(sb.ToString());
Sign up to request clarification or add additional context in comments.

Comments

0

simply you can convert as follows.

var dt = new DataTable();
dt.Load(cmd.ExecuteReader());
var rows = dt.AsEnumerable().ToArray();
int num = 0;
string[] strarr = new string[rows.Length];
foreach (DataRow raw in rows)
{
    strarr[num] = raw.ItemArray[0].ToString(); 
    num++;
}

Comments

0

Easy with Linq:

dr.ItemArray.Select(c => c.ToString()).ToArray();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.