ADO.NET Access a picture with image columns in SQL Server

xiaoxiao2021-03-06  20

Table Structure 3 ID INT 4 00 Imgtitle Char 50 10 ImgType Char 50 10 IMGData Image 16 1 Save Picture Store.aspx Protected System.Web.ui.htmlControls.htmlInputFile File1; // Upload File with protected system.web.ui.webControls .TextBox TextBox1; protected System.Web.UI.WebControls.Button Button1; // fill in the file name private void Button1_Click (object sender, System.EventArgs e) {Stream imgdatastream = File1.PostedFile.InputStream; int imgdatalen = File1.PostedFile .ContentLength; string imgtype = File1.PostedFile.ContentType; string imgtitle = TextBox1.Text; byte [] imgdata = new byte [imgdatalen]; int n = imgdatastream.Read (imgdata, 0, imgdatalen); string connstr = "workstation id = OVERMIND; packet size = 4096; user id = sa; password = sa; data source = OVERMIND; persist security info = False; initial catalog = wztj "; SqlConnection connection = new SqlConnection (connstr); SqlCommand command = new SqlCommand (" INSERT INTO imageStore (Imgtitle, ImgType, IMGData) Values ​​(@Imgtitle, @ ImgType, @ imgdata) "; SQLPARA meter paramTitle = new SqlParameter ( "@ imgtitle", SqlDbType.VarChar, 50); paramTitle.Value = imgtitle; command.Parameters.Add (paramTitle); SqlParameter paramData = new SqlParameter ( "@imgdata", SqlDbType.Image); paramData .Value = imgdata; command.Parameters.Add (paramData); SqlParameter paramType = new SqlParameter ( "@imgtype", SqlDbType.VarChar, 50); paramType.Value = imgtype; command.Parameters.Add (paramType); connection.Open (); Int NumrowSaffected = Command.executenonQuery (); connection.close ();

} Image taken getImage.aspx private void Page_Load (object sender, System.EventArgs e) {string imgid = "1"; // Request.QueryString [ "imgid"]; string connstr = "workstation id = OVERMIND; packet size = 4096 ; user id = sa; password = sa; data source = OVERMIND; persist security info = False; initial catalog = wztj "; string sql =" SELECT imgdata, imgtype FROM ImageStore WHERE id = " imgid; SqlConnection connection = new SqlConnection ( Connstr); sqlcommand command = new sqlcommand (SQL, Connection); connection.open (); SqlDataReader DR = Command.executeReader (); if (Dr.Read ()) {response.contentType = DR ["iMgType"]. Tostring (); Response.binaryWrite (Byte []) DR ["IMGDATA"]); //Response.writefile (} connection.close (); // put user code to initialize the page head} Call GetImage.aspx page Show.htm

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

New Post(0)