// This method is used to convert
datatable to json string
public string
ConvertDataTabletoString()
{
DataTable dt = new DataTable();
using (SqlConnection
con = new SqlConnection("Data Source=SureshDasari;Initial
Catalog=master;Integrated Security=true"))
{
using (SqlCommand
cmd = new SqlCommand("select
title=City,lat=latitude,lng=longitude,description from LocationDetails",
con))
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
List<Dictionary<string, object>>
rows = new List<Dictionary<string,
object>>();
Dictionary<string, object> row;
foreach (DataRow
dr in dt.Rows)
{
row = new Dictionary<string,
object>();
foreach (DataColumn
col in dt.Columns)
{
row.Add(col.ColumnName, dr[col]);
}
rows.Add(row);
}
return serializer.Serialize(rows);
}
}
}
0 comments:
Post a Comment