public string ToJson(int count, Packet packet) { string result = ""; ExportData ajaxJson = new ExportData(); PacketTable table = new PacketTable(); for (int i = 0; i < count; i++) { table.Clear(); if (!packet.Read(ref table)) { return("PackTable不完整"); } int nRows = table.Rows; int nCols = table.Cols; DataTable dt = new DataTable(); for (int m = 0; m < nCols; m++) { PacketTable.FieldType Type = table.GetField(m).Type; string strName = table.GetField(m).Name; if (Type == PacketTable.FieldType.TypeByte) { dt.Columns.Add(table.GetField(m).Name, typeof(byte)); } else if (table.GetField(m).Type == PacketTable.FieldType.TypeDateTime) { dt.Columns.Add(table.GetField(m).Name, typeof(DateTime)); } else if (table.GetField(m).Type == PacketTable.FieldType.TypeDouble) { dt.Columns.Add(table.GetField(m).Name, typeof(double)); } else if (table.GetField(m).Type == PacketTable.FieldType.TypeFloat) { dt.Columns.Add(table.GetField(m).Name, typeof(float)); } else if (table.GetField(m).Type == PacketTable.FieldType.TypeImage) { dt.Columns.Add(table.GetField(m).Name, typeof(byte[])); } else if (table.GetField(m).Type == PacketTable.FieldType.TypeInt) { dt.Columns.Add(table.GetField(m).Name, typeof(int)); } else if (table.GetField(m).Type == PacketTable.FieldType.TypeString) { dt.Columns.Add(table.GetField(m).Name, typeof(string)); } else if (table.GetField(m).Type == PacketTable.FieldType.TypeUint) { dt.Columns.Add(table.GetField(m).Name, typeof(uint)); } } for (int nRow = 0; nRow < nRows; nRow++) { DataRow row = dt.NewRow(); for (int nCol = 0; nCol < nCols; nCol++) { PacketTable.Field field = table.GetField(nCol); if (field.Type == PacketTable.FieldType.TypeByte) { row[nCol] = (byte)table.GetValue(nRow, nCol); } else if (field.Type == PacketTable.FieldType.TypeDouble) { row[nCol] = (double)table.GetValue(nRow, nCol); } else if (field.Type == PacketTable.FieldType.TypeFloat) { row[nCol] = (float)table.GetValue(nRow, nCol); } else if (field.Type == PacketTable.FieldType.TypeImage) { byte[] data = (byte[])table.GetValue(nRow, nCol); row[nCol] = data; } else if (field.Type == PacketTable.FieldType.TypeInt) { row[nCol] = (int)table.GetValue(nRow, nCol); } else if (field.Type == PacketTable.FieldType.TypeUint) { row[nCol] = (UInt32)table.GetValue(nRow, nCol); } else if (field.Type == PacketTable.FieldType.TypeString) { row[nCol] = (string)table.GetValue(nRow, nCol); } else if (field.Type == PacketTable.FieldType.TypeDateTime) { row[nCol] = (DateTime)table.GetValue(nRow, nCol); } } dt.Rows.Add(row); } ajaxJson.arrTable.Add(dt); } ajaxJson.Status = "1"; ajaxJson.Msg = "执行成功!"; result = JsonConvert.SerializeObject(ajaxJson); return(result); }
public DataTable ToTable(Packet packet, int count = 1) { DataTable dt = new DataTable(); PacketTable table = new PacketTable(); table.Clear(); if (!packet.Read(ref table)) { return(null); } int nRows = table.Rows; int nCols = table.Cols; for (int m = 0; m < nCols; m++) { PacketTable.FieldType Type = table.GetField(m).Type; string strName = table.GetField(m).Name; if (Type == PacketTable.FieldType.TypeByte) { dt.Columns.Add(table.GetField(m).Name, typeof(byte)); } else if (table.GetField(m).Type == PacketTable.FieldType.TypeDateTime) { dt.Columns.Add(table.GetField(m).Name, typeof(DateTime)); } else if (table.GetField(m).Type == PacketTable.FieldType.TypeDouble) { dt.Columns.Add(table.GetField(m).Name, typeof(double)); } else if (table.GetField(m).Type == PacketTable.FieldType.TypeFloat) { dt.Columns.Add(table.GetField(m).Name, typeof(float)); } else if (table.GetField(m).Type == PacketTable.FieldType.TypeImage) { dt.Columns.Add(table.GetField(m).Name, typeof(byte[])); } else if (table.GetField(m).Type == PacketTable.FieldType.TypeInt) { dt.Columns.Add(table.GetField(m).Name, typeof(int)); } else if (table.GetField(m).Type == PacketTable.FieldType.TypeString) { dt.Columns.Add(table.GetField(m).Name, typeof(string)); } else if (table.GetField(m).Type == PacketTable.FieldType.TypeUint) { dt.Columns.Add(table.GetField(m).Name, typeof(uint)); } } for (int nRow = 0; nRow < nRows; nRow++) { DataRow row = dt.NewRow(); for (int nCol = 0; nCol < nCols; nCol++) { PacketTable.Field field = table.GetField(nCol); if (field.Type == PacketTable.FieldType.TypeByte) { row[nCol] = (byte)table.GetValue(nRow, nCol); } else if (field.Type == PacketTable.FieldType.TypeDouble) { row[nCol] = (double)table.GetValue(nRow, nCol); } else if (field.Type == PacketTable.FieldType.TypeFloat) { row[nCol] = (float)table.GetValue(nRow, nCol); } else if (field.Type == PacketTable.FieldType.TypeImage) { byte[] data = (byte[])table.GetValue(nRow, nCol); row[nCol] = data; } else if (field.Type == PacketTable.FieldType.TypeInt) { row[nCol] = (int)table.GetValue(nRow, nCol); } else if (field.Type == PacketTable.FieldType.TypeUint) { row[nCol] = (UInt32)table.GetValue(nRow, nCol); } else if (field.Type == PacketTable.FieldType.TypeString) { row[nCol] = (string)table.GetValue(nRow, nCol); } else if (field.Type == PacketTable.FieldType.TypeDateTime) { row[nCol] = (DateTime)table.GetValue(nRow, nCol); } } dt.Rows.Add(row); } return(dt); }