SQL Server one of the data types is Image that is used to store the image size no more than 2g picture, convert the picture to binary! The disadvantage is that it takes up a lot of data storage space. However, it is much easier to read pictures and store pictures in the previous physical path of storage.
so the picture in MVC How is the program stored in the database and displayed on the page from the database:
Here's a simple little example:
private string sqlconn = "Data source=;initial catalog=image; Persist Security info=true; User Id=sa; password=123456 "; GET:/updownload/public actionresult Index () {return View (); } [HttpPost] [ValidateInput (false)] public bool Upload (httppostedfilebase[] filetoupload) { String path = ""; try {//toddo: Read anywhere path foreach (httppostedfilebase file in filetoupload) {Path = System.IO.Path.Combine (Server.MapPath ("~/"), "uploadimage\\" + System.IO.Path.GetFileN Ame ("XX" + file.) Filename.substring (file. Filename.lastindexof ("."))); Write to database file. SaveAs (path); Read the picture that needs to be stored as a data stream FileStream fs = new FileStream (path, FileMode.Open, FileAccess.Read); byte[] Imgbtye = new Byte[fs. Length]; Fs. Read (Imgbtye, 0, Convert.ToInt32 (fs. Length)); Fs. Close (); using (SqlConnection conn = new SqlConnection (sqlconn)) {Conn. Open (); SqlCommand cmd = new SqlCommand (); Cmd. Connection = conn; Cmd.commandtext = "INSERT into T_teacherimage (teacherimage) VALUES (@imgfile)"; SqlParameter par = new SqlParameter ("@imgfile", sqldbtype.image); Par. Value = Imgbtye; Cmd. Parameters.Add (PAR); Cmd. ExecuteNonQuery (); }} return true; } catch {return false; }} #region read the file directly to the view public void Read () {byte[] MyData = new Byte[0]; using (SqlConnection conn = new SqlConnection (sqlconn)) {Conn. Open (); SqlCommand cmd = new SqlCommand (); Cmd. Connection = conn; Cmd.commandtext = "Select Teacherimage from T_teacherimage where id= ' 1 '"; SqlDataReader SDR = cmd. ExecuteReader (); Sdr. Read (); MyData = (byte[]) sdr["Teacherimage"]; Response.ContentType = "Image/gif"; Response.BinaryWrite (MyData); Conn. Close (); }} #endregion
So how does the picture appear on the page, it's simple:
if (xhr.readystate = = 4 && Xhr.status = =) { $ (' #personimg '). attr ("src", "http://localhost:55576/ Updownload/read "); }
this Demo the implementation environment is MVC, the Get path to the picture is the current service host address /controller/action
How, SQL Server provides a lot of convenience for the reading and writing of pictures.
Neglected SQL Server data type--image