转载的,但原文出处不知道在哪儿了
上面是主界面
上面是显示界面
数据库为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", SqlDbType.Image, 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", SqlDbType.Int).Value = 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(); } } }