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();
    }
}

上一篇:return的作用


下一篇:Django 之 models的 F() 和 Q() 函数