把图像文件上传到数据库,并从数据库读出

    技术2022-05-11  153

    上传图片到数据库:

    using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Data.SqlClient;using System.Drawing;using System.Web;using System.Web.SessionState;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.HtmlControls;using System.IO;

    namespace myUpLoad{/// <summary>/// img2sql 的摘要说明。/// </summary>public class img2sql : System.Web.UI.Page{protected System.Web.UI.WebControls.TextBox imgTitleTextBox;protected System.Web.UI.HtmlControls.HtmlInputFile upLoadImg;protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;protected System.Web.UI.WebControls.Button Button1;protected SqlConnection myConnection;

    private void Page_Load(object sender, System.EventArgs e){// 在此处放置用户代码以初始化页面string conn="server=(local);database=test;uid=sa;pwd=ilovenm";myConnection=new SqlConnection(conn);}

    #region Web Form Designer generated codeoverride protected void OnInit(EventArgs e){//// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。//InitializeComponent();base.OnInit(e);}

    /// <summary>/// 设计器支持所需的方法 - 不要使用代码编辑器修改/// 此方法的内容。/// </summary>private void InitializeComponent(){this.Button1.Click += new System.EventHandler(this.Button1_Click);this.Load += new System.EventHandler(this.Page_Load);

    }#endregion

    private void Button1_Click(object sender, System.EventArgs e){Stream myStream=upLoadImg.PostedFile.InputStream;int imgDataLen=upLoadImg.PostedFile.ContentLength;string imgType=upLoadImg.PostedFile.ContentType;string imgTitle=imgTitleTextBox.Text;byte[] imgData=new byte[imgDataLen];int n=myStream.Read(imgData,0,imgDataLen);

    //string conn="server=(local);database=test;uid=sa;pwd=ilovenm";//SqlConnection myConnection=new SqlConnection(conn);

    SqlCommand myCommand = new SqlCommand("INSERT INTO image (imgtitle,imgtype,imgdata) VALUES ( @imgtitle, @imgtype, @imgdata )", myConnection);

    myCommand.Parameters.Add(new SqlParameter("@imgtitle",SqlDbType.VarChar,50));myCommand.Parameters["@imgtitle"].Value=imgTitle;

    myCommand.Parameters.Add(new SqlParameter("@imgtype",SqlDbType.VarChar,50));myCommand.Parameters["@imgtype"].Value=imgType;

    myCommand.Parameters.Add(new SqlParameter("@imgdata",SqlDbType.Image));myCommand.Parameters["@imgdata"].Value=imgData;

    myConnection.Open();int numRowsAffected=myCommand.ExecuteNonQuery();myConnection.Close();}}}

    显示:using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Data.SqlClient;using System.Drawing;using System.Web;using System.Web.SessionState;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.HtmlControls;

    namespace myUpLoad{/// <summary>/// showimg 的摘要说明。/// </summary>public class showimg : System.Web.UI.Page{protected SqlConnection myConnection;

    private void Page_Load(object sender, System.EventArgs e){// 在此处放置用户代码以初始化页面string imgID=Request.QueryString["imgid"];string conn="server=(local);database=test;uid=sa;pwd=ilovenm";//string conn="server=(local);database=test;uid=sa;pwd=ilovenm";            myConnection=new SqlConnection(conn);

    string selectCmd="select imgdata,imgtype from image where id="+imgID;SqlCommand myCommand=new SqlCommand(selectCmd,myConnection);myConnection.Open();SqlDataReader myDataReader=myCommand.ExecuteReader();

    if (myDataReader.Read()){Response.ContentType=myDataReader["imgtype"].ToString();Response.BinaryWrite((byte[])myDataReader["imgdata"]);}}

    #region Web Form Designer generated codeoverride protected void OnInit(EventArgs e){//// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。//InitializeComponent();base.OnInit(e);}

    /// <summary>/// 设计器支持所需的方法 - 不要使用代码编辑器修改/// 此方法的内容。/// </summary>private void InitializeComponent(){this.Load += new System.EventHandler(this.Page_Load);}#endregion}}

     


    最新回复(0)