【轉】DataTable儲存與讀取 stream

來源:互聯網
上載者:User

標籤:

private void SaveToFile(byte[] value,string filePath){    System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.OpenOrCreate);    fs.Write(value, 0, value.Length);     fs.Flush();     fs.Close(); }private byte[] ConvertStreamToByteBuffer(string filePath){    System.IO.Stream theStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);    int b1;    System.IO.MemoryStream tempStream = new System.IO.MemoryStream();    while ((b1 = theStream.ReadByte()) != -1)    {         tempStream.WriteByte(((byte)b1));     }    return tempStream.ToArray();}         public static byte[] GetsetBinary(DataTable dt){               byte[] bArrayResult = null; //用於存放序列化後的資料    dt.RemotingFormat = SerializationFormat.Binary; //指定DataSet序列化格式是二進位    MemoryStream ms = new MemoryStream();//定義記憶體流對象,用來存放DataSet序列化後的值    IFormatter IF = new BinaryFormatter();//產生二進位序列化格式    IF.Serialize(ms, dt);//序列化到記憶體中    bArrayResult = ms.ToArray(); // 將DataSet轉化成byte[]    ms.Close();    ms.Dispose();    return bArrayResult;       }       public DataTable RetrieveDataSet(byte[] binaryData){               MemoryStream ms = new MemoryStream(binaryData);//建立記憶體流    IFormatter bf = new BinaryFormatter();//產生二進位序列化格式    object obj = bf.Deserialize(ms);//反序列化到記憶體中    //類型檢驗    ms.Close();    if (obj is DataTable)    {                  DataTable dataSetResult = (DataTable)obj;         return dataSetResult;    }    else    {          return null;     }}        public static byte[] GetBytesByImage(System.Drawing.Image image){     byte[] photo_byte = null;     using (MemoryStream ms = new MemoryStream())        {            Bitmap bmp = new Bitmap(image);            bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);            photo_byte = new byte[ms.Length];            ms.Position = 0;            ms.Read(photo_byte, 0, Convert.ToInt32(ms.Length));            bmp.Dispose();        }    return photo_byte;}            //調用方法:DataTable dt = DbHelperSQL.Query(sql);DataTable newDt = dt.Clone();newDt.Columns.Add("photo", System.Type.GetType("System.Byte[]")); foreach (DataRow dr in dt.Rows) {       newDt.ImportRow(dr);       System.Drawing.Image img = System.Drawing.Image.FromFile(StudentPhotoPath(dr["PhotoUrl"].ToString()));       img = FixedSize(img, 135, 180);       newDt.Rows[newDt.Rows.Count - 1]["photo"] = GetBytesByImage(img);       img.Dispose();}byte[] tableByteArray = GetsetBinary(newDt);SaveToFile(tableByteArray, @"D:\hello.yzd");
byte[] byteArray = ConvertStreamToByteBuffer(@"D:\hello.yzd");DataTable dt = RetrieveDataSet(byteArray);

 

【轉】DataTable儲存與讀取 stream

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.