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

上传人:cl****1 文档编号:561924150 上传时间:2023-11-06 格式:DOCX 页数:6 大小:13.19KB
返回 下载 相关 举报
net在sqlserver中的图片存取技术(例子在管理里inoutimg).docx_第1页
第1页 / 共6页
net在sqlserver中的图片存取技术(例子在管理里inoutimg).docx_第2页
第2页 / 共6页
net在sqlserver中的图片存取技术(例子在管理里inoutimg).docx_第3页
第3页 / 共6页
net在sqlserver中的图片存取技术(例子在管理里inoutimg).docx_第4页
第4页 / 共6页
net在sqlserver中的图片存取技术(例子在管理里inoutimg).docx_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《net在sqlserver中的图片存取技术(例子在管理里inoutimg).docx》由会员分享,可在线阅读,更多相关《net在sqlserver中的图片存取技术(例子在管理里inoutimg).docx(6页珍藏版)》请在金锄头文库上搜索。

1、 net在sqlserver中的图片存取技术(例子在管理里inoutimg)1.使用将图片上传并存入SQL Server中,然后从SQL Server中读取并显示出来:1)上传并存入SQL Server数据库构造create table testid identity(1,1),FImage image相关的存储过程Create proc UpdateImage(UpdateImage Image)AsInsert Into test(FImage) values(UpdateImage)GO在UpPhoto.aspx文件中添加如下: 然后在后置代码文件UpPhoto.aspx.cs添加btn

2、Add按钮的单击大事处理代码: private void btnAdd_Click(object sender, System.EventArgs e)/获得图象并把图象转换为byteHttpPostedFile upPhoto=UpPhoto.PostedFile;int upPhotoLength=upPhoto.ContentLength;byte PhotoArray=new ByteupPhotoLength;Stream PhotoStream=upPhoto.InputStream;PhotoStream.Read(PhotoArray,0,upPhotoLength);/连接数

3、据库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=Pho

4、toArray;/假如你盼望不使用存储过程来添加图片把上面四句代码改为:/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中读取并显示出来在需要显示图片的

5、地方添加如下代码: 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=ne

6、w 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数据库构造和使用的存储过过程,

7、同上面的一样首先,在窗体中加一个OpenFileDialog控件,命名为ofdSelectPic ;然后,在窗体上添加一个翻开文件按钮,添加如下单击大事代码:Stream ms;byte picbyte;/ofdSelectPic.ShowDialog();if (ofdSelectPic.ShowDialog()=DialogResult.OK)if (ms=ofdSelectPic.OpenFile()!=null)/MessageBox.Show(“ok“);picbyte=new bytems.Length;ms.Position=0;ms.Read(picbyte,0,Convert

8、.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“,SqlD

9、bType.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

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号