`
- 浏览:
70646 次
- 来自:
...
-
本文总结如何在.Net WinForm和.Net WebForm(asp.net)中将图片存入SQL Server中并读取显示的方法 。
1.使用asp.net将图片上传并存入SQL Server中,然后从SQL Server中读取并显示出来:
1)上传并存入SQL Server
数据库结构
create table test
{
id identity(1,1),
FImage image
}
相关的存储过程
Create proc UpdateImage
(
@UpdateImage Image
)
As
Insert Into test(FImage) values(@UpdateImage)
GO
在UpPhoto.aspx文件中添加如下:
<input id="UpPhoto" name="UpPhoto" runat="server" type="file">
<asp:Button id="btnAdd" name="btnAdd" runat="server" Text="上传"></asp:Button>
然后在后置代码文件UpPhoto.aspx.cs添加btnAdd按钮的单击事件处理代码:
private void btnAdd_Click(object sender, System.EventArgs e)
{
//获得图象并把图象转换为byte[]
HttpPostedFile upPhoto=UpPhoto.PostedFile;
int upPhotoLength=upPhoto.ContentLength;
byte[] PhotoArray=new Byte[upPhotoLength];
Stream PhotoStream=upPhoto.InputStream;
PhotoStream.Read(PhotoArray,0,upPhotoLength);
//连接数据库
SqlConnection conn=new SqlConnection();
conn.ConnectionString="Data Source=localhost;Database=test;User Id=sa;Pwd=sa";
SqlCommand cmd=new SqlCommand("UpdateImage",conn);
cmd.CommandType=CommandType.StoredProcedure;
cmd.Parameters.Add("@UpdateImage",SqlDbType.Image);
cmd.Parameters["@UpdateImage"].Value=PhotoArray;
//如果你希望不使用存储过程来添加图片把上面四句代码改为:
//string strSql="Insert into test(FImage) values(@FImage)";
//SqlCommand cmd=new SqlCommand(strSql,conn);
//cmd.Parameters.Add("@FImage",SqlDbType.Image);
//cmd.Parameters["@FImage"].Value=PhotoArray;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
2)从SQL Server中读取并显示出来
在需要显示图片的地方添加如下代码:
<asp:image id="imgPhoto" runat="server" ImageUrl="ShowPhoto.aspx"></asp:image>
ShowPhoto.aspx主体代码:
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
SqlConnection conn=new SqlConnection()
conn.ConnectionString="Data Source=localhost;Database=test;User Id=sa;Pwd=sa";
string strSql="select * from test where id=2";//这里假设获取id为2的图片
SqlCommand cmd=new SqlCommand(strSql,conn);
conn.Open();
SqlDataReader reader=cmd.ExecuteReader();
reader.Read();
Response.ContentType="application/octet-stream";
Response.BinaryWrite((Byte[])reader["FImage"]);
Response.End();
reader.Close();
}
}
2.在WinForm中将图片存入SQL Server,并从SQL Server中读取并显示在picturebox中
1),存入SQL Server
数据库结构和使用的存储过过程,同上面的一样
首先,在窗体中加一个OpenFileDialog控件,命名为ofdSelectPic ;
然后,在窗体上添加一个打开文件按钮,添加如下单击事件代码:
Stream ms;
byte[] picbyte;
//ofdSelectPic.ShowDialog();
if (ofdSelectPic.ShowDialog()==DialogResult.OK)
{
if ((ms=ofdSelectPic.OpenFile())!=null)
{
//MessageBox.Show("ok");
picbyte=new byte[ms.Length];
ms.Position=0;
ms.Read(picbyte,0,Convert.ToInt32(ms.Length));
//MessageBox.Show("读取完毕!");
//连接数据库
SqlConnection conn=new SqlConnection();
conn.ConnectionString="Data Source=localhost;Database=test;User Id=sa;Pwd=sa";
SqlCommand cmd=new SqlCommand("UpdateImage",conn);
cmd.CommandType=CommandType.StoredProcedure;
cmd.Parameters.Add("@UpdateImage",SqlDbType.Image);
cmd.Parameters["@UpdateImage"].Value=picbyte;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
ms.Close();
}
}
2)读取并显示在picturebox中
首先,添加一个picturebox,名为ptbShow
然后,添加一个按钮,添加如下响应事件:
SqlConnection conn=new SqlConnection();
conn.ConnectionString="Data Source=localhost;Database=test;User Id=sa;Pwd=sa";
string strSql="select FImage from test where id=1";
SqlCommand cmd=new SqlCommand(strSql,conn);
conn.Open();
SqlDataReader reader=cmd.ExecuteReader();
reader.Read();
MemoryStream ms=new MemoryStream((byte[])reader["FImage"]);
Image image=Image.FromStream(ms,true);
reader.Close();
conn.Close();
ptbShow.Image=image;<script></script>
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
ASP.Net在SqlServer中的图片存取技术
不是经典不上传 Asp.Net在SqlServer中的图片存取技术 很经典 参考 不是存储图片名而是这个图片
ASP.NET如何存取SQL Server数据库图片
如何用VB.NET存取SQL Server2000中的图像数据.pdf
ADO.NET存取SQL SERVER 2000中图像数据的实现.pdf
ASP.NET如何存取SQL Server数据库图片
基于ADO.NET的SQL Server数据库image对象的存取及其效率分析.pdf
有一个员工表Employee,需要保存员工照片(Photo)到数据库(sql server)上。员工照片对应的字段是varbinary(max),也就是要存成二进制文件类型(这和以前讨巧地存图片文件路径就不相同了),默认可以为空。
结合具体实例,说明在Web数据库应用系统中,如何运用ASP.NET技术实现SQL SERVER2000数据库对图片文件进行存储并通过浏览器进行显示。该实例为数据库中多种二进制大对象类型的数据,包括图片、声音、视频及各种...
C#+存取SqlServer中的Image类型
Asp.Net技术文档 ADO.NET是微软的Microsoft ActiveX Data Objects (ADO)的下一代产品,是在微软的.NET中创建分布式和数据共享应用程序的应用程序开发接口(API)。 <br/>ADO.NET能被用在任何用户的应用程序,...
最近做东西遇到向数据库插入二进制图片然后在网页中读取的问题,通过网上查资料写了个例子,拿出来分享一下,本文件采用VS2010+Sqlserver2008
主要为大家详细介绍了ASP.NET数据库如何存取图片,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
SQLServer作为后台数据库在数据发布和收集中扮演了一个重要角色,然而Web收信与发布的信息是多方面的,如何将图片、文件、多媒体数据等二进制大对象(BOLB)在SQLServer数据库中存取,是一项应用广泛的技术,为解决远程...
懒人工作通采用基于WEB的企业计算,采用了世界上最先进的微软技术:C#语言、SqlServer/Access数据库引擎和IIS服务器,性能稳定可靠。数据存取集中控制,避免了数据泄漏的可能。提供数据备份工具,保护系统数据安全。...
383 实例270 使用存取文件名的方法存取图片 383 实例271 使用ADO.NET对象向SQL Server数据库存入图片 384 9.7 数据修改 387 实例272 利用数据绑定控件修改数据 387 实例273 利用数据对象...