Hồ Tấn Phong
Hồ Tấn Phong

Reputation: 23

How to convert datarow to string of array?

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

Upvotes: 0

Views: 15631

Answers (3)

Mohsen
Mohsen

Reputation: 4235

Easy with Linq:

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

Upvotes: 0

Charitha Goonewardena
Charitha Goonewardena

Reputation: 4714

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++;
}

Upvotes: 0

Vishwanath Mishra
Vishwanath Mishra

Reputation: 445

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());

Upvotes: 3

Related Questions