net在sqlserver中的图片存取技术(例子在管理里inoutimg)

net在sqlserver中的图片存取技术(例子在管理里inoutimg),第1张

net在sqlserver中的图片存取技术(例子在管理里inoutimg),第2张

本文总结了如何在SQL Server中存储图片并读入图片。Net WinForm和。Net WebForm(asp.net)。
1。使用ASP.NET在SQL Server中上传并存储图片,然后从SQL Server中读取并显示:
1)上传并存储在SQL Server中
数据库结构
创建表测试
{
Id Identity (1,1),
fi image
}
相关存储过程
创建proc更新映像
(
@ update image
)[/br

然后在post代码文件UpPhoto.aspx.cs中添加btnAdd按钮的click事件处理代码:
private void BTNADD _ click(对象发送方,系统。eventargs e)
{
/获取图像并将其转换为byte[]
http posted file up photo =
int up photolength = up photo。ContentLength
Byte[]photo array = new Byte[upPhotoLength];
Stream photo Stream = up photo。InputStream
PhotoStream。Read(PhotoArray,0,upPhotoLength);
//连接到数据库
sqlconnectionconn = new sqlconnection();
conn . ConnectionString = " Data Source = localhost;数据库=测试;用户Id = sapwd = sa ";
SqlCommand cmd = new SqlCommand(" UpdateImage ",conn);
cmd。CommandType=CommandType。存储过程;
cmd。参数. Add("@UpdateImage ",SqlDbType。图片);
cmd。参数["@UpdateImage"]。Value = PhotoArray
//如果想不使用存储过程添加图片,将上述四段代码改为:
//string strSQL = " insert into test(f image)values(@ f image)";
//SqlCommand cmd = new SqlCommand(strSql,conn);
//cmd。参数. Add("@FImage ",SqlDbType。图片);
//cmd。参数["@FImage"]。Value = PhotoArray
conn . Open();
cmd。ExecuteNonQuery();
conn . Close();
}
2)从SQL Server中读取并显示它
在需要显示图片的地方添加以下代码:

ShowPhoto.aspx正文代码:
Private void page _ load(object sender,system . eventargs e)
{
if(!页面。IsPostBack)
{
SqlConnection conn = new SqlConnection()
conn . ConnectionString = " Data Source = localhost;数据库=测试;用户Id = sapwd = sa ";
string strSql = " select * from test where id = 2 ";//这里假设获得id为2的图像
SQLCommand cmd = New SQLCommand(strSQL,Conn);
conn . Open();
SqlDataReader reader=cmd。ExecuteReader();
读者。read();
响应。ContentType= "应用程序/八位字节流";
响应。binary write((Byte[])reader[" FImage "]);
响应。end();
读者。close();
}
}
2。在WinForm中,图片存储在SQL Server中,从SQL Server中读取并显示在picturebox中
1),存储在SQL Server中
数据库结构和存储过程,同上
首先,在窗体中
然后,在窗体上添加一个打开文件按钮,并添加下面的click事件代码:
Stream ms;
byte[]pic byte;
//ofdSelectPic。ShowDialog();
if(of select pic。ShowDialog()==DialogResult。好)
{
if((ms = of dselectpic。OpenFile())!= null)
{
//MessageBox。show(" ok ");
pic byte =新字节[毫秒长度];
ms . Position = 0;
ms.Read(picbyte,0,Convert。ToInt32(毫秒长度));
//MessageBox。秀(“看完了!”);
//连接到数据库
sqlconnectionconn = new sqlconnection();
conn . ConnectionString = " Data Source = localhost;数据库=测试;用户Id = sapwd = sa ";
SqlCommand cmd = new SqlCommand(" UpdateImage ",conn);
cmd。CommandType=CommandType。存储过程;
cmd。参数. Add("@UpdateImage ",SqlDbType。图片);
cmd。参数["@UpdateImage"]。Value = picbyte
conn . Open();
cmd。ExecuteNonQuery();
conn . Close();
ms . Close();
}
}
2)在picturebox中读取和显示
首先,添加一个名为ptbShow
的picturebox。然后,添加一个按钮,添加以下响应事件:
SQLConnectionConn = New SQLConnection(
conn . ConnectionString = " Data Source = localhost;数据库=测试;用户Id = sapwd = sa ";
string strSql = " select FImage from test where id = 1 ";
SqlCommand cmd = new SqlCommand(strSql,conn);
conn . Open();
SqlDataReader reader=cmd。ExecuteReader();
读者。read();
memory stream ms = new memory stream((byte[])reader[" FImage "]);
Image Image =图像。FromStream(ms,true);
读者。close();
conn . Close();
ptbShow。图像=图像

位律师回复
DABAN RP主题是一个优秀的主题,极致后台体验,无插件,集成会员系统
白度搜_经验知识百科全书 » net在sqlserver中的图片存取技术(例子在管理里inoutimg)

0条评论

发表评论

提供最优质的资源集合

立即查看 了解详情