Reputation: 23
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
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
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