[C#]: winform中,如何通过oledb 读取oracle数据库中blob 字段 图片?

newchar 2004-07-27 10:52:38
如题: 如何读取? 看到很多类似sql 的做法,但用oledb 连接时,执行sql 语句出错?不知有没有可行的方案?
引用
private byte[] ReadImage(string pSql)
{
byte[] bty;
OleDbConnection cn;
OleDbCommand cmd;
cn=new OleDbConnection(ConnString) ;
try
{
// psql="select image from table"
cmd=new OleDbCommand(pSql,cn);
cn.Open();
bty=((byte[])cmd.ExecuteScalar());
......
}
catch(Exception a)
{
throw a;
}
}
...全文
494 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
newchar 2004-07-28
  • 打赏
  • 举报
回复
自行搞定! 原来就是那么小小的问题!
fgc5201314 2004-07-28
  • 打赏
  • 举报
回复
//可能你的byte[] imgdata 没有得到流数据:
//here ,use byte[] b instead of byte[] imgdata
//please try the code below:

byte[] b = (byte[])cmd.ExecuteScalar();
if(b.Length > 0)
{
System.IO.MemoryStream stream = new System.IO.MemoryStream(b, true);
stream.Write(b, 0, b.Length);

this.pictureBox1.Image= Image.FromStream(stream);
}
试试看,跳过开始的 78 个字节,忽略这 78 个字节。
这 78 个字节是 OLE 格式的题头。


fgc5201314 2004-07-28
  • 打赏
  • 举报
回复
http://dotnet.aspx.cc/ShowDetail.aspx?id=2A5DD7C6-A45A-48AB-A2E8-342A29F17506
newchar 2004-07-27
  • 打赏
  • 举报
回复
google 中搜索,没有什么有关图片读取的文章. 能说明白点吗?
newchar 2004-07-27
  • 打赏
  • 举报
回复
我试过上述几种方法,都出现这种错误提示. 我怀疑oledb 不支持oracle中这种读取
Error Message: 未指定的错误\r\nOracle error occurred, but error message could not be retrieved from Oracle.\r\nData type is not supported.
Bob 2004-07-27
  • 打赏
  • 举报
回复
在google搜索“.NET 数据访问架构指南”
Bob 2004-07-27
  • 打赏
  • 举报
回复
将BLOB数据写入到数据库中

下面的代码演示了如何利用ADO.NET将从某个文件获得的二进制数据写入SQL Server image字段中。

public void StorePicture( string filename )
{
// Read the file into a byte array
FileStream fs = new FileStream( filename, FileMode.Open, FileAccess.Read );
byte[] imageData = new Byte[fs.Length];
fs.Read( imageData, 0, (int)fs.Length );
fs.Close();

SqlConnection conn = new SqlConnection("");
SqlCommand cmd = new SqlCommand("StorePicture", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@filename", filename );
cmd.Parameters["@filename"].Direction = ParameterDirection.Input;
cmd.Parameters.Add("@blobdata", SqlDbType.Image);
cmd.Parameters["@blobdata"].Direction = ParameterDirection.Input;
// Store the byte array within the image field
cmd.Parameters["@blobdata"].Value = imageData;
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
conn.Close();
}
}

从数据库中读取BLOB数据

在通过ExecuteReader方法创建SqlDataReader对象以读取包含BLOB数据的行时,需使用CommandBehavior.SequentialAccess枚举值。如果没有此枚举值,阅读器一次只从服务器中向客户端发送一行数据。如果行包含了BOLB数据,这预示着要占用大量内存。通过利用枚举值,就获得了更好的控制权,因为BLOB数据只在被引用时才被发出(例如,利用GetBytes方法,可以控制读取的字节数)。这在下面的代码片段中进行了演示。

// Assume previously established command and connection
// The command SELECTs the IMAGE column from the table
conn.Open();
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
reader.Read();
// Get size of image data - pass null as the byte array parameter
long bytesize = reader.GetBytes(0, 0, null, 0, 0);
// Allocate byte array to hold image data
byte[] imageData = new byte[bytesize];
long bytesread = 0;
int curpos = 0;
while (bytesread < bytesize)
{
// chunkSize is an arbitrary application defined value
bytesread += reader.GetBytes(0, curpos, imageData, curpos, chunkSize);
curpos += chunkSize;
}
// byte array 'imageData' now contains BLOB from database

注意使用CommandBehavior.SequentialAccess需要以严格的顺序访问列数据。例如,如果BLOB数据存在于第3列,并且还需要从第1,2列中读取数据,那么在读取第3列前必须先读取第1,2列。

gengruibbx 2004-07-27
  • 打赏
  • 举报
回复
也问,替你顶!
newchar 2004-07-27
  • 打赏
  • 举报
回复
byte 已经读出来了.(换成另外一个连接引擎),但是下面方法怎么不能再picturebox 中显示
...
MemoryStream ms;

if(bty.Length>0)
{
ms=new MemoryStream(bty);
pImg.Image= Image.FromStream(ms);
}
...
newchar 2004-07-27
  • 打赏
  • 举报
回复
byte 已经读出来了.(换成另外一个连接引擎),但是下面方法怎么不能再picturebox 中显示
...
MemoryStream ms;

if(bty.Length>0)
{
ms=new MemoryStream();
pImg.Image= Image.FromStream(ms);
}
...
newchar 2004-07-27
  • 打赏
  • 举报
回复
不明白,怎么读取? 请指点. 谢谢!
chrch 2004-07-27
  • 打赏
  • 举报
回复
用命令参数的方式读取,参数类型为OleDbType.LongVarBinary

110,531

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧