標籤:eof ons null create second span 轉換 try ram
將datatable的某一列轉換成json格式的字串(轉換完後自己在字串前後加{})
/// <summary>
///DataTable裝換 Column資料 組合成json 現在的格式
/// </summary>
/// <param name="dt"></param>
/// <param name="rowIndex">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;
}
C#將datatable的某一列轉換成json格式的字串