Upload the image file to the database and read from the database

zhaozj2021-02-11  163

Upload pictures to the database:

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; use system.web.ui.htmlcontrols; using system.io;

Namespace myupload {///

/// img2sql's summary description. /// 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.RequidfieldValidator RequiredFieldValidator1; protected system.web.ui.webcontrols.button button1; protected sqlconnection myconnection;

Private void page_load (object sender, system.eventargs e) {// Place user code here to initialize page string conn = "server = (local); database = test; uid = sa; pwd = ilovenm; MyConnection = New SqlConnection (conn);

#Region Web Form Designer Generated CodeOverride Protected Void OnNit (Eventargs E) {//// Codegen: This call is required for the ASP.NET Web Form Designer. //Initializecomponent();Base.oninit (E);

///

/// Designer supports the required method - do not use the code editor to modify the // / this method. /// private () {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 ("@ 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 ("@ imgdata", sqldbtype.image)); mycommand.parameters ["@ imgdata"]. value = imgdata;

MyConnection.open (); int nuMrowsaffected = mycommand.executenonquery (); myconnection.close ();}}}

Display: 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 {///

/// showimg's summary description. /// public class showimg: system.Web.ui.page {protected sqlconnection myconnection;

Private void page_load (object sender, system.eventargs e) {// Place user code here to initialize page string imgid = request.QueryString ["IMGID"]; string conn = "server = (local); data = test; UID = SA; PWD = ilovenm "; // String conn =" server = (local); data = 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 OnNit (Eventargs E) {//// Codegen: This call is required for the ASP.NET Web Form Designer. //Initializecomponent();Base.oninit (E);

///

/// Designer supports the required method - do not use the code editor to modify the // / this method. /// private () {this.load = new system.eventhandler (this.page_load);} # endregion}}

转载请注明原文地址:https://www.9cbs.com/read-5822.html

New Post(0)