欢迎光临
我们一直在努力

如何在 SQL Server2000 中保存图像及读取图像信息-.NET教程,数据库应用

建站超值云服务器,限时71元/月

////////////////////////////////////////////////////////////////////////////////

//author: stardicky //

//e-mail: stardicky@hotmail.com //

//qqnumber: 9531511 //

//companyname: ezone international //

//class: hbs-0308 //

//title: 如何在 sql server2000 中保存保存图像及读取图像信息 //

////////////////////////////////////////////////////////////////////////////////

//注意:运行这实例的之前需要在查询分析器中执行下列sql语句: //

//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 = "文件上传给数据库";

//

// 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 = "开始上传";

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 = "从数据库下载图像文件并预览图像";

//

// 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 = "开始下载";

this.btndownload.click += new system.eventhandler(this.btndownload_click);

//

// mysqlcon 数据库的连接对象

//

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 = "亿众国际 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 – 亿众国际";

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

{

//用文件的路径信息创建一个文件对象

fileinfo mypicture=new fileinfo(this.txtfilepath.text);

//检查文件的存在性

if(!mypicture.exists)

{

messagebox.show("文件不存在!");

}

else

{

//检查文件的扩展名,如果不符合要求就返回

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("文件类型不正确!");break;

}

}

}

catch

{

messagebox.show("文件路径信息不正确!");

}

}

/// <summary>

/// 向数据库写入图像文件

/// </summary>

private void uploadimagefile(fileinfo obj)

{

try

{

//创建一字节数组,用来存储图像文件.(数组的长度是图像文件的长度)

byte[] content=new byte[obj.length];

//打开文件并用他初始化一个文件流对象

filestream imagefilestream=obj.openread();

//将文件内容写入字节数组

imagefilestream.read(content,0,content.length);

//关闭文件流

imagefilestream.close();

//创建一个sql命令对象,用来插入记录

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);

//图像的数据字节数组

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);

//图像的类型新信息

sqlparameter imagetypeparameter=new sqlparameter("@imagetype",sqldbtype.nvarchar);

imagetypeparameter.value=obj.extension;

command.parameters.add(imagetypeparameter);

//打开数据库连接

this.mysqlcon.open();

//执行 sql 语句

command.executenonquery();

//关闭数据库连接

this.mysqlcon.close();

messagebox.show("图像文件 " + obj.fullname + " 成功上传到数据库!");

}

catch(exception ezone)

{

messagebox.show(ezone.message);

}

}

/// <summary>

/// 从数据库读取图像

/// </summary>

private void btndownload_click(object sender, system.eventargs e)

{

try

{

//创建一个sql命令对象,用来查询记录

sqlcommand command=new sqlcommand("select * from images order by imageid desc",this.mysqlcon);

//打开数据库连接

this.mysqlcon.open();

//执行sql语句

sqldatareader myreader=command.executereader(commandbehavior.closeconnection);

myreader.read();

//创建一个内存流对象并用imagefile字段,imagesize字段初始化他

memorystream mymemorystream=new memorystream((byte[])myreader["imagefile"],0,convert.toint32(myreader["imagesize"]));

//创建一个 image 对象,并赋值给 picture对象的 image 属性

this.imgpicturebox.image=image.fromstream(mymemorystream);

//关闭内存流

mymemorystream.close();

myreader.close();

}

catch(exception ezone)

{

messagebox.show(ezone.message);

}

}

}

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 如何在 SQL Server2000 中保存图像及读取图像信息-.NET教程,数据库应用
分享到: 更多 (0)