被人忽視的sqlserver資料類型--image,sqlserver--image

來源:互聯網
上載者:User

被人忽視的sqlserver資料類型--image,sqlserver--image

  SqlServer中有一種資料類型是Image,用來儲存圖片大小不超過2g的圖片,將圖片轉換為二進位!缺點是佔用了很大的資料存放區空間。但是現對於之前的儲存實體路徑來說讀取圖片和儲存圖片方便了很多。

  那麼圖片在MVC程式中是如何存入資料庫,並從資料庫顯示到頁面上的呢:

  下面是一個簡單的小例子:

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:讀取任何地方的路徑                foreach (HttpPostedFileBase file in fileToUpload)                {                    path = System.IO.Path.Combine(Server.MapPath("~/"), "uploadimage\\" + System.IO.Path.GetFileName("00" + file.FileName.Substring(file.FileName.LastIndexOf("."))));                    //寫入資料庫                    file.SaveAs(path);                    //將需要儲存的圖片讀取為資料流                    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 讀取檔案直接顯示到視圖上        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
那麼圖片又是如何顯示到網頁上的呢,很簡單:

if (xhr.readyState == 4 && xhr.status == 200) {        $('#personimg').attr("src", "http://localhost:55576/UpDownload/Read");            }

這個demo的實現環境是MVC,圖片的擷取路徑是,當前服務主機地址/controller/action

怎麼樣,sqlserver為圖片的讀寫,提供了很多方便之處吧!


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.