微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

    本文总结如何在.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,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,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;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐