将datatable的某一列转换成json格式的字符串(转换完后自己在字符串前后加{})
/// <summary>
///DataTable装换 Column数据 组合成json 现在的格式
/// </summary>
/// <param></param>
/// <param>datatable第几行</param>
/// <returns></returns>
protected static string createTabColumnDataJson(System.Data.DataTable dt, int rowIndex)
{
string jsonStr = "";
DateTime dtime;
for (int i = 0; i < dt.Columns.Count; i++)
{
string cName = dt.Columns[i].ColumnName;
string cValue = dt.Rows[rowIndex][cName].ToString();
if (!String.IsNullOrEmpty(cValue)) //!String.IsNullOrEmpty(cValue) edit by Spark 2011-3-30 f_parcel_term1,f_parcel_term1
{
try
{
//Regex r = new Regex(@"\d{4}-\d{1,2}-\d{1,2}(\s+\d{1,2}:\d{1,2}:\d{1,2})?");
if (dt.Columns[cName].DataType == typeof(DateTime))
//if (r.IsMatch(cValue))
{
dtime = DateTime.Parse(cValue);
if (dtime.Hour == 0 && dtime.Minute == 0 && dtime.Second == 0)
{
cValue = dtime.ToString("yyyy-MM-dd");
}
else
{
cValue = dtime.ToString("yyyy-MM-dd HH:mm:ss");
}
}
}
catch (Exception ex)
{ }
if (jsonStr == "")
jsonStr = cName + ":‘" + cValue + "‘";
else
jsonStr += "," + cName + ":‘" + cValue + "‘";
}
}
return jsonStr;
}