本文总结如何在Net Winform和Net webform()中将图片存入sqlserver中并读取显示的方法
使用将图片上传并存入SqlServer中然后从SqlServer中读取并显示出来
一上传并存入SqlServer
数据库结构
create table test
{
id identity()
FImage image
}
相关的存储过程
Create proc UpdateImage
(
@UpdateImage Image
)
As
Insert Into test(FImage) values(@UpdateImage)
GO
在UpPhotoaspx文件中添加如下:
<input id=UpPhoto name=UpPhoto runat=server type=file>
<asp:Button id=btnAdd name=btnAdd runat=server Text=上传></asp:Button>
然后在后置代码文件UpPhotoaspxcs添加btnAdd按钮的单击事件处理代码:
private void btnAdd_Click(object sender SystemEventArgs e)
{
//获得图象并把图象转换为byte[]
HttpPostedFile upPhoto=UpPhotoPostedFile;
int upPhotoLength=upPhotoContentLength;
byte[] PhotoArray=new Byte[upPhotoLength];
Stream PhotoStream=upPhotoInputStream;
PhotoStreamRead(PhotoArrayupPhotoLength);
//连接数据库
SqlConnection conn=new SqlConnection();
connConnectionString=Data Source=localhost;Database=test;User Id=sa;Pwd=sa;
SqlCommand cmd=new SqlCommand(UpdateImageconn);
cmdCommandType=CommandTypeStoredProcedure;
cmdParametersAdd(@UpdateImageSqlDbTypeImage);
cmdParameters[@UpdateImage]Value=PhotoArray;
//如果你希望不使用存储过程来添加图片把上面四句代码改为
//string strSql=Insert into test(FImage) values(@FImage);
//SqlCommand cmd=new SqlCommand(strSqlconn);
//cmdParametersAdd(@FImageSqlDbTypeImage);
//cmdParameters[@FImage]Value=PhotoArray;
connOpen();
cmdExecuteNonQuery();
connClose();
}
二从SqlServer中读取并显示出来
在需要显示图片的地方添加如下代码:
<asp:image id=imgPhoto runat=server ImageUrl=ShowPhotoaspx></asp:image>
ShowPhotoaspx主体代码
private void Page_Load(object sender SystemEventArgs e)
{
if(!PageIsPostBack)
{
SqlConnection conn=new SqlConnection()
connConnectionString=Data Source=localhost;Database=test;User Id=sa;Pwd=sa;
string strSql=select * from test where id=;//这里假设获取id为的图片
SqlCommand cmd=new SqlCommand()
readerRead();
ResponseContentType=application/octetstream;
ResponseBinaryWrite((Byte[])reader[FImage]);
ResponseEnd();
readerClose();
}
}
在winform中将图片存入sqlserver并从sqlserver中读取并显示在picturebox中
存入sqlserver
数据库结构和使用的存储过过程同上面的一样
在窗体中加一个OpenFileDialog控件命名为ofdSelectPic
在窗体上添加一个打开文件按钮添加如下单击事件代码
Stream ms;
byte[] picbyte;
//ofdSelectPicShowDialog();
if (ofdSelectPicShowDialog()==DialogResultOK)
{
if ((ms=ofdSelectPicOpenFile())!=null)
{
//MessageBoxShow(ok);
picbyte=new byte[msLength];
msPosition=;
msRead(picbyteConvertToInt(msLength));
//MessageBoxShow(读取完毕!);
//连接数据库
SqlConnection conn=new SqlConnection();
connConnectionString=Data Source=localhost;Database=test;User Id=sa;Pwd=sa;
SqlCommand cmd=new SqlCommand(UpdateImageconn);
cmdCommandType=CommandTypeStoredProcedure;
cmdParametersAdd(@UpdateImageSqlDbTypeImage);
cmdParameters[@UpdateImage]Value=picbyte;
connOpen();
cmdExecuteNonQuery();
connClose();
msClose();
}
}
读取并显示在picturebox中
添加一个picturebox名为ptbShow
添加一个按钮添加如下响应事件
SqlConnection conn=new SqlConnection();
connConnectionString=Data Source=localhost;Database=test;User Id=sa;Pwd=sa;
string strSql=select FImage from test where id=;
SqlCommand cmd=new SqlCommand(strSqlconn);
connOpen();
SqlDataReader reader=cmdExecuteReader();
readerRead();
MemoryStream ms=new MemoryStream((byte[])reader[FImage]);
Image image=ImageFromStream(mstrue);
readerClose();
connClose();
ptbShowImage=image;