Click here to Skip to main content
15,917,320 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
public static Dictionary<string, object> ToJson(DataTable table)
{
    Dictionary<string, object> j = new Dictionary<string, object>();
    j.Add(table.TableName, RowsToDictionary(table));
    return j;
}

private static List<Dictionary<string, object>> RowsToDictionary(DataTable table)
{
    List<Dictionary<string, object>> objs =
        new List<Dictionary<string, object>>();
    foreach (DataRow dr in table.Rows)
    {
        Dictionary<string, object> drow = new Dictionary<string, object>();
        for (int i = 0; i < table.Columns.Count; i++)
        {
            drow.Add(table.Columns[i].ColumnName, dr[i]);
        }
        objs.Add(drow);
    }

    return objs;
}
Posted
Comments
AmitGajjar 7-Sep-12 23:35pm    
if you really don't know what this method do, Then i suggest you not to use this function.

1 solution

Well, the second method RowsToDictionary converts a DataTable to a List of Dictionaries (basically it translates a whole table into a C# data structure that resembles the structure of a table), while the second method ToJson actually gives it a name and returns it
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900