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 {///
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);
///
} #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 {///
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);
///