读写数据库图象字段
来源:互联网 发布:如何设置网络共享盘 编辑:程序博客网 时间:2024/05/16 15:14
从论坛上看到好多人在讨论这方面的问题,把代码整理出来,说不定回答问题的时候还能用上呢。
首先,是数据库的设计。我使用的Access数据库。定义一个数据表photo.mdb,定义两个字段photoID和photo ,其中photoID为主键,字段类型为“文本”,photoID的字段类型为“OLE对象”。(注意:如果采用SQL数据库photo的字段类型为“image “。
以字节形式读写数据库图象字段:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
namespace WindowsApplication2
...{
public partial class Form1 : Form
...{
public OleDbConnection dbconn = new OleDbConnection();
public OleDbCommand comm = new OleDbCommand();
public Form1()
...{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
...{
this.OpenConn();
this.pictureBox1.Image = null;
Image im;
this.getPhoto("0000", out im);
this.pictureBox1.Image = im;
}
/**//// <summary>
/// 从数据库中读取图象
/// </summary>
/// <param name="pid"></param>
/// <param name="im"></param>
public void getPhoto(string pid, out Image im)
...{
//读取图象
im = null;
OleDbDataReader dr = SelectDR("select top 1 photo from photo where photoid='" + pid + "'");
if (dr.HasRows)
...{
dr.Read();
if (dr[0] != DBNull.Value)
...{
try
...{
Byte[] byteBLOBData = new Byte[0];
byteBLOBData = (Byte[])dr[0];
MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
im = Image.FromStream(stmBLOBData);
}
catch (Exception ex)
...{
MessageBox.Show(ex.Message);
}
}
else
...{
im = null;
}
}
dr.Close();
}
/**//// <summary>
/// 查询
/// </summary>
/// <param name="SqlStr"></param>
/// <returns></returns>
public OleDbDataReader SelectDR(string SqlStr)
...{
OleDbCommand cmd = new OleDbCommand(SqlStr, dbconn );
return cmd.ExecuteReader();
}
/**//// <summary>
/// 打开数据库连接。
/// </summary>
public void OpenConn()
...{
if (dbconn.State.ToString() == "Closed")
...{
//DBConn.ConnectionString=ConfigurationSettings.AppSettings["constring"]; //."user id=sa;data source=127.0.0.1;persist security info=True;initial catalog=PPF;password=7388";
dbconn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=" + Application.StartupPath + @"dataphoto.mdb;User ID=Admin;Password=";
try
...{
dbconn.Open();
}
catch
...{
MessageBox.Show("数据库连接失败");
}
}
}
private void button1_Click(object sender, EventArgs e)
...{
savePhoto("0000", this.pictureBox1.Image);
}
/**//// <summary>
/// 保存图象到数据库
/// </summary>
/// <param name="pid"></param>
/// <param name="im"></param>
public void savePhoto(string pid, Image im)
...{
OleDbCommand cmd;
OleDbDataReader dr = SelectDR("select photoid from photo where photoID='" + pid + "'");
if (dr.HasRows) //is exist
...{
cmd = new OleDbCommand("Update photo set photoID=@pid, photo=@photo where photoID='" + pid + "'", dbconn );
}
else // not exist
...{
cmd = new OleDbCommand("Insert Into photo (photoID,photo) Values(@pid,@photo) ", dbconn );
}
dr.Close();
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue ("@pid", pid);
cmd.Parameters.AddWithValue ("@photo", DBNull.Value);
//将图片保存到数据库中
if (im == null)
...{
cmd.Parameters["@photo"].Value = DBNull.Value;
}
else
...{
try
...{
MemoryStream ms = new MemoryStream();
im.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] myData = new Byte[ms.Length];
ms.Position = 0;
ms.Read(myData, 0, Convert.ToInt32(ms.Length));
cmd.Parameters["@photo"].Value = myData;
}
catch (System.Exception ee)
...{
MessageBox.Show(ee.Message);
}
}//else
cmd.ExecuteNonQuery();
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
namespace WindowsApplication2
...{
public partial class Form1 : Form
...{
public OleDbConnection dbconn = new OleDbConnection();
public OleDbCommand comm = new OleDbCommand();
public Form1()
...{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
...{
this.OpenConn();
this.pictureBox1.Image = null;
Image im;
this.getPhoto("0000", out im);
this.pictureBox1.Image = im;
}
/**//// <summary>
/// 从数据库中读取图象
/// </summary>
/// <param name="pid"></param>
/// <param name="im"></param>
public void getPhoto(string pid, out Image im)
...{
//读取图象
im = null;
OleDbDataReader dr = SelectDR("select top 1 photo from photo where photoid='" + pid + "'");
if (dr.HasRows)
...{
dr.Read();
if (dr[0] != DBNull.Value)
...{
try
...{
Byte[] byteBLOBData = new Byte[0];
byteBLOBData = (Byte[])dr[0];
MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
im = Image.FromStream(stmBLOBData);
}
catch (Exception ex)
...{
MessageBox.Show(ex.Message);
}
}
else
...{
im = null;
}
}
dr.Close();
}
/**//// <summary>
/// 查询
/// </summary>
/// <param name="SqlStr"></param>
/// <returns></returns>
public OleDbDataReader SelectDR(string SqlStr)
...{
OleDbCommand cmd = new OleDbCommand(SqlStr, dbconn );
return cmd.ExecuteReader();
}
/**//// <summary>
/// 打开数据库连接。
/// </summary>
public void OpenConn()
...{
if (dbconn.State.ToString() == "Closed")
...{
//DBConn.ConnectionString=ConfigurationSettings.AppSettings["constring"]; //."user id=sa;data source=127.0.0.1;persist security info=True;initial catalog=PPF;password=7388";
dbconn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=" + Application.StartupPath + @"dataphoto.mdb;User ID=Admin;Password=";
try
...{
dbconn.Open();
}
catch
...{
MessageBox.Show("数据库连接失败");
}
}
}
private void button1_Click(object sender, EventArgs e)
...{
savePhoto("0000", this.pictureBox1.Image);
}
/**//// <summary>
/// 保存图象到数据库
/// </summary>
/// <param name="pid"></param>
/// <param name="im"></param>
public void savePhoto(string pid, Image im)
...{
OleDbCommand cmd;
OleDbDataReader dr = SelectDR("select photoid from photo where photoID='" + pid + "'");
if (dr.HasRows) //is exist
...{
cmd = new OleDbCommand("Update photo set photoID=@pid, photo=@photo where photoID='" + pid + "'", dbconn );
}
else // not exist
...{
cmd = new OleDbCommand("Insert Into photo (photoID,photo) Values(@pid,@photo) ", dbconn );
}
dr.Close();
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue ("@pid", pid);
cmd.Parameters.AddWithValue ("@photo", DBNull.Value);
//将图片保存到数据库中
if (im == null)
...{
cmd.Parameters["@photo"].Value = DBNull.Value;
}
else
...{
try
...{
MemoryStream ms = new MemoryStream();
im.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] myData = new Byte[ms.Length];
ms.Position = 0;
ms.Read(myData, 0, Convert.ToInt32(ms.Length));
cmd.Parameters["@photo"].Value = myData;
}
catch (System.Exception ee)
...{
MessageBox.Show(ee.Message);
}
}//else
cmd.ExecuteNonQuery();
}
}
}
- 读写数据库图象字段
- 如何对数据库中的图象数据进行读写?
- 读写SQL数据库Image字段
- Oracle数据库Date类型字段读写经验
- 如何读写Oracle数据库Blog字段(Java)
- C#读写数据库Image字段实例
- c#读写MSSQL数据库image字段内容
- C#读写数据库Image字段实例
- PCX 图象文件格式的读写
- 数据库中的图象存取
- .net C#,access,MDB数据库BLOB二进制字段的读写
- c++builder对数据库中BLOB字段读写
- C#读写Access数据库的OLE对象字段
- Delphi 实现文件从Oralce数据库 Blob 字段的读写
- 使用MYSQLCPPCONN连接MYSQL数据库与读写BLOB字段
- 使用DataGrid显示来自于数据库的图象
- 使用DataGrid显示来自于数据库的图象
- 也来谈谈数据库中的图象存取
- C# 心跳定时器
- 连接SQL,并写入文本文件
- ASP.NET中Cookie编程的基础知识-什么是 Cookie
- cookie对象的使用
- C版IsNumric()
- 读写数据库图象字段
- ASP教程:Global.asa文件的使用及Chat程序
- 男人三十岁。。。。。。。。。
- asp对象cookie应用集合
- 汇编语言rep movsd
- Cookie的使用
- 什么是Session
- JavaScript使用面向对象思想处理cookie
- 如果在sql server2000里访问oracle