odp.net 读写oracle blob字段
2024-08-24 23:01:32
DEVELOPER: ODP.NET Serving Winning LOBs:
http://www.oracle.com/technetwork/issue-archive/2005/05-nov/o65odpnet-085139.html
Data Provider for .NET Developer's Guide:
https://docs.oracle.com/database/121/ODPNT/OracleBlobClass.htm#ODPNT4035
从blob字段读取一个图片文件的代码:
OracleConnection con = new OracleConnection(connStr);
con.Open(); // statement to get a blob
string sql = "select yz from sysusers where yhdh='123'"; // create command object
// InitialLOBFetchSize
// defaults to 0
OracleCommand cmd = new OracleCommand(sql, con); // create a datareader
using (OracleDataReader dr = cmd.ExecuteReader())
{
// read the single row result
dr.Read();
// use typed accessor to retrieve the blob
OracleBlob blob = dr.GetOracleBlob(); // create a memory stream from the blob
using (MemoryStream ms = new MemoryStream(blob.Value))
{
// set the image property equal to a bitmap
// created from the memory stream
pictureBox1.Image = new Bitmap(ms);
}
}
保存文件到blob字段的代码:
con.Open(); // 利用事务处理(必须)
OracleTransaction transaction = con.BeginTransaction();
string sql="select yz from sysusers where yhdh='123' FOR UPDATE";
OracleCommand cmd = new OracleCommand(sql, con); using ( OracleDataReader reader = cmd.ExecuteReader())
{
//Obtain the first row of data.
reader.Read();
//Obtain a LOB.
OracleBlob blob = reader.GetOracleBlob();
blob.Erase();
// 将文件写入 BLOB 中
byte[] Buffer;
FileStream fs = new FileStream(@"d:\01.jpg", FileMode.Open);
using (MemoryStream ms = new MemoryStream())
{
int b;
while ((b = fs.ReadByte()) != -)
{
ms.WriteByte((byte)b);
}
Buffer = ms.ToArray();
} // Begin ChunkWrite to improve performance
// Index updates occur only once after EndChunkWrite
blob.Position=;
blob.BeginChunkWrite(); //启用BeginChunkWrite不是必须,只与性能有关
blob.Write(Buffer, ,Buffer .Length); //从字节数据写入blob字段
blob.EndChunkWrite();
blob.Close();
}
// 提交事务
transaction.Commit();
con.Close();
最新文章
- 使用caffe时遇到的问题
- Bzoj1115 石子游戏Kam
- python二维数组
- [LeetCode]题解(python):082 - Remove Duplicates from Sorted List II
- Oracle目录结构及创建新数据库
- MongoDB 的创建、查询、更新、删除
- vijosP1159 岳麓山上打水
- thinkphp如何写find_in_set这样的orm查询封装
- Asp.net Mvc 第二回 UrlRouting
- SSDT表详解
- iOS 用UISearchDisplayController实现查找功能
- kafka服务安装-SuSE Linux Enterprise Server 11 SP3
- 1599: [Usaco2008 Oct]笨重的石子
- 深入理解JavaScript的this指向问题
- Linux - 系统信息相关命令
- 5. Go函数
- 《Java程序设计》第二周学习记录(2)
- 整理this笔记
- concurrent.futures模块(进程池/线程池)
- ftrace利器之trace-cmd和kernelshark