Neglected SQL Server data type--image

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.