Dataset column name and row value

倖福魔咒の 提交于 2019-12-23 12:55:54

问题


I have a data set:

DataSet ds= _ExecuteQuery(contentCmd, CommandType.Text, contentParams);

// Column name
foreach (DataColumn column in ds.Tables[0].Columns)
{
  Console.WriteLine(column.ColumnName);
}

// Rows value
foreach (DataTable dt in ds.Tables){
     foreach (DataRow dr in dt.Rows){
        Console.Writeline(dr.Table.Rows[0]["name"].ToString());
     }
}

How can I retrieve rows value and column name of the row?

List<columnName, name>;

回答1:


I assume that you're looking for something like this:

        Dictionary<string, string> vals = new Dictionary<string, string>();

        foreach (DataRow dr in dt.Rows)
        {
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                vals.Add(dt.Columns[i].ColumnName, dr[dt.Columns[i].ColumnName].ToString());
            }
        }

Depends on your dataset, but this gives a full dictionary of each column name and corresponding value



来源:https://stackoverflow.com/questions/42337983/dataset-column-name-and-row-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!