public static string DataTable2Json(DataTable dt, int totalRows) { StringBuilder jsonBuilder = new StringBuilder(); jsonBuilder.Append("{rows:["); for (int i = 0; i < dt.Rows.Count; i++) { if (string.IsNullOrEmpty(dt.Rows[i]["OID"].ToString())) { continue; } jsonBuilder.Append("{"); for (int j = 0; j < dt.Columns.Count; j++) { jsonBuilder.Append(dt.Columns[j].ColumnName); jsonBuilder.Append(":"); jsonBuilder.Append("'" + DataTableConvertJson.GetFilteredStrForJSON(dt.Rows[i][j].ToString()) + "'"); jsonBuilder.Append(","); } jsonBuilder.Remove(jsonBuilder.Length - 1, 1); jsonBuilder.Append("},"); } //不存在数据时 if (jsonBuilder.Length > 7) { jsonBuilder.Remove(jsonBuilder.Length - 1, 1); } //jsonBuilder.Remove(jsonBuilder.Length - 1, 1); if (totalRows == 0) { jsonBuilder.Append("],total:0"); } else { jsonBuilder.Append("],total:" + totalRows); } jsonBuilder.Append("}"); return(jsonBuilder.ToString()); }
/// <summary> /// 添加数据 /// </summary> /// <returns></returns> private string addData() { string lb = getUTF8ToString("lb"); if (lb == "readWords" || lb == "hisWords")//文件,历史词汇 { return("false"); } string text = getUTF8ToString("text"); text = DataTableConvertJson.GetFilteredStrForJSON(text); if (string.IsNullOrEmpty(text)) { return("false"); } string enName = getUTF8ToString("FK_MapData"); string AttrKey = getUTF8ToString("AttrKey"); string lbStr = ""; string fk_emp = ""; if (lb == "myWords")//我的词汇 { lbStr = "1"; fk_emp = WebUser.No; } if (lb == "sysWords")//系统词汇 { lbStr = "3"; fk_emp = ""; } string addQue = " and FK_MapData='" + enName + "' and AttrKey='" + AttrKey + "' and CurValue='" + text + "'"; string sql = "select * from sys_defval where LB='" + lbStr + "' and FK_Emp='" + fk_emp + "'" + addQue; if (DBAccess.RunSQLReturnCOUNT(sql) != 0) { return("false"); } try { DefVal dv = new DefVal(); dv.FK_MapData = enName; dv.AttrKey = AttrKey; dv.LB = lbStr; dv.FK_Emp = fk_emp; dv.CurValue = text; dv.Insert(); } catch { DefVal dv = new DefVal(); dv.RunSQL("drop table Sys_DefVal"); dv.CheckPhysicsTable(); } return("true"); }