All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
精通数据库系列之入门-基础篇2_mssql学习_编程技术
ASP.NE技巧:GridView绑定DropdownList_[Asp.Net教程]
用动态网页技术PHP打造个人网站全攻略_php资料_编程技术
ASP.NET,Web应用程序的简单AJAX实现_[Asp.Net教程]
C#,4.0语言将出现重大改变,带来一段Code,Preview_[Asp.Net教程]
DataGrid资料_[Asp.Net教程]
c#中GDI+图形图像:GDI+中的区域使用方法
Asp.Net2.0中实现多任务异步页的一点提示_.net资料_编程技术
vs2005安装了sp1后发布问题解决办法_[Asp.Net教程]
SQL Server查询优化方法_[SQL Server教程]
Reading number is top 10 pictures
西班牙山村小景1
超强高考作文
2012 national geographic daily picture9
Japanese snow monkeys in the hot spring to keep warm, close their eyes to enjoy
陪睡门马睿菈自曝写真 称首拍大尺度照片2
Soong ching ling's former residence1
机器人也有性生活吗?
泳装名模阿尔贝特妮写真2
Born After 90 Beijing sports university campus flower photos5
奇趣的世界记录1
Download software ranking
仙剑奇侠传98版歌曲
The Bermuda triangle2
Call Of Duty2
Tram sex maniac 2 (H) rar bag6
C#高级编程(第4版)
Tram sex maniac 2 (H) rar bag4
Jinling thirteen stock
网页特效实例大全
功夫熊猫2(上集)
WebService在.NET中的实战应用教学视频 → 第5集
delv published in(发表于) 2014/1/6 8:46:56 Edit(编辑)
.NET在SQL,Server中的图片存取技术_[Asp.Net教程]

.NET在SQL,Server中的图片存取技术_[Asp.Net教程]

.NET在SQL Server中的图片存取技术_[Asp.Net教程]

本文总结如何在.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文件中添加如下:


然后在后置代码文件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中读取并显示出来
在需要显示图片的地方添加如下代码:

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;
原文地址:http://stewen.cnblogs.com/archive/2005/12/20/300587.aspx







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.