public string toJSON(SqlDataReader o)
{
StringBuilder s = new StringBuilder();
s.Append("[");
if (o.HasRows)
while (o.Read())
s.Append("{" + '"' + "Id" + '"' + ":" + o["Id"] + ", "
+ '"' + "CN" + '"' + ":" + o["CatName"] + ", "
+ '"' + "Ord" + '"' + ":" + o["Ord"] + ","
+ '"' + "Icon" + '"' + ":" + o["Icon"] + "}, ");
s.Remove(s.Length - 2, 2);
s.Append("]");
o.Close();
return s.ToString();
}
我在这里使用自己的函数进行序列化。我需要知道这是否是一个好方法,或者我应该使用其他方法。顺便说一下,我已经尝试过使用JavaScriptSerializer
,但它与SqlDataReader不兼容。谢谢。
string.Format
,让你的生活更轻松。 - OdedStringBuilder.AppendFormat
呢?使用逐字字符串文字(以@
开头的字符串)呢? - Oded