简体   繁体   English

c#中如何将DataTable中的值转换为字符串数组

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

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 the string array index For example, if my DataTable is as follows我想将此 DataTable 值转换为字符串数组,以便我可以通过字符串数组索引访问该 DataTable 的列值例如,如果我的 DataTable 如下

|  Name  |  Address  |   Age  |
-------------------------------
|  jim   |    USA    |   23   |

I want to store the values in that Datatable into my string array such that MyStringArray[1] will give me the value USA.我想将该数据表中的值存储到我的字符串数组中,这样MyStringArray[1]会给我值 USA。

Thanks in Advance提前致谢

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.其中DataRow.ItemArray属性是一个对象数组,其中包含数据表每一列的行值。

Perhaps something like this, assuming that there are many of these rows inside of the datatable and that each row is row :也许是这样的,假设数据表中有很多这样的行并且每一行都是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:如果您使用linqpad ,这是您示例的一个非常简单的场景:

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"
}

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

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

If you would like to use System.Data instead of System.LINQ , then you can do something like this.如果您想使用System.Data而不是System.LINQ ,那么您可以执行以下操作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM