How to Convert the value in DataTable into a string array in c#

前端 未结 6 1178
灰色年华
灰色年华 2020-12-16 00:26

I have a DataTable that contains a single row. I want to convert this DataTable values into a string array such that i can access the column values of that DataTable through

相关标签:
6条回答
  • 2020-12-16 01:01

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

    0 讨论(0)
  • 2020-12-16 01:06
        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;
        }
    
    0 讨论(0)
  • 2020-12-16 01:10

    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"
    }
    
    0 讨论(0)
  • 2020-12-16 01:11

    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
    
    0 讨论(0)
  • 2020-12-16 01:17

    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.

    0 讨论(0)
  • 2020-12-16 01:18

    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;
    
    0 讨论(0)
提交回复
热议问题