using Cksoft.Unity; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Data; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; namespace DllUnityBll { public class MsgPackage { //把数据集序列化成二进制流 public static byte[] DataSetToBytes(DataSet ds, ref string errorinfo) { try { ds.RemotingFormat = SerializationFormat.Binary; MemoryStream ms = new MemoryStream(); IFormatter bf = new BinaryFormatter(); bf.Serialize(ms, ds); byte[] bytes = ms.ToArray(); ms.Close(); //BinaryFormatter ser = new BinaryFormatter(); //MemoryStream unCompressMS = new MemoryStream(); //ds.RemotingFormat = SerializationFormat.Binary; //ser.Serialize(unCompressMS, ds); //byte[] bytes = unCompressMS.ToArray(); //unCompressMS.Close(); return bytes; } catch (Exception ex) { errorinfo = ex.ToString(); return null; } } //将二进制转换为DataSet public static DataSet BytesToDataSet(byte[] datas, ref string errorinfo) { try { MemoryStream ms = new MemoryStream(datas); BinaryFormatter bf = new BinaryFormatter(); object obj = bf.Deserialize(ms); DataSet ds = (DataSet)obj; ms.Close(); return ds; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return null; } } public static string DataSetToStr(DataSet ds, ref string errorinfo) { try { DataTable tableinfo = new DataTable("table_info"); tableinfo.Columns.Add("tablename"); tableinfo.Columns.Add("fnum", Type.GetType("System.Int32")); tableinfo.Columns.Add("colname"); tableinfo.Columns.Add("coltype"); JObject jsonobj = new JObject(); jsonobj.Add("table_count", ds.Tables.Count); string tablename = ""; foreach (DataTable dt in ds.Tables) { tablename += tablename == "" ? dt.TableName : "," + dt.TableName; jsonobj.Add(dt.TableName, dt.ToJson()); int fnum = 0; foreach (DataColumn dc in dt.Columns) { fnum += 10; DataRow addrow= tableinfo.NewRow(); tableinfo.Rows.Add(addrow); addrow["tablename"] = dt.TableName; addrow["fnum"] = fnum; addrow["colname"] = dc.ColumnName; addrow["coltype"] = dc.DataType.ToString(); } } jsonobj.Add("table_name", tablename); jsonobj.Add("table_info", tableinfo.ToJson()); return jsonobj.ToJson(); } catch (Exception ex) { errorinfo = ex.Message.ToString(); return ""; } } public static DataSet StrToDataSet(string orgstr, ref string errorinfo) { try { JObject jsonobj = JsonConvert.DeserializeObject(orgstr); if(jsonobj.ContainsKey("errorinfo")) { errorinfo = jsonobj["errorinfo"].ToString(); return null; } int tablecount = int.Parse(jsonobj["table_count"].ToString()); string tablename = jsonobj["table_name"].ToString(); string[] tabs = tablename.Split(','); DataSet reds = new DataSet(); foreach (string ttname in tabs) { string strs = jsonobj[ttname].ToString(); DataTable tempdt = JsonConvert.DeserializeObject(strs); tempdt.TableName = ttname; reds.Merge(tempdt); } string tableinfostr = jsonobj["table_info"].ToString(); DataTable tableinfodt= JsonConvert.DeserializeObject(tableinfostr); string condition =""; DataRow[] rows =null; foreach (DataTable dt in reds.Tables) { if (dt.Columns.Count > 0)//说明有,无需处理 { //要转换数据类型 //foreach(DataColumn dc in dt.Columns) //{ // condition = string.Format($"tablename='{dt.TableName}' and colname='{dc.ColumnName}'"); // rows = tableinfodt.Select(condition, "fnum"); // if (rows.Length <= 0) // continue; // dc.DataType = Type.GetType(rows[0]["coltype"].ToString()); //} continue; } condition = string.Format($"tablename='{dt.TableName}'"); rows = tableinfodt.Select(condition, "fnum"); foreach(DataRow ttrow in rows) { DataColumn dc = dt.Columns.Add(ttrow["colname"].ToString(), Type.GetType(ttrow["coltype"].ToString())); } } return reds; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return null; } } } }