////////////////////////////////////////////////////////////////////////////////
//author: stardicky //
//e-mail: [email protected] //
//qqnumber: 9531511 //
//companyname: ezone international //
//class: hbs-0308 //
//title: 如何在 sql server2000 中保存保存圖像及讀取圖像信息 //
////////////////////////////////////////////////////////////////////////////////
//注意:運(yùn)行這實(shí)例的之前需要在查詢分析器中執(zhí)行下列sql語(yǔ)句: //
//create database ezonedb //
//go //
//use ezonedb //
//go //
//create table [images] ( //
//[imageid] [int] identity (1, 1) not null , //
//[description] [nvarchar] (100) collate sql_latin1_general_cp1_ci_as null , //
//[imagefile] [image] null , //
//[imagesize] [int] null , //
//[imagetype] [nvarchar] (100) collate sql_latin1_general_cp1_ci_as null //
//) on [primary] textimage_on [primary] //
//go //
////////////////////////////////////////////////////////////////////////////////
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
using system.data.sqlclient;
using system.data.sqltypes;
using system.io;
namespace ezoneimageinsql
{
public class frmmain : system.windows.forms.form
{
private system.windows.forms.groupbox groupbox1;
private system.windows.forms.groupbox groupbox2;
private system.data.sqlclient.sqlconnection mysqlcon;
private system.windows.forms.label labfilepath;
private system.windows.forms.textbox txtfilepath;
private system.windows.forms.button btnupload;
private system.windows.forms.picturebox imgpicturebox;
private system.windows.forms.button btndownload;
private system.windows.forms.label labdescription;
private system.windows.forms.textbox txtimage;
private system.componentmodel.container components = null;
public frmmain()
{
initializecomponent();
}
protected override void dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
private void initializecomponent()
{
this.labfilepath = new system.windows.forms.label();
this.groupbox1 = new system.windows.forms.groupbox();
this.txtfilepath = new system.windows.forms.textbox();
this.btnupload = new system.windows.forms.button();
this.groupbox2 = new system.windows.forms.groupbox();
this.imgpicturebox = new system.windows.forms.picturebox();
this.btndownload = new system.windows.forms.button();
this.mysqlcon = new system.data.sqlclient.sqlconnection();
this.labdescription = new system.windows.forms.label();
this.txtimage = new system.windows.forms.textbox();
this.groupbox1.suspendlayout();
this.groupbox2.suspendlayout();
this.suspendlayout();
//
// labfilepath
//
this.labfilepath.location = new system.drawing.point(8, 72);
this.labfilepath.name = "labfilepath";
this.labfilepath.size = new system.drawing.size(80, 16);
this.labfilepath.tabindex = 0;
this.labfilepath.text = "文件路徑:";
this.labfilepath.textalign = system.drawing.contentalignment.middleleft;
//
// groupbox1
//
this.groupbox1.controls.add(this.txtimage);
this.groupbox1.controls.add(this.labdescription);
this.groupbox1.controls.add(this.btnupload);
this.groupbox1.controls.add(this.txtfilepath);
this.groupbox1.controls.add(this.labfilepath);
this.groupbox1.location = new system.drawing.point(8, 8);
this.groupbox1.name = "groupbox1";
this.groupbox1.size = new system.drawing.size(280, 128);
this.groupbox1.tabindex = 1;
this.groupbox1.tabstop = false;
this.groupbox1.text = "文件上傳給數(shù)據(jù)庫(kù)";
//
// txtfilepath
//
this.txtfilepath.location = new system.drawing.point(72, 64);
this.txtfilepath.name = "txtfilepath";
this.txtfilepath.size = new system.drawing.size(200, 21);
this.txtfilepath.tabindex = 1;
this.txtfilepath.text = "";
//
// btnupload
//
this.btnupload.location = new system.drawing.point(192, 96);
this.btnupload.name = "btnupload";
this.btnupload.tabindex = 2;
this.btnupload.text = "開(kāi)始上傳";
this.btnupload.click += new system.eventhandler(this.btnupload_click);
//
// groupbox2
//
this.groupbox2.controls.add(this.btndownload);
this.groupbox2.controls.add(this.imgpicturebox);
this.groupbox2.location = new system.drawing.point(8, 144);
this.groupbox2.name = "groupbox2";
this.groupbox2.size = new system.drawing.size(280, 200);
this.groupbox2.tabindex = 2;
this.groupbox2.tabstop = false;
this.groupbox2.text = "從數(shù)據(jù)庫(kù)下載圖像文件并預(yù)覽圖像";
//
// imgpicturebox
//
this.imgpicturebox.location = new system.drawing.point(16, 24);
this.imgpicturebox.name = "imgpicturebox";
this.imgpicturebox.size = new system.drawing.size(168, 160);
this.imgpicturebox.sizemode = system.windows.forms.pictureboxsizemode.stretchimage;
this.imgpicturebox.tabindex = 0;
this.imgpicturebox.tabstop = false;
//
// btndownload
//
this.btndownload.location = new system.drawing.point(192, 24);
this.btndownload.name = "btndownload";
this.btndownload.tabindex = 1;
this.btndownload.text = "開(kāi)始下載";
this.btndownload.click += new system.eventhandler(this.btndownload_click);
//
// mysqlcon 數(shù)據(jù)庫(kù)的連接對(duì)象
//
this.mysqlcon.connectionstring = "integrated security=sspi;data source=.;persist security info=false;initial catalog=ezonedb";
//
// labdescription
//
this.labdescription.location = new system.drawing.point(8, 32);
this.labdescription.name = "labdescription";
this.labdescription.size = new system.drawing.size(88, 16);
this.labdescription.tabindex = 3;
this.labdescription.text = "文件描述信息:";
this.labdescription.textalign = system.drawing.contentalignment.middleleft;
//
// txtimage
//
this.txtimage.location = new system.drawing.point(96, 24);
this.txtimage.name = "txtimage";
this.txtimage.size = new system.drawing.size(176, 21);
this.txtimage.tabindex = 4;
this.txtimage.text = "億眾國(guó)際 ezone international";
//
// frmmain
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(292, 349);
this.controls.add(this.groupbox2);
this.controls.add(this.groupbox1);
this.name = "frmmain";
this.text = "ezone international - 億眾國(guó)際";
this.groupbox1.resumelayout(false);
this.groupbox2.resumelayout(false);
this.resumelayout(false);
}
[stathread]
static void main()
{
application.run(new frmmain());
}
private void btnupload_click(object sender, system.eventargs e)
{
try
{
//用文件的路徑信息創(chuàng)建一個(gè)文件對(duì)象
fileinfo mypicture=new fileinfo(this.txtfilepath.text);
//檢查文件的存在性
if(!mypicture.exists)
{
messagebox.show("文件不存在!");
}
else
{
//檢查文件的擴(kuò)展名,如果不符合要求就返回
switch(mypicture.extension.toupper())
{
case ".jpg":this.uploadimagefile(mypicture);break;
case ".gif":this.uploadimagefile(mypicture);break;
case ".bmp":this.uploadimagefile(mypicture);break;
default:messagebox.show("文件類(lèi)型不正確!");break;
}
}
}
catch
{
messagebox.show("文件路徑信息不正確!");
}
}
/// <summary>
/// 向數(shù)據(jù)庫(kù)寫(xiě)入圖像文件
/// </summary>
private void uploadimagefile(fileinfo obj)
{
try
{
//創(chuàng)建一字節(jié)數(shù)組,用來(lái)存儲(chǔ)圖像文件.(數(shù)組的長(zhǎng)度是圖像文件的長(zhǎng)度)
byte[] content=new byte[obj.length];
//打開(kāi)文件并用他初始化一個(gè)文件流對(duì)象
filestream imagefilestream=obj.openread();
//將文件內(nèi)容寫(xiě)入字節(jié)數(shù)組
imagefilestream.read(content,0,content.length);
//關(guān)閉文件流
imagefilestream.close();
//創(chuàng)建一個(gè)sql命令對(duì)象,用來(lái)插入記錄
sqlcommand command=new sqlcommand("insert into images(description,imagefile,imagesize,imagetype) values(@description,@imagefile,@imagesize,@imagetype)",this.mysqlcon);
//圖像的描述信息
sqlparameter imagedescriptionparameter=new sqlparameter("@description",sqldbtype.nvarchar);
imagedescriptionparameter.value=this.txtimage.text;
command.parameters.add(imagedescriptionparameter);
//圖像的數(shù)據(jù)字節(jié)數(shù)組
sqlparameter imagefileparameter=new sqlparameter("@imagefile",sqldbtype.image);
imagefileparameter.value=content;
command.parameters.add(imagefileparameter);
//圖像的大小信息
sqlparameter imagesizeparameter=new sqlparameter("@imagesize",sqldbtype.int);
imagesizeparameter.value=content.length;
command.parameters.add(imagesizeparameter);
//圖像的類(lèi)型新信息
sqlparameter imagetypeparameter=new sqlparameter("@imagetype",sqldbtype.nvarchar);
imagetypeparameter.value=obj.extension;
command.parameters.add(imagetypeparameter);
//打開(kāi)數(shù)據(jù)庫(kù)連接
this.mysqlcon.open();
//執(zhí)行 sql 語(yǔ)句
command.executenonquery();
//關(guān)閉數(shù)據(jù)庫(kù)連接
this.mysqlcon.close();
messagebox.show("圖像文件 " + obj.fullname + " 成功上傳到數(shù)據(jù)庫(kù)!");
}
catch(exception ezone)
{
messagebox.show(ezone.message);
}
}
/// <summary>
/// 從數(shù)據(jù)庫(kù)讀取圖像
/// </summary>
private void btndownload_click(object sender, system.eventargs e)
{
try
{
//創(chuàng)建一個(gè)sql命令對(duì)象,用來(lái)查詢記錄
sqlcommand command=new sqlcommand("select * from images order by imageid desc",this.mysqlcon);
//打開(kāi)數(shù)據(jù)庫(kù)連接
this.mysqlcon.open();
//執(zhí)行sql語(yǔ)句
sqldatareader myreader=command.executereader(commandbehavior.closeconnection);
myreader.read();
//創(chuàng)建一個(gè)內(nèi)存流對(duì)象并用imagefile字段,imagesize字段初始化他
memorystream mymemorystream=new memorystream((byte[])myreader["imagefile"],0,convert.toint32(myreader["imagesize"]));
//創(chuàng)建一個(gè) image 對(duì)象,并賦值給 picture對(duì)象的 image 屬性
this.imgpicturebox.image=image.fromstream(mymemorystream);
//關(guān)閉內(nèi)存流
mymemorystream.close();
myreader.close();
}
catch(exception ezone)
{
messagebox.show(ezone.message);
}
}
}
}
菜鳥(niǎo)學(xué)堂: