c# 存储图片到oracle,c# winform 读取oracle中blob字段的图片并且显示到pictureBox里 保存进库...
时间: 2024-03-05 15:53:12 浏览: 118
存储图片到Oracle:
```c#
using Oracle.DataAccess.Client;
using System.Drawing;
using System.IO;
//连接Oracle数据库
string connString = "User Id=<username>;Password=<password>;Data Source=<datasource>";
OracleConnection conn = new OracleConnection(connString);
conn.Open();
//将图片转换为字节数组
Image image = Image.FromFile("<imagePath>");
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] imageBytes = ms.ToArray();
//插入图片数据到Oracle
string sql = "INSERT INTO <tableName> (image_column) VALUES (:imageParam)";
OracleCommand cmd = new OracleCommand(sql, conn);
OracleParameter param = new OracleParameter(":imageParam", OracleDbType.Blob);
param.Value = imageBytes;
cmd.Parameters.Add(param);
cmd.ExecuteNonQuery();
//关闭连接
conn.Close();
```
读取Oracle中BLOB字段的图片并显示到PictureBox:
```c#
using Oracle.DataAccess.Client;
using System.Drawing;
using System.IO;
//连接Oracle数据库
string connString = "User Id=<username>;Password=<password>;Data Source=<datasource>";
OracleConnection conn = new OracleConnection(connString);
conn.Open();
//查询图片数据
string sql = "SELECT image_column FROM <tableName> WHERE <condition>";
OracleCommand cmd = new OracleCommand(sql, conn);
OracleDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
//从查询结果中读取图片数据
byte[] imageBytes = (byte[])reader["image_column"];
//将字节数组转换为Image对象
MemoryStream ms = new MemoryStream(imageBytes);
Image image = Image.FromStream(ms);
//将Image对象显示在PictureBox中
pictureBox1.Image = image;
}
//关闭连接
conn.Close();
```
其中 `<username>`、`<password>`、`<datasource>`、`<tableName>`、`<condition>` 需要根据实际情况进行替换。
阅读全文