转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 数据库 >> Sql Server >> 正文
.Net在SqlServer中的图片存取技术         

.Net在SqlServer中的图片存取技术

作者:闵涛 文章来源:闵涛的学习笔记 点击数:950 更新时间:2007/11/14 11:11:15

本文总结如何在.Net Winform和.Net webform(asp.net)中将图片存入sqlserver中并读取显示的方法
1,使用asp.net将图片上传并存入SqlServer中,然后从SqlServer中读取并显示出来
一,上传并存入SqlServer
 数据库结构
  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();
}

二,从SqlServer中读取并显示出来
在需要显示图片的地方添加如下代码:
<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()
                reader.Read();
                Response.ContentType="application/octet-stream";
                Response.BinaryWrite((Byte[])reader["FImage"]);
                Response.End();
                reader.Close();
     }
}


3,在winform中将图片存入sqlserver,并从sqlserver中读取并显示在picturebox中

1,存入sqlserver
数据库结构和使用的存储过过程,同上面的一样
 1.1,在窗体中加一个OpenFileDialog控件,命名为ofdSelectPic
 1.2,在窗体上添加一个打开文件按钮,添加如下单击事件代码:
    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中
 2.1 添加一个picturebox,名为ptbShow
 2.2 添加一个按钮,添加如下响应事件:
      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;


[其他]手工升级ACCESS到SQLSERVER方法详解  [Web开发]asp+sqlserver 分页方法(不用存储过程)
[办公软件]在sybase中插入图片、PDF、文本文件  [办公软件]安装Sybase ASE
[办公软件]linux指令大全(完整篇)  [办公软件]Linux新手入门常用命令大全
[办公软件]在RedHat Linux 9里安装gaim0.80  [办公软件]浅谈Linux 下Java 1.5 汉字方块问题解决方法
[办公软件]Linux程序员必读:中文化与GB18030标准  [办公软件]linux指令大全
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · Sql Server  · MySql
    · Access  · ORACLE
    · SyBase  · 其他
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台