c#读取access数据库里的图片 怎么读取,大概的语句是什么用什么数据结构存储图片比较好 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 数据库使用 binary 类型。程序ado.stream读取。 access里叫OLE对象相当于sql的image/binary 看错了,不好意思。以为是VB呢。C#就更简单了,直接用Stream读取以后传给Bitmap对象就可以了。具体代码Google之。 FileStream读读成二进制流byte[]接受二进制流存byte【】进数据库数据库各使用image或binary都可以 pictureBox1.Image要怎么显示。。 string sql = "Select photo From Tb Where ID='1'"; OleDbConnection conn = new OleDbConnection(strConn ); conn.Open(); OleDbCommand comm = new OleDbCommand(sql, conn); OleDbDataReader sdr = comm.ExecuteReader(); sdr.Read(); MemoryStream ms = new MemoryStream((byte[])sdr[0]); Image image = Image.FromStream(ms); sdr.Close(); conn.Close(); pictureBox1.Image = image; 用Ole对象打开Access,将字段值取出后放到Byte数组中去,然后将Byte数组转换成文件流付给你的图片框就可以了。 using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Data.OleDb;using System.Data.SqlClient;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO;namespace WindowsFormsApplication1{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { pictureBox1.Image = ConMdb(); } //读取Access数据库中的图片 private Image ConMdb() { DataSet ds = new DataSet(); //定义连接加密ACCESS数据库的字符串,数据库密码为xiangyu string strFilePath = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Application.StartupPath + "\\OEMInfo.mdb;Jet OLEDB:Database Password=xiangyu"; //定义连接数据源信息数据表为Info,其中“标识”项为Ole类型,存储的为数据。 string sql = "select * from Info"; OleDbConnection con = new OleDbConnection(strFilePath); OleDbDataAdapter da = new OleDbDataAdapter(sql, con); try { da.Fill(ds); } catch (Exception ex) { throw new Exception(ex.ToString()); } finally { con.Close(); con.Dispose(); da.Dispose(); } Image image = null; try { MemoryStream ms = new MemoryStream((byte[])ds.Tables[0].Rows[0]["标识"]); image = Image.FromStream(ms); } catch (Exception ex) { //image = image.Dispose(); //throw new Exception(ex.ToString()); } return 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.IO;using System.Data.OleDb;namespace WindowsApplication1{ public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); if (ofd.ShowDialog() == DialogResult.OK) { label1.Text = ofd.FileName; this.pictureBox1.Image = Image.FromFile(label1.Text); } } OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\student.mdb"); DataSet ds = new DataSet(); private void button2_Click(object sender, EventArgs e) { FileStream fs = new FileStream(label1.Text, FileMode.Open, FileAccess.Read); byte[] data = new byte[fs.Length]; fs.Read(data, 0, data.Length); fs.Close(); OleDbCommand command = new OleDbCommand(); command.CommandText = "insert into pictureTab values(@id,@picture)"; command.Parameters.AddWithValue("@id", textBox1.Text); command.Parameters.AddWithValue("@picture", data); command.Connection = conn; try { conn.Open(); int i = command.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { conn.Close(); command = null; } } private void button3_Click(object sender, EventArgs e) { OleDbCommand command = new OleDbCommand(); command.CommandText = "select picture from pictureTab where (id = @id)"; command.Parameters.AddWithValue("@id", textBox1.Text); command.Connection = conn; OleDbDataAdapter adapter = new OleDbDataAdapter(command); try { //ds = new DataSet(); conn.Open(); adapter.Fill(ds, "picture"); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { conn.Close(); command = null; adapter = null; } byte[] data = (byte[])ds.Tables["picture"].Rows[0]["picture"]; using (MemoryStream memStream = new MemoryStream(data)) { this.pictureBox1.Image = Image.FromStream(memStream); ds.Tables.Clear(); } } }}/*int id = int.Parse(textBox1.Text);command = new OleDbCommand();command.CommandText = "select picture from ?? where(id=@id)";command.Parameters.AddWithValue("@id", id);command.Connection = conn;conn.Open();OleDbDataReader reader = command.ExecuteReader();reader.Read();byte[] data = (byte[])reader["picture"];MemoryStream mem = new MemoryStream(data);pictureBox1.Image = Image.FromStream(mem);conn.Close();*/ WCF服务不定期错误 C# 获取GPS返回数据 如何将用户的各种信息以链表的方式存储并写入text文件中,C# 请问。。。。。 急!子窗体设置层叠,水平排列问题? 如何在splitcontainer的分隔线上放置控件? ASP.net与JavaScript或是VBScript交互 问个问题,在 .NET 中有没支持 RSA, 从而使用某些输入生成 公匙 和 私钥 的类? 望指教. 文件读写问题, 请教各位大虾:vs上的编译问题 如何自定义打印机纸张大小 C#中TEXTBOX属性PasswordChar 只要设置字符就会出现 外部组件发生异常
程序ado.stream读取。
具体代码Google之。
byte[]接受二进制流
存byte【】进数据库
数据库各使用image或binary都可以
OleDbConnection conn = new OleDbConnection(strConn );
conn.Open();
OleDbCommand comm = new OleDbCommand(sql, conn);
OleDbDataReader sdr = comm.ExecuteReader();
sdr.Read();
MemoryStream ms = new MemoryStream((byte[])sdr[0]);
Image image = Image.FromStream(ms);
sdr.Close();
conn.Close();
pictureBox1.Image = image;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;using System.IO;namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Image = ConMdb();
}
//读取Access数据库中的图片
private Image ConMdb()
{
DataSet ds = new DataSet();
//定义连接加密ACCESS数据库的字符串,数据库密码为xiangyu
string strFilePath = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Application.StartupPath + "\\OEMInfo.mdb;Jet OLEDB:Database Password=xiangyu";
//定义连接数据源信息数据表为Info,其中“标识”项为Ole类型,存储的为数据。
string sql = "select * from Info";
OleDbConnection con = new OleDbConnection(strFilePath);
OleDbDataAdapter da = new OleDbDataAdapter(sql, con);
try
{
da.Fill(ds);
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
finally
{
con.Close();
con.Dispose();
da.Dispose();
}
Image image = null;
try
{
MemoryStream ms = new MemoryStream((byte[])ds.Tables[0].Rows[0]["标识"]);
image = Image.FromStream(ms);
}
catch (Exception ex)
{
//image = image.Dispose();
//throw new Exception(ex.ToString());
}
return 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.IO;
using System.Data.OleDb;namespace WindowsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
label1.Text = ofd.FileName;
this.pictureBox1.Image = Image.FromFile(label1.Text);
}
} OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\student.mdb");
DataSet ds = new DataSet();
private void button2_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream(label1.Text, FileMode.Open, FileAccess.Read);
byte[] data = new byte[fs.Length];
fs.Read(data, 0, data.Length);
fs.Close(); OleDbCommand command = new OleDbCommand();
command.CommandText = "insert into pictureTab values(@id,@picture)";
command.Parameters.AddWithValue("@id", textBox1.Text);
command.Parameters.AddWithValue("@picture", data);
command.Connection = conn;
try
{
conn.Open();
int i = command.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
conn.Close();
command = null;
}
} private void button3_Click(object sender, EventArgs e)
{
OleDbCommand command = new OleDbCommand();
command.CommandText = "select picture from pictureTab where (id = @id)";
command.Parameters.AddWithValue("@id", textBox1.Text);
command.Connection = conn;
OleDbDataAdapter adapter = new OleDbDataAdapter(command);
try
{
//ds = new DataSet();
conn.Open();
adapter.Fill(ds, "picture");
}
catch (Exception ex)
{ MessageBox.Show(ex.ToString()); }
finally
{
conn.Close();
command = null;
adapter = null;
} byte[] data = (byte[])ds.Tables["picture"].Rows[0]["picture"];
using (MemoryStream memStream = new MemoryStream(data))
{
this.pictureBox1.Image = Image.FromStream(memStream);
ds.Tables.Clear();
}
}
}
}
/*int id = int.Parse(textBox1.Text);
command = new OleDbCommand();
command.CommandText = "select picture from ?? where(id=@id)";
command.Parameters.AddWithValue("@id", id);
command.Connection = conn;
conn.Open();
OleDbDataReader reader = command.ExecuteReader();
reader.Read();
byte[] data = (byte[])reader["picture"];
MemoryStream mem = new MemoryStream(data);
pictureBox1.Image = Image.FromStream(mem);
conn.Close();*/