您好,欢迎来到步遥情感网。
搜索
您的当前位置:首页图片二进制存数据库

图片二进制存数据库

来源:步遥情感网
查看文章 C#如何在SQLSERVER数据库中存取图片 2008年06月16日 星期一 14:47 //放入数据库

if(this.openFileDialog1.ShowDialog()==DialogResult.OK) {

this.pictureBox1.Image = Image.FromFile(openFileDialog1.FileName); SqlConnection conn = new

SqlConnection(\"server=(local);uid=sa;pwd=sa;database=ac\"); conn.Open();

SqlCommand cmd = new SqlCommand(\"insert into xia (names) values (@i)\conn);

byte[] ib = new Byte[6000000];

FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);

fs.Read(ib, 0, 6000000);

cmd.Parameters.Add(\"@i\cmd.Parameters[\"@i\"].Value = ib; cmd.ExecuteNonQuery(); conn.Close(); }

//从数据库中读取 try {

System.Data.SqlClient.SqlConnection conn = new SqlConnection(connstring); conn.Open();

string sql = \"select ImageNum from image where ImageName=@name\"; SqlCommand comm = new SqlCommand(sql, conn);

string strname = listBox1.SelectedItem.ToString();

comm.Parameters.Add(\"@name\System.Data.SqlClient.SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = comm;

System.Data.DataSet dataset = new DataSet();

adapter.SelectCommand.ExecuteNonQuery(); adapter.Fill(dataset, \"image\");

byte[] b = (byte[])dataset.Tables[0].Rows[0][\"ImageNum\"]; if (b.Length > 0) {

MemoryStream stream = new MemoryStream(b, true); stream.Write(b, 0, b.Length);

System.Drawing.Bitmap bitmap = new Bitmap(stream); imageshow.Image = bitmap;

imageshow.Left = 700; imageshow.Width = 300; imageshow.Height = 300; stream.Close(); }

conn.Close(); }

catch (Exception) {

MessageBox.Show(\"你可能没选择图片,请选择\"); }

在论坛中碰到有朋友问到如和把图片存到数据库中,自己用VS2005+SQLServer2005实现了这个功能.

上面是主界面

上面是显示界面

数据库为Picture,数据表为Picture,表结构设计如下所示:

PictureID int 4, PictureContent Image, PictureText nvarchar(50) 下面是主界面的代码

namespace PictureToDataBase {

public partial class Main : Form {

string fileSaveURL; public Main() {

InitializeComponent(); }

private void cmdOpen_Click(object sender, EventArgs e) {

this.openFileDialog.ShowDialog();

string fileURL = this.openFileDialog.FileName; this.picView.ImageLocation = fileURL; this.fileSaveURL = fileURL;

}

private void cmdSave_Click(object sender, EventArgs e) { //获取图片的二进制流

FileStream fs = new FileStream(fileSaveURL, FileMode.Open); BinaryReader br = new BinaryReader(fs); byte[] photo = br.ReadBytes((int)fs.Length); br.Close(); fs.Close();

//把图片写到数据库中

string conn = @\"Data Source=JNITDEV\\SQLEXPRESS;Initial Catalog=Picture;Integrated Security=True\";

using (SqlConnection sqlConn = new SqlConnection(conn)) {

SqlCommand sqlComm = new SqlCommand(); sqlConn.Open();

sqlComm.Connection = sqlConn;

sqlComm.CommandText = \"INSERT INTO Picture (PictureContent, PictureText) VALUES (@Picture,'Test')\"; sqlComm.CommandType = CommandType.Text;

sqlComm.Parameters.Add(\"@Picture\photo.Length).Value = photo;

sqlComm.ExecuteNonQuery(); } }

private void cmdShow_Click(object sender, EventArgs e) {

PicShow picShow = new PicShow(); picShow.Show(); } } }

下面是显示界面代码

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.SqlClient;

namespace PictureToDataBase {

public partial class PicShow : Form {

int picID; int maxID; int minID;

string conn = @\"Data Source=JNITDEV\\SQLEXPRESS;Initial Catalog=Picture;Integrated Security=True\"; public PicShow() {

InitializeComponent(); }

private void PicShow_Load(object sender, EventArgs e) {

using (SqlConnection sqlConn = new SqlConnection(conn)) {

SqlCommand sqlComm = new SqlCommand();

sqlConn.Open();

sqlComm.Connection = sqlConn;

sqlComm.CommandText = \"SELECT TOP 1

PictureContent,PictureID FROM Picture ORDER BY PictureID DESC\"; sqlComm.CommandType = CommandType.Text;

using (SqlDataReader dr = sqlComm.ExecuteReader()) {

dr.Read();

MemoryStream ms = new MemoryStream((byte[])dr[0]); Image img = Image.FromStream(ms); this.picShowPic.Image = img; this.picID = (int)dr[1];

}

SetButton(); } }

private void SetButton() {

using (SqlConnection sqlConn = new SqlConnection(conn)) {

SqlCommand sqlComm = new SqlCommand();

sqlConn.Open();

sqlComm.Connection = sqlConn;

sqlComm.CommandText = \"SELECT MAX(PictureID) AS maxID,MIN(PictureID) AS minID FROM Picture\";

sqlComm.CommandType = CommandType.Text;

using (SqlDataReader dr = sqlComm.ExecuteReader()) {

dr.Read();

maxID = (int)dr[0]; minID = (int)dr[1]; } }

this.cmdPreview.Enabled = picID > minID; this.cmdNext.Enabled = picID < maxID; }

private void cmdNext_Click(object sender, EventArgs e) {

this.picID++; LoadPicture(); SetButton(); }

private void LoadPicture() {

using (SqlConnection sqlConn = new SqlConnection(conn)) {

SqlCommand sqlComm = new SqlCommand(); sqlConn.Open();

sqlComm.Connection = sqlConn;

sqlComm.CommandText = \"SELECT PictureContent,PictureID FROM Picture WHERE PictureID = @picID\";

sqlComm.CommandType = CommandType.Text;

sqlComm.Parameters.Add(\"@picID\= picID;

using (SqlDataReader dr = sqlComm.ExecuteReader()) {

dr.Read();//以下把数据库中读出的Image流在图片框中显示出来.

MemoryStream ms = new MemoryStream((byte[])dr[0]); Image img = Image.FromStream(ms); this.picShowPic.Image = img; this.picID = (int)dr[1]; } } }

private void cmdPreview_Click(object sender, EventArgs e) {

this.picID--; LoadPicture(); SetButton(); } } }

//图片保存到数据库中

public void AddPicture(string lastName, string firstName, string title, DateTime hireDate, int reportsTo, string photoFilePath, string connectionString) {

byte[] photo = GetPhoto(photoFilePath);

SqlConnection connection = new SqlConnection(connectionString)

SqlCommand command = new SqlCommand(\"INSERT INTO Employees (LastName, FirstName,Title,HireDate,ReportsTo, Photo) Values(@LastName, @FirstName, @Title, @HireDate, @ReportsTo, @Photo)\

command.Parameters.Add(\"@LastName\ SqlDbType.NVarChar, 20).Value = lastName; command.Parameters.Add(\"@FirstName\ command.Parameters.Add(\"@Title\

command.Parameters.Add(\"@HireDate\ command.Parameters.Add(\"@ReportsTo\ SqlDbType.Int).Value = reportsTo;

command.Parameters.Add(\"@Photo\

connection.Open();

command.ExecuteNonQuery(); }

// 读二进制流的方法。

public static byte[] GetPhoto(string filePath) {

FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read); BinaryReader reader = new BinaryReader(stream);

byte[] photo = reader.ReadBytes((int)stream.Length);

reader.Close(); stream.Close();

return photo; }

//从数据库读图片到picturebox public void ReaderPicture() {

SqlConnection conn=new SqlConnectio(@\"data source=chenyuming2004VSdotNET;uid=sa;pwd=cym;database=lhf\"); conn.Open();

SqlCommand cmd=new SqlCommand(\"select 照片 from fuser where password='1b'\SqlDataReader reader=cmd.ExecuteReader(); reader.Read();

MemoryStream buf=new MemoryStream((byte[])reader[0]); Image image=Image.FromStream(buf,true);

pictureBox1.Image=image;

pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;//拉伸图片 }

本文来自CSDN博客,转载请标明http://blog.csdn.net/sx811125/archive/2009/08/04/4408016.aspx

try {

SqlConnection cn = new SqlConnection(strCn); cn.Open();

//Retrieve BLOB from database into DataSet.

出处:

SqlCommand cmd = new SqlCommand(\"SELECT BLOBID, BLOBData FROM BLOBTest ORDER BY BLOBID\ cn);

SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds, \"BLOBTest\");

int c = ds.Tables[\"BLOBTest\"].Rows.Count; if(c>0)

{ //BLOB is read into Byte array, then used to construct MemoryStream, //then passed to PictureBox.

Byte[] byteBLOBData = new Byte[0];

byteBLOBData = (Byte[])(ds.Tables[\"BLOBTest\"].Rows[c - 1][\"BLOBData\"]); MemoryStream stmBLOBData = new MemoryStream(byteBLOBData); pictureBox1.Image= Image.FromStream(stmBLOBData); } cn.Close(); }

catch(Exception ex)

{MessageBox.Show(ex.Message);}

----------------------------------------------------------------------------------------------- OleDbConnection conn=new OleDbConnection(strCon);

string sql=\"SELECT * FROM mana where E=\"+XXX; OleDbCommand command=new OleDbCommand(sql,conn);

try {

conn.Open(); } catch {

MessageBox.Show(\"不能打开数据联接!\") ; } OleDbDataReader dr=command.ExecuteReader(); if(dr.Read()) {

byte[] mydata=((byte[])dr[\"D\"]);

MemoryStream myStream=new MemoryStream(); foreach(byte a in mydata) {

myStream.WriteByte(a); }

Image Img=Image.FromStream(myStream); myStream.Close();

m_DisplayPort.SetImage(Img); try {}

catch(Exception ee) {

MessageBox.Show(ee.ToString()); } } else {

MessageBox.Show(\"没有成功读入数据!\") ; } conn.Close(); }

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- obuygou.com 版权所有 赣ICP备2024042798号-5

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务