欢迎访问我的博客 https://javascript.shop

net9:图片文件转换成二进制流存入SQL数据库,以及从数据库中读取二进制流输出文件

原文发布时间为:2008-08-10 —— 来源于本人的百度文章 [由搬家工具导入]

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


using System.Data.SqlClient;

public partial class Default4 : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["imgDataConn"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Guid gid = Guid.NewGuid();
        conn.Open();
        SqlCommand cmd = new SqlCommand("Insert into imgdata(gid,filedata) values(@gid,@filedata)", conn);
        cmd.Parameters.Add("@gid", SqlDbType.UniqueIdentifier).Value = gid;
        cmd.Parameters.Add("@filedata",SqlDbType.Image).Value=FileUpload1.FileBytes;
        cmd.ExecuteNonQuery();
        conn.Close();

        Session["gid"]=gid;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand("Select filedata from imgdata where gid='" + Session["gid"].ToString() + "'", conn);
        byte[] fbt = (byte[])cmd.ExecuteScalar();
        conn.Close();
        Response.OutputStream.Write(fbt, 0, fbt.Length);
        Response.End();
    }
}

posted @ 2017-07-09 15:04  孑孓子  阅读(209)  评论(0编辑  收藏  举报
欢迎访问我的博客 https://javascript.shop