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();
05-11 20:11