熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

用 C# 編程實現讀寫Binary

2013-11-13 10:28:17  來源: .NET編程 

  本文給出一個用 C# 編程實現讀寫 Binary 的實例代碼對於初學者來說是個不可多得的參考性文章……

  

  以下是引用片段   //返回blob數據   public MemoryStream getBlob(string SQL)   {   try   {   Db_Conn();   cmd = new OleDbCommand(SQL Conn);   cmdCommandType = CommandTypeText;//是sql   OleDbDataReader Rs = cmdExecuteReader();   if (RsRead()) //循環到下一條記錄   {   if (!(RsGetValue() is SystemDBNull))   {   byte[] image_bytes = (byte[])RsGetValue();   MemoryStream ms = new MemoryStream(image_bytes);   return ms;   }   else   return null;   }   else   return null;   }   finally   {   thisclose();   }   }   //設置blob   public bool SetBlob(string SQL MemoryStream Ms)   {   try   {   Db_Conn();   cmd = new OleDbCommand(SQL Conn);   cmdCommandType = CommandTypeText;//是sql   int n=ConvertToInt(MsLengthToString());   MsPosition = ;   byte[] pReadByte = new Byte[n];   MsRead(pReadByte n);   cmdParametersAdd(BLOB OleDbTypeBinary)Value = pReadByte;   cmdExecuteNonQuery();   return true;   }   catch (Exception ex)   {   MessageBoxShow(錯誤:因 + exMessage + 無法執行: + SQL);   return false;   }   finally   {   thisclose();   }   }

  調用 getBlob

  

  以下是引用片段   String sqlStr = select content from dp where id= + ID;
//content為dp中的BLOB字段ID為主鍵   MemoryStream ms = DBClassgetBlob(sqlStr);   if (ms == null)   richTextBoxClear();   else   {   if (msLength > )   {   msPosition = ;   try   {   richTextBoxLoadFile(ms RichTextBoxStreamTypeRichText);   }catch{   richTextBoxLoadFile(ms RichTextBoxStreamTypePlainText);   }   }else   richTextBoxClear();   }

  調用setBlob

  

  以下是引用片段   String sqlStr = update dp set content=:BLOB where id= + ID;   MemoryStream ms = new MemoryStream();   richTextBoxSaveFile(ms RichTextBoxStreamTypeRichText);   if (!DBClassSetBlob(sqlStr ms))   {   MessageBoxShow(保存失敗);   }


From:http://tw.wingwit.com/Article/program/net/201311/13757.html
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.