123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- 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 DllMsgPackage
- {
- 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<JObject>(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<DataTable>(strs);
- tempdt.TableName = ttname;
- reds.Merge(tempdt);
- }
- string tableinfostr = jsonobj["table_info"].ToString();
- DataTable tableinfodt= JsonConvert.DeserializeObject<DataTable>(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;
- }
- }
- }
- }
|