在 C#中实现存储图片到 SQLServer2005 中实现存储图片到 新一篇: 新一篇 安装 Ms SQL Server 2005 开发版时出现性能计数器要求安装错误的解决办法 在论坛中碰到有朋友问到如和把图片存到数据库中,自己用 VS2005+SQLServe r2005 实现了这个功能. 上面是主界面 上面是显示界面 数据库为 Picture,数据表为 Picture,表结构设计如下所示: PictureID int 4, (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 Catalo g=Picture;Integrated Security=True"; using (SqlConnection sqlConn = new SqlConnection(conn)) { SqlCommand sqlComm = new SqlCommand(); PictureContent Image, PictureText nvarchar sqlConn.Open(); sqlComm.Connection = sqlConn; sqlComm.CommandText = "INSERT INTO Picture (PictureConten t, PictureText) VALUES (@Picture,'Test')"; sqlComm.CommandType = CommandType.Text; sqlComm.Parameters.Add("@Picture", SqlDbType.Image, pho to.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,Pictur eID 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,M IN(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 FR OM Picture WHERE PictureID = @picID"; sqlComm.CommandType = CommandType.Text; sqlComm.Parameters.Add("@picID", SqlDbType.Int).Value = picI D; 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(); } } }