/// <summary>
/// table转成2进制
/// </summary>
/// <param name="table">表</param>
/// <returns>2进制</returns>
static public byte[] TableToByte(ref System.Data.DataTable table)
{
TableData result=new TableData();
result.m_RowNum=table.Rows.Count;
result.m_ColNum=table.Columns.Count-1;
int nLength=8;
byte [][]byteCell=new byte[result.m_RowNum*result.m_ColNum][];
result.m_ushortCellLength=new ushort[result.m_RowNum,result.m_ColNum];
for(int i=0;i<result.m_RowNum;i++)
{
for(int j=0;j<result.m_ColNum;j++)
{
byteCell[i*result.m_ColNum+j]=Encoding.Unicode.GetBytes(table.Rows[i][j].ToString());
result.m_ushortCellLength[i,j]=(ushort)byteCell[i*result.m_ColNum+j].Length;
nLength+=byteCell[i*result.m_ColNum+j].Length;
}
}
result.m_Cell=new byte[nLength+result.m_RowNum*result.m_ColNum*2];
byte []byteRow=BitConverter.GetBytes(result.m_RowNum);
byte []byteCol=BitConverter.GetBytes(result.m_ColNum);
            byteRow.CopyTo(result.m_Cell,0);
byteCol.CopyTo(result.m_Cell,4);
int nLocation=8;
for(int i=0;i<result.m_RowNum;i++)
{
for(int j=0;j<result.m_ColNum;j++)
{
byte []byteCellLength=BitConverter.GetBytes(result.m_ushortCellLength[i,j]);
byteCellLength.CopyTo(result.m_Cell,nLocation);
nLocation+=2;
}
}
int nRow=0,nCol=0;
for(int i=0;i<result.m_RowNum*result.m_ColNum;i++)
{
nRow=i/result.m_ColNum;
nCol=i-nRow*result.m_ColNum;
byteCell[i].CopyTo(result.m_Cell,nLocation);
nLocation+=result.m_ushortCellLength[nRow,nCol];
}
return result.m_Cell;
} /// <summary>
/// 2进制转成table
/// </summary>
/// <param name="byteContent">2进制内容</param>
/// <param name="strCaption">标题</param>
/// <returns>表</returns>
static public DataTable byteToTable(ref byte []byteContent,string []strCaption)
{
System.Data.DataTable table=new DataTable();
            TableData result=new TableData();
result.m_RowNum=BitConverter.ToInt32(byteContent,0);
result.m_ColNum=BitConverter.ToInt32(byteContent,4);
result.m_ushortCellLength=new ushort[result.m_RowNum,result.m_ColNum];
int i,j;
for(i=0;i<result.m_ColNum;i++)
{
System.Data.DataColumn col=new DataColumn(strCaption[i],System.Data.DbType.String.GetType());
table.Columns.Add(col);
}
for(i=0;i<result.m_RowNum;i++)
{
System.Data.DataRow row=table.NewRow();
table.Rows.Add(row);
}
int nLocation=8;
//设置单元格长度
for(i=0;i<result.m_RowNum;i++)
{
for(j=0;j<result.m_ColNum;j++)
{
result.m_ushortCellLength[i,j]=BitConverter.ToUInt16(byteContent,nLocation);
nLocation+=2;
}
}
//设置表内容
for(i=0;i<result.m_RowNum;i++)
{
for(j=0;j<result.m_ColNum;j++)
{
table.Rows[i][j]=Encoding.Unicode.GetString(byteContent,nLocation,result.m_ushortCellLength[i,j]);
nLocation+=result.m_ushortCellLength[i,j];
}
}
return table;
}

解决方案 »

  1.   

    /// <summary>
    /// 查询结果包
    /// </summary>
    private struct TableData
    {
    public int m_RowNum; //行数
    public int m_ColNum; //列数
    public ushort[,] m_ushortCellLength; //单元格长度
    public byte[] m_Cell; //单元格(i×列数+j)
    }
      

  2.   

    你可以传输一个DataSet过去.DataTable 是不行的.因为datatable 它不能序列化.
      

  3.   

    DATASET能序列化么?那如何反序列化?
      

  4.   

    用 conan19771130(柯南) 的方法
      

  5.   

    /// <summary>
    /// 对象序列化对象类
    /// </summary>
    public class Serializable
    {
    private Serializable()
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
    }
    private static Serializable mSingleOjb = null;
    public static Serializable SingleOjb
    {
    get
    {
    if(mSingleOjb == null)
    mSingleOjb =new Serializable();
    return mSingleOjb;
    }
    }
    public string SerializeObjectToString(object pObj)
    {
    Byte[] bytes = SerializeObject(pObj);
    //string objtxt= System.Text.Encoding.Default.GetString(bytes);
    string objtxt = Convert.ToBase64String(bytes);
    return objtxt;
    }
    /// <summary>
    /// 把对象序列化并返回相应的字节
    /// </summary>
    /// <param name="pObj">需要序列化的对象</param>
    /// <returns>byte[]</returns>
    public  byte[] SerializeObject(object pObj)
    {
    if(pObj == null)
    return null;
    System.IO.MemoryStream _memory = new System.IO.MemoryStream();
    BinaryFormatter formatter = new BinaryFormatter();
    formatter.Serialize(_memory,pObj);
    _memory.Position = 0;
    byte[] read = new byte[_memory.Length];
    _memory.Read(read,0,read.Length);
    _memory.Close();
    return read;
    }
    /// <summary>
    /// 序列化对象并保存到文件中
    /// </summary>
    /// <param name="pObj">需要序列化的对象</param>
    /// <param name="pFileName">保存的文件名</param>
    public  void SerializeObject(object pObj,string pFileName)
    {
    System.IO.FileStream stream = System.IO.File.Open(pFileName,System.IO.FileMode.Create,System.IO.FileAccess.Write);
    byte[] bytes = SerializeObject(pObj);
    stream.Write(bytes,0,bytes.Length);
    stream.Flush();
    stream.Close();
    }
    /// <summary>
    /// 把文件反序列化成对象
    /// </summary>
    /// <param name="pFileName">保存序列化信息的文件</param>
    /// <returns>object</returns>
    public  object DeserializeObject(string pFileName)
    {
    System.IO.FileStream stream = System.IO.File.Open(pFileName,System.IO.FileMode.Open,System.IO.FileAccess.Read);
    byte[] bytes = new byte[stream.Length];
    stream.Read(bytes,0,bytes.Length);
    stream.Close();
    return DeserializeObject(bytes);
    }
    /// <summary>
    /// 把字节反序列化成相应的对象
    /// </summary>
    /// <param name="pBytes">字节流</param>
    /// <returns>object</returns>
    public  object DeserializeObject(byte[] pBytes)
    {
    object _newOjb = null;
    if(pBytes == null)
    return _newOjb;
    System.IO.MemoryStream _memory = new System.IO.MemoryStream(pBytes);
    _memory.Position = 0;
    BinaryFormatter formatter = new BinaryFormatter();
    _newOjb = formatter.Deserialize(_memory);
    _memory.Close();
    return _newOjb;
    }
    /// <summary>
    /// 把字符串反序列化成对象
    /// </summary>
    /// <param name="strobject">保存对象信息的字符串</param>
    /// <returns>object</returns>
    public object DeserializeObjectByString(string strobject)
    {
    Byte[] bytes = Convert.FromBase64String(strobject);//System.Text.Encoding.Default.GetBytes(strobject);
    return DeserializeObject(bytes);
    }
    }
      

  6.   

    另一种方法:
    利用DataSet的WriteXml方法将数据写入一个流然后转成byte[]发送,收到后再用ReadXml读入到DataSet中去。
      

  7.   

    我用 conan19771130(柯南) 
    反序列化失败,郁闷
      

  8.   

    使用基于Tcp的Remoting吧,我一直都是用这个来实现远程数据共享。
      

  9.   

    http://msdn.microsoft.com/library/en-us/dnnetsec/html/secnetht15_topic1
      

  10.   

    怎么将DataTable,利用DataSet的WriteXml方法将数据写入一个流
    然后转成byte[]发送,收到后再用ReadXml读入到DataSet中去。
    ????
    ------------------------------------------------------------------
    [C#] 
    private void WriteXmlToFile(DataSet thisDataSet) {
       if (thisDataSet == null) { return; }
       // Create a file name to write to.
       string filename = "myXmlDoc.xml";
       // Create the FileStream to write with.
       System.IO.FileStream myFileStream = new System.IO.FileStream
          (filename, System.IO.FileMode.Create);
       // Write to the file with the WriteXml method.
       thisDataSet.WriteXml(myFileStream);   
    }
    ----------------------------------
    上面就是WriteXml的用法,看不懂,
    东西写到什么地方去了??
    我怎么转化为byte[]
      

  11.   

    http://msdn.microsoft.com/library/en-us/dnnetsec/html/secnetht15_topic1
      

  12.   

    MemoryStream s = new MemoryStream();
    DataSet ds = new DataSet();
    .....读入数据
    ds.WriteXml(s);
    byte[] b = s.ToArray();
      

  13.   

    BinaryFormatter
    类去看看帮助里面有详细的列子
      

  14.   

    楼主,网上有一个免费的 ZipLib 组件,还带源代码的,可以帮你把 DataSet 压缩成 byte[],不但实现了你的目的,还帮你作了数据压缩,推荐使用。
      

  15.   

    强烈推荐FJGoodGood(_FJ_强中强) 的方法.
      

  16.   

    datatable序列化后比较容易实现。
      

  17.   

    做法很简单 有两种方法:
    1。tcp/ip的:
    在客户端:   DataSet ds=new DataSet();
        DataTable dt=new DataTable("aaa");
        ds.Tables.Add(dt);
        string xml=ds.GetXml();
                 byte[] sendbuf=Encoding.UTF8.GetBytes(xml);在服务器端: string xml=System.Text.UTF8Encoding.UTF8.GetString(sendbuf,0,bytes.Length);
                 System.IO.StringReader sr=new System.IO.StringReader(xml);
                 DataSet ds=new DataSet();
                 DataTable dt=new DataTable();
                 ds.ReadXml(sr);
                 dt=ds.Table[0];2.另一种方法是用Webservice 会更简单,这里不略。。记住要给分啊
    如有不明的可以msn探讨 :[email protected]
      

  18.   

    我用了harryCom() 的方法,
    在客户端写了一个函数
    public void SendData(DataTable DataTableData)
     {
    DataSet ds=new DataSet();
    DataTable dt=new DataTable("aaa");
    ds.Tables.Add(dt);
    string xml=ds.GetXml();
    byte[] sendbuf=Encoding.UTF8.GetBytes(xml);
    //当前tcp流写入缓冲字节
    if (networkStream.CanWrite)
    networkStream.Write(sendbuf,0,sendbuf.Length); Thread.Sleep(TimeSpan.FromMilliseconds(500d));
     }然后在服务端接收
    byte[] buf=new byte[1024*1024]; //预先定义1MB的缓冲 
    int Len=0; //流的实际长度 
    NetworkStream networkStream=tcpClient.GetStream(); //建立读写Tcp的流 
    string xml=System.Text.UTF8Encoding.UTF8.GetString(buf,0,buf.Length);
    System.IO.StringReader sr=new System.IO.StringReader(xml);
    DataSet ds=new DataSet();
    DataTable dt=new DataTable();
    ds.ReadXml(sr);
    dt=ds.Tables[0];
    string TBName = dt.TableName;
    this.MessageList.Items.Add(TBName);结果报错,我看了传过来的xml的值都是一些0\0\0\0\
    怎么回事呀???
      

  19.   

    datatable 应该有直接序列化的函数吧,比如 writexml 等,传这个东西过去就行了,另一端直接 read
    另外也可以先把它变成字节数组,然后再变成串,发送串过去,再相反的过程就行了.
      

  20.   

    我现在正在做,remoting,十分有效的。
      

  21.   

    我把
    string xml=System.Text.UTF8Encoding.UTF8.GetString(buf,0,buf.Length);
    改为
    string xml=Encoding.UTF8.GetString(buf,0,buf.Length);
    就可以了,谢谢
    揭贴