本文介绍了如何保存此序列化数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何保存此序列化数据?
How do I save this serialized data?
String connStr = ConfigurationManager.ConnectionStrings["MDFserializerdb"].ConnectionString;
String cmdStr = "SELECT * FROM [Table1];";
DataSet ds = new DataSet();
DataTable dt = new DataTable();
try
{
using (SqlConnection conn = new SqlConnection(connStr))
{
using (SqlCommand cmd = new SqlCommand(cmdStr, conn))
{
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
dt = ds.Tables[0];
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);
}
String obj = serializer.Serialize(rows);
conn.Close();
cmd.Dispose();
conn.Dispose();
}
}
}
catch (Exception ex)
{
Label2.Text = ex.ToString();
}
FileWriter file = new FileWriter("c:\\path\\test.json");
file.write(obj.ToJSONString());
file.flush();
file.close();
ASP.net不接受FileWriter。
ASP.net does not accept "FileWriter".
推荐答案
using (StreamWriter _testData = new StreamWriter(Server.MapPath("~/data.txt"), true))
{
_testData.WriteLine(obj); // Write the file.
}
编辑:删除了ToJSONString()。
Edited: Removed ToJSONString().
这篇关于如何保存此序列化数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!