之前在網上搜索C#操作Oracle中大數據的源代碼,找到的文章基本都是一篇,都是沒有提供引用庫的,還是得自己寫一個 - -
代碼如下:
1 using System; 2 using System.Data.OracleClient; 3 using System.Data; 4 using System.IO; 5 using System.Windows.Forms; 6 7 8 namespace OraclePDF 9 { 10 public class OracleBlobControl 11 { 12 private string constr = "data source=xxxx;password=xxxx;persist security info=True;user id=xxxx"; 13 private string tempFilePath = @"\"; 14 OracleConnection Conn = null; 15 OracleCommand cmd = null; 16 /// <summary> 17 /// 數據庫連接串 18 /// </summary> 19 public string Constr 20 { 21 get { return constr; } 22 set { constr = value; } 23 } 24 /// <summary> 25 /// 臨時文件目錄 26 /// </summary> 27 public string TempFilePath 28 { 29 get { return tempFilePath; } 30 set { tempFilePath = value;} 31 } 32 33 34 /// <summary> 35 /// 將文件用blob格式保存在數據庫中 blobName:sql語句中的blobs列名 36 /// </summary> 37 /// <param name="FilePath"></param> 38 /// <param name="sql"></param> 39 /// <param name="blobName"></param> 40 /// <returns></returns> 41 public string SaveBlob(string FilePath,string sql,string blobName) 42 { 43 // string sql = "insert into CHY_BLOB_TEST(id,big) values('" + System.DateTime.Now.ToShortTimeString() + "',:a)"; 44 try 45 { 46 //文件流 47 Byte[] blob = GetblobByFilePath(FilePath); 48 //初始化數據庫連接 49 init(sql); 50 //綁定數據 51 OracleParameter param = new OracleParameter(blobName, OracleType.Blob, blob.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, blob); 52 cmd.Parameters.Add(param); 53 //執行 54 cmd.ExecuteNonQuery(); 55 56 return "1"; 57 58 } 59 catch (Exception ex) 60 { 61 return "err;" + ex.Message; 62 } 63 finally 64 { 65 if (Conn != null) 66 { 67 Conn.Close(); 68 } 69 } 70 } 71 72 73 /// <summary> 74 /// 將數據庫中的blob文件以FileType的格式讀出 sql語句返回的第一個列是blob 返回文件的臨時位置 75 /// </summary> 76 /// <param name="FileType"></param> 77 /// <param name="sql"></param> 78 /// <param name="blobName"></param> 79 /// <returns></returns> 80 public string ReaderBlob(string FileType,string sql) 81 { 82 //string sql = "SELECT a.big FROM CHY_BLOB_TEST a where a.id = '17:39'"; 83 try 84 { 85 //初始化數據庫連接 86 init(sql); 87 //從數據庫中讀取blob 88 Byte[] blob = GetBlobByReader(); 89 //文件臨時位置 90 string filePath = tempFilePath + "OraclePDF_Temp." + FileType; 91 SaveTempFile(blob, filePath); 92 return filePath; 93 } 94 catch (Exception e) 95 { 96 return "SQL Exception: " + e.Message; 97 } 98 finally 99 { 100 if (Conn != null) 101 { 102 Conn.Close(); 103 } 104 } 105 } 106 107 public string ExceSqlReturnOne(string sql) 108 { 109 try 110 { 111 //初始化數據庫連接 112 init(sql); 113 return cmd.ExecuteOracleScalar().ToString(); 114 } 115 catch (Exception e) 116 { 117 return "-1"; 118 } 119 finally 120 { 121 if (Conn != null) 122 { 123 Conn.Close(); 124 } 125 } 126 } 127 128 129 /// <summary> 130 /// 將blob存到臨時文件 131 /// </summary> 132 /// <param name="blob"></param> 133 /// <param name="filePath"></param> 134 private static void SaveTempFile(Byte[] blob, string filePath) 135 { 136 FileStream fs = null; 137 //如果存在臨時文件 先刪除 138 if (System.IO.File.Exists(filePath)) //打開文件 139 { 140 System.IO.File.Delete(filePath); 141 } 142 fs = new FileStream(filePath, FileMode.Create, FileAccess.Write); 143 fs.Write(blob, 0, blob.Length); 144 fs.Close(); 145 } 146 147 /// <summary> 148 /// 從OracleDataReader獲取blob二進制流 149 /// </summary> 150 /// <returns></returns> 151 private Byte[] GetBlobByReader() 152 { 153 OracleDataReader odr = cmd.ExecuteReader(); 154 odr.Read(); 155 Byte[] blob = null; 156 //GetBytes 返回字段中的可用字節數 157 //列號 讀取開始位置 要寫入的緩沖區 寫入位置 要讀取的字節數 158 blob = new Byte[(odr.GetBytes(0, 0, null, 0, int.MaxValue))]; 159 odr.GetBytes(0, 0, blob, 0, blob.Length); 160 odr.Close(); 161 return blob; 162 } 163 164 /// <summary> 165 /// 根據文件地址獲取二進制blob 166 /// </summary> 167 /// <param name="FilePath"></param> 168 /// <returns></returns> 169 private static Byte[] GetblobByFilePath(string FilePath) 170 { 171 System.IO.FileStream fs = new System.IO.FileStream(FilePath, FileMode.Open, FileAccess.Read); 172 Byte[] blob = new Byte[fs.Length]; 173 fs.Read(blob, 0, blob.Length); 174 fs.Close(); 175 return blob; 176 } 177 178 /// <summary> 179 /// 初始化數據庫連接 180 /// </summary> 181 private void init(string sql) 182 { 183 Conn = new OracleConnection(constr); 184 Conn.Open(); 185 cmd = new OracleCommand(); 186 cmd.Connection = Conn; 187 cmd.CommandText = sql; 188 } 189 190 191 192 } 193 }
好像通用性還是不夠,果然還是新手做給自己用的東西。