C#操作Oracle中大数据(Blob)

之前在网上搜索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 }

 

好像通用性还是不够,果然还是新手做给自己用的东西。

 

转载于:https://www.cnblogs.com/Vercher/archive/2012/09/28/2707547.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值