电脑故障

位置:IT落伍者 >> 电脑故障 >> 浏览文章

datatable序列化与反序列化


发布日期:2024/5/28
 
// datatable序列化

public string getSendDetailQuery(DateTime timeS DateTime timeE string sccount)

{

try

{

SmsOperate so = new SmsOperate()

//得到dt

DataTable dtt = sogetSendDetailQuery(timeS timeE sccount)

JavaScriptSerializer serializer = new JavaScriptSerializer()

string aaa = Serialize(dtt false)//datatable不能直接序列化此为序列化方法

return : + aaa;

}

catch (Exception e)

{

return + eMessage;

}

}

/// <summary>序列化方法

/// 不需要分页

/// </summary>

/// <param name=dt></param>

/// <param name=flag>false</param>

/// <returns></returns>

public string Serialize(DataTable dt bool flag)

{

JavaScriptSerializer serializer = new JavaScriptSerializer()

List<Dictionary<string object》 list = new List<Dictionary<string object》()

foreach (DataRow dr in dtRows)

{

Dictionary<string object> result = new Dictionary<string object>()

foreach (DataColumn dc in dtColumns)

{

resultAdd(dcColumnName dr[dc]ToString())

}

listAdd(result)

}

return serializerSerialize(list) ;

}

//反序列化

public DataTable getSendDetailTest()

{

DataTable dtb = new DataTable()

//得到序列化结果aaa

string aaa = getSendDetailQuery(ConvertToDateTime( :: ConvertToDateTime( :: wangsub

if (aaaSubstring( ) ==

{

try

{

JavaScriptSerializer serializer = new JavaScriptSerializer()

// var obj = serializerDeserializeObject(aaa)//反序列化

ArrayList dic = serializerDeserialize<ArrayList>(aaa)//反序列化ArrayList类型

if (dicCount >

{

foreach (Dictionary<string object> drow in dic)

{

if (dtbColumnsCount ==

{

foreach (string key in drowKeys)

{

dtbColumnsAdd(key drow[key]GetType())//添加dt的列名

}

}

DataRow row = dtbNewRow()

foreach (string key in drowKeys)

{

row[key] = drow[key];//添加列值

}

dtbRowsAdd(row)//添加一行

}

}

}

catch (Exception e)

{

//

}

}

else

{

//

}

return dtb;

}

上一篇:DataGrid模板列中的超级链接

下一篇:GridView中的超链接