将 C# 的图像对象保存到 Oracle BLOB 字段中
来源:互联网 发布:蒸腾系数的算法 编辑:程序博客网 时间:2024/06/04 19:45
将 C# 的图像对象保存到 Oracle BLOB 字段中
开源中国诚邀您参加 Cloud Foundry 中国群英会!(北京、上海、杭州、成都、深圳)
本文我想跟大家分享的是如何将 C# 中的一些图像对象保存到 Oracle 中的 BLOB 字段中,这里我们并不想从零开始,而是使用我自己的框架,下面我们开始。
由于Oracle课程设计中涉及到图片的处理,测试了好长的时间和查阅了很多的资料,终于把这个问题解决了,在这和大家分享一下:
一、图片保存到数据库
oracle中一般保存图片的类型为blob,一般的步骤为:
1,首先把图片转化为二进制(byte[])
FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);
byte[] buffByte = new byte[fs.Length];
fs.Read(buffByte, 0, Convert.ToInt32(fs.Length));
fs.Close();
2,把二进制保存到oracle数据库
string strconn = "data source = oem;user id= exam; password=exam;persist security info=false;";
OracleConnection conn = new OracleConnection(strconn);
conn.Open();
OracleCommand comm = new OracleCommand("addUser", conn);//存储过程adduser
comm.CommandType = CommandType.StoredProcedure;
comm.Parameters.Add("i_name", OracleType.VarChar, 50);
comm.Parameters.Add("i_pic", OracleType.Blob);
//comm.Parameters.Add("i_result", OracleType.Int32);
comm.Parameters[0].Value = this.textBox2.Text;
comm.Parameters[1].Value = buffByte;//这个为图片的二进制形式
//comm.Parameters[2].Direction = ParameterDirection.Output;
comm.ExecuteNonQuery();
conn.Close();
二、读取oracle数据库转化为图片www.linuxidc.com
步骤:
1、读出二进制
string strconn = "data source = oem;user id= exam; password=exam;persist security info=false;";
OracleConnection conn = new OracleConnection(strconn);
conn.Open();
OracleCommand comm = new OracleCommand("selectUser", conn);
comm.CommandType = CommandType.StoredProcedure;
comm.Parameters.Add("flag", OracleType.Int32);
comm.Parameters.Add("i_name", OracleType.VarChar, 50);
comm.Parameters.Add("cur_out", OracleType.Cursor);
comm.Parameters[0].Value = 2;
comm.Parameters[1].Value = this.comboBox1.SelectedValue.ToString();
comm.Parameters[2].Direction = ParameterDirection.Output;
OracleDataAdapter da = new OracleDataAdapter(comm);
DataSet ds = new DataSet();
da.Fill(ds);
conn.close();
2、转化为图片
if (ds.Tables[0].Rows.Count > 0)
{
MemoryStream buf = new MemoryStream();
byte[] blob = (byte[])ds.Tables[0].Rows[0]["pic"];//
buf.Write(blob, 0, blob.Length);
Image image = Image.FromStream(buf);
pictureBox2.Image = image;
}
涉及到的存储过程
//create or replace procedure addUser
//(i_name in U_INFO.NAME%type,
// i_pic in u_info.pic%type
// )
// is
// begin
// insert into U_INFO(name,pic) values(i_name,i_pic);
// end adduser;
//create or replace procedure selectUser
//(flag in int,
// i_name in u_info.name%type,
// cur_out out sys_refcursor
// )
// is
// begin
// if flag=1 then
// open cur_out for
// select name from U_Info;
// end if;
// if flag=2 then
// open cur_out for
// select * from U_Info where name=i_name;
// end if;
// end selectUser;
注:在此遇见的问题
Image.FromStream(buf);这块报参数无效
可能的原因:
1,图片转化为二进制是出现问题,导致读取的二进制不能转化为图片,所以报参数无效
2,读取时转化出错,可能byte长度不够,导致不完整,报参数无效
本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http://www.linuxidc.com/Linux/2011-05/36422.htm
1. 首先创建一个空的表:
1
CREATE
TABLE
GAMBAR
2
(
3
GAMBAR_BIN BLOB
4
);
01
using
System.Windows;
02
using
System.Data.OracleClient;
03
using
System.Data;
04
using
System;
05
06
namespace
WpfSavePhotoToOracle
07
{
08
/// <summary>
09
/// Interaction logic for Window1.xaml
10
/// </summary>
11
public
partial
class
Window1 : Window
12
{
13
OracleConnection conn;
14
OracleCommand cmd;
15
16
public
Window1()
17
{
18
InitializeComponent();
19
}
20
21
private
void
btnSaveToOracle_Click(
object
sender, RoutedEventArgs e)
22
{
23
try
24
{
25
conn =
new
OracleConnection(
26
"Data Source=localhost;"
+
27
"Persist Security Info=True;"
+
28
"User ID=C07;Password=C07;Unicode=True"
);
29
conn.Open();
30
31
cmd =
new
OracleCommand(
32
"INSERT INTO GAMBAR VALUES(:blobtodb)"
,
33
conn);
34
cmd.CommandType = CommandType.Text;
35
36
OracleParameter param = cmd.Parameters.Add(
"blobtodb"
,
37
OracleType.Blob);
38
param.Direction = ParameterDirection.Input;
39
param.Value = imgViewer.ImageBinaryData;
40
41
cmd.ExecuteNonQuery();
42
43
conn.Close();
44
MessageBox.Show(
"Image saved"
);
45
}
46
catch
(Exception ex)
47
{
48
MessageBox.Show(
"failed to save ("
+ ex.Message +
")"
);
49
}
50
}
51
}
52
}
7. 现在可以测试你的项目了,点击 Browse... ,选择一个图片然后点击保存按钮,你将看到一个 “Image Saved” 的消息提示框,这表示成功了。
8. 现在检查你的数据库看看写入的数据。
好了,就这么简单。
下面你可以阅读:从 Oracle BLOB 字段中加载图像并显示到 WPF 图像控件
英文原文,OSCHINA原创翻译
- 将 C# 的图像对象保存到 Oracle BLOB 字段中
- 将 C# 的图像对象保存到 Oracle BLOB 字段中
- C#将文件保存到Oracle的BLOB字段
- 处理Blob类型数据,例如将对象保存到blob字段中
- c# winform 读取oracle中blob字段的图片并且显示到pictureBox里,保存进库
- 将图片保存至Oracle数据库Blob字段中
- 利用sql将图像存入oracle blob字段中
- 将网络中的图片存为NSData并保存到sqlite的BLOB字段中.
- 将网络中的图片存为NSData并保存到sqlite的BLOB字段中
- 将网络中的图片存为NSData并保存到sqlite的BLOB字段中
- 将网络中的图片存为NSData并保存到sqlite的BLOB字段中
- struts+hibernate上传图片保存到oracle的blob字段
- Kettle 生成的XML格式数据保存到Oracle 数据库的BLOB类型字段中
- 关于文件保存到Oracle中BLOB字段的方法及例子
- c# winform 操作oracle数据库的Blob字段,把图片存储到数据库,保存图片到数据库
- 将Oracle中Blob字段存储的图片转换成字节流直接输出到浏览器
- c# winform 读取oracle中blob字段的图片显示到pictureBox里
- java将文件保存为二进制流到oracle表中的blob字段
- 带自定义类的Cocos2dx_lua项目编译到Android
- ListView异步加载网络图片完美版之双缓存技术
- ZOJ3228 Trie树
- OpenCV2.4.4实现HOG行人检测
- 让NDK支持C++
- 将 C# 的图像对象保存到 Oracle BLOB 字段中
- QQ赞(初学javascript开发的chrome扩展)
- 绑定变量
- linux C编程学习之线程(二)
- 批量转换为因子变量
- uboot中C语言代码入口函数(start_armboot)的注释
- MyBatis 自动生成单表的Model DAO SqlMap 底层操作代码
- android自定义控件
- 宏 container_of 详解