score:29

Accepted answer

very easy:

var stringarr = datatable.rows[0].itemarray.select(x => x.tostring()).toarray();

where datarow.itemarray property is an array of objects containing the values of the row for each columns of the data table.

score:0

    private string[] getprimarykeysoftable(string tablename)
    {
        string stsqlcommand = "select column_name from information_schema.key_column_usage " +
                              "where objectproperty(object_id(constraint_name), 'isprimarykey') = 1" +
                              "and table_name = '" +tablename+ "'";
        sqlcommand command = new sqlcommand(stsqlcommand, connection);
        sqldataadapter adapter = new sqldataadapter();
        adapter.selectcommand = command;

        datatable table = new datatable();
        table.locale = system.globalization.cultureinfo.invariantculture;

        adapter.fill(table);

        string[] result = new string[table.rows.count];
        int i = 0;
        foreach (datarow dr in table.rows)
        {
            result[i++] = dr[0].tostring();
        }

        return result;
    }

score:1

if you would like to use system.data instead of system.linq, then you can do something like this.

 list<string> brandsindatatable = new list<string>();
 datatable g = access._me_g_brandsinpop(); 
        if (g.rows.count > 0)
        {
            for (int x = 0; x < g.rows.count; x++)
                brandsindatatable.add(g.rows[x]["name"].tostring()); 
        }
        else return;

score:3

 
            string[] result = new string[table.columns.count];
            datarow dr = table.rows[0];
            for (int i = 0; i < dr.itemarray.length; i++)
            {
                result[i] = dr[i].tostring();
            }
            foreach (string str in result)
                console.writeline(str);

score:4

if that's all what you want to do, you don't need to convert it into an array. you can just access it as:

string mydata=yourdatatable.rows[0][1].tostring();//gives you usa

score:6

perhaps something like this, assuming that there are many of these rows inside of the datatable and that each row is row:

list<string[]> mystringarrays = new list<string[]>();
foreach( var row in datatable.rows )//or similar
{
 mystringarrays.add( new string[]{row.name,row.address,row.age.tostring()} );
}

you could then access one:

mystringarrays.elementat(0)[1]

if you use linqpad, here is a very simple scenario of your example:

class datatable
{
 public list<data> rows { get; set; }
 public datatable(){
  rows = new list<data>();
 }
}

class data
{
 public string name { get; set; }
 public string address { get; set; }
 public int age { get; set; }
}

void main()
{
 var datatable = new datatable();
 var r = new data();
 r.name = "jim";
 r.address = "usa";
 r.age = 23;
 datatable.rows.add(r);
 list<string[]> mystringarrays = new list<string[]>();
 foreach( var row in datatable.rows )//or similar
 {
  mystringarrays.add( new string[]{row.name,row.address,row.age.tostring()} );
 }
 var s = mystringarrays.elementat(0)[1];
 console.write(s);//"usa"
}

Related Query

More Query from same tag