关于文件保存到Oracle中BLOB字段的方法及例子


  1. public class FileOpClass  
  2.     {  
  3.         public static byte[] GetFileStream(string filepath)  
  4.         {  
  5.             byte[] byteArray = null;  
  6.             FileStream fs = null;  
  7.             try  
  8.             {  
  9.                 fs = new FileStream(filepath, FileMode.Open);  
  10.                 long filelength = fs.Length;  
  11.                 byteArray = new byte[filelength];  
  12.                 fs.Read(byteArray, 0, byteArray.Length);  
  13.             }  
  14.             catch (Exception ee)  
  15.             {  
  16.             }  
  17.             finally  
  18.             {  
  19.                 if (fs != null)  
  20.                 {  
  21.                     fs.Close();  
  22.                     fs.Dispose();  
  23.                     fs = null;  
  24.                 }  
  25.             }  
  26.             return byteArray;              
  27.         }  
  28.         public static void WriteToFile(byte[] byteArray, string filepath)  
  29.         {  
  30.             if (System.IO.File.Exists(filepath) == true)  
  31.             {  
  32.                 try  
  33.                 {  
  34.                     System.IO.File.Delete(filepath);  
  35.                 }  
  36.                 catch { }                  
  37.             }  
  38.             FileStream fs = null;  
  39.             try  
  40.             {  
  41.                 fs = new FileStream(filepath, FileMode.OpenOrCreate);  
  42.                 fs.Write(byteArray, 0, byteArray.Length);  
  43.                 fs.Flush();  
  44.             }  
  45.             catch (Exception ee)  
  46.             {  
  47.             }  
  48.             finally  
  49.             {  
  50.                 if (fs != null)  
  51.                 {  
  52.                     fs.Close();  
  53.                     fs.Dispose();  
  54.                     fs = null;  
  55.                 }  
  56.             }  
  57.         }  
  58.     }  
  59. //------------------------------------------   
  60. 先插入一条objblob字段为空的记录,用insert into tablename(objid,objname) values('xxx','yyyy');  
  61. 然后对objblog字段作update方式操作  
  62. //------------------------------------------   
  63. byte[] byteArray =FileOpClass.GetFileStream(path);    
  64. object UpdateBLOBFdValue=byteArray;               
  65.                       
  66. //------------------------------------------   
  67. //更新BLOB字段值的方法   
  68. //------------------------------------------   
  69. public bool UpdateBLOBFieldValue(string UpdateTableName, string UpdateBLOBFieldName, object UpdateBLOBFdValue, string WhereEqFieldName, string WhereEqFdValue)  
  70. {  
  71.             bool rbc = false;  
  72.             DatabaseHelperClass dbh = new DatabaseHelperClass(m_Database);  
  73.             DbConnection dbconn = null;  
  74.             DbTransaction dbtrans = null;  //Provider=OraOleDb.Oracle;   
  75.             DbCommand dbCmd1 = null;  
  76.             try  
  77.             {  
  78.                 dbconn = dbh.db.CreateConnection();  
  79.                 if (dbconn.State != ConnectionState.Open)  
  80.                 {  
  81.                     dbconn.Open();  
  82.                 }  
  83.                 dbtrans = dbconn.BeginTransaction();  
  84.   
  85.                 string PrixChar = dbh.PrixChar;  
  86.                 string x = "update " + UpdateTableName + " set " + UpdateBLOBFieldName + "=" + PrixChar + "Img where " + WhereEqFieldName + "=" + PrixChar + "tmpguid ";  
  87.                 byte[] byteArray = UpdateBLOBFdValue as byte[];  
  88.                 //   
  89.                 dbCmd1 = dbconn.CreateCommand();  
  90.                 dbCmd1.CommandText = x;  
  91.                 dbCmd1.CommandType = CommandType.Text;  
  92.                 dbCmd1.Connection = dbconn;  
  93.                 dbCmd1.Transaction = dbtrans;  
  94.                 //   
  95.                 DbParameter dbparam = dbh.CreateParameter("" + PrixChar + "Img", byteArray);  
  96.                 dbparam.Direction = ParameterDirection.Input;  
  97.                 dbparam.DbType = System.Data.DbType.Binary;  
  98.                 dbparam.Value = byteArray;  
  99.                 dbCmd1.Parameters.Add(dbparam);  
  100.                 dbCmd1.Parameters.Add(dbh.CreateParameter("" + PrixChar + "tmpguid", WhereEqFdValue));  
  101.                 if (dbCmd1.ExecuteNonQuery() > 0)  
  102.                 {  
  103.                     rbc = true;  
  104.                     dbtrans.Commit();  
  105.                 }  
  106.             }  
  107.             catch (Exception ee)  
  108.             {  
  109.                 if (dbtrans != null)  
  110.                 {  
  111.                     dbtrans.Rollback();  
  112.                 }  
  113.                 rbc = false;  
  114.             }  
  115.             finally  
  116.             {  
  117.                 if (dbCmd1 != null)  
  118.                 {  
  119.                     dbCmd1.Dispose();  
  120.                     dbCmd1 = null;  
  121.                 }  
  122.                 //----   
  123.                 if (dbtrans != null)  
  124.                 {  
  125.                     dbtrans.Dispose();  
  126.                     dbtrans = null;  
  127.                 }  
  128.                 if (dbconn != null)  
  129.                 {  
  130.                     dbconn.Dispose();  
  131.                     dbconn = null;  
  132.                 }  
  133.             }  
  134.             return rbc;  
  135. }  
  136. ----the---end-------  

相关内容