您好,欢迎来到宝玛科技网。
搜索
您的当前位置:首页在C#中实现存储图片到SQLServer2005

在C#中实现存储图片到SQLServer2005

来源:宝玛科技网
 在C#中实现存储图片到SQLServer2005

新一篇: 安装Ms SQL Server 2005 开发版时出现性能计数器要求安装错误的解决办法

在论坛中碰到有朋友问到如和把图片存到数据库中,自己用VS2005+SQLServe

r2005实现了这个功能.

上面是主界面

上面是显示界面

数据库为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(); } } }

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

Copyright © 2019- baomayou.com 版权所有 赣ICP备2024042794号-6

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

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