日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

Storing and Retrieving Images from SQL Server using Microsoft .NET

發布時間:2023/12/9 asp.net 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Storing and Retrieving Images from SQL Server using Microsoft .NET 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Storing and Retrieving Images from SQL Server using Microsoft .NET

原文?Storing and Retrieving Images from SQL Server using Microsoft .NET

  • Download source - 19.6 Kb

Introduction

This article is about storing and retrieving images from database in Microsoft .NET using C#.

Tools Used

  • SQL Server 2000
  • Microsoft .NET Version 1.1
  • C# (Windows Forms based application)

Storing Images

  • Create a table in a SQL Server 2000 database which has at least one field of type?IMAGE.

    Here is the script I used:

    ?Collapse|?Copy Code CREATE TABLE [dbo].[tblImgData] ([ID] [int] NOT NULL ,[Name] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,[Picture] [image] NULL ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
  • Actually?IMAGE?field is just holding the reference to the page containing the binary data so we have to convert our image into bytes.
  • I used a file open dialog box to locate the file. ?Collapse|?Copy Code this.openFileDialog1.ShowDialog(this); string strFn=this.openFileDialog1.FileName;
  • By using?FileInfo?class, I retrieved the file size: ?Collapse|?Copy Code FileInfo fiImage=new FileInfo(strFn);
  • Declare an array of that size. ?Collapse|?Copy Code this.m_lImageFileLength=fiImage.Length; m_barrImg=new byte[Convert.ToInt32(this.m_lImageFileLength)];
  • By using?FileStream?object, I filled the byte array. ?Collapse|?Copy Code FileStream fs=new FileStream(strFn,FileMode.Open, FileAccess.Read,FileShare.Read); int iBytesRead=fs.Read(m_barrImg,0,Convert.ToInt32(this.m_lImageFileLength)); fs.Close();
  • Complete Load Image Code

    ?Collapse|?Copy Code protected void LoadImage() {try{this.openFileDialog1.ShowDialog(this);string strFn=this.openFileDialog1.FileName;this.pictureBox1.Image=Image.FromFile(strFn);FileInfo fiImage=new FileInfo(strFn);this.m_lImageFileLength=fiImage.Length;FileStream fs=new FileStream(strFn,FileMode.Open, FileAccess.Read,FileShare.Read);m_barrImg=new byte[Convert.ToInt32(this.m_lImageFileLength)];int iBytesRead = fs.Read(m_barrImg,0, Convert.ToInt32(this.m_lImageFileLength));fs.Close();}catch(Exception ex){MessageBox.Show(ex.Message);} }
  • Saving byte array data to database.
  • Create command text to insert record. ?Collapse|?Copy Code this.sqlCommand1.CommandText= "INSERT INTO tblImgData(ID,Name,Picture)" + " values(@ID,@Name,@Picture)";
  • Create parameters. ?Collapse|?Copy Code this.sqlCommand1.Parameters.Add("@ID",System.Data.SqlDbType.Int, 4); this.sqlCommand1.Parameters.Add("@Name", System.Data.SqlDbType.VarChar, 50);this.sqlCommand1.Parameters.Add("@Picture", System.Data.SqlDbType.Image);

    Notice “@Picture” has “SqlDbType.Image” because it is of?IMAGE?type Field.

  • Provide the value to the parameters. ?Collapse|?Copy Code this.sqlCommand1.Parameters["@ID"].Value=this.editID.Text; this.sqlCommand1.Parameters["@Name"].Value=this.editName.Text;this.sqlCommand1.Parameters["@Picture"].Value=this.m_barrImg;

    this.m_barrImg” is a byte array which we filled in the previous step.

  • Now execute non-query for saving the record to the database. ?Collapse|?Copy Code int iresult=this.sqlCommand1.ExecuteNonQuery();
  • Complete Save Image Code

    ?Collapse|?Copy Code private void btnSave_Click(object sender, System.EventArgs e) {try{this.sqlConnection1.Open();if (sqlCommand1.Parameters.Count ==0 ){this.sqlCommand1.CommandText="INSERT INTO tblImgData(ID," + " Name,Picture) values(@ID,@Name,@Picture)";this.sqlCommand1.Parameters.Add("@ID", System.Data.SqlDbType.Int,4);this.sqlCommand1.Parameters.Add("@Name", System.Data.SqlDbType.VarChar,50);this.sqlCommand1.Parameters.Add("@Picture", System.Data.SqlDbType.Image);}this.sqlCommand1.Parameters["@ID"].Value=this.editID.Text;this.sqlCommand1.Parameters["@Name"].Value=this.editName.Text;this.sqlCommand1.Parameters["@Picture"].Value=this.m_barrImg;int iresult=this.sqlCommand1.ExecuteNonQuery();MessageBox.Show(Convert.ToString(iresult));}catch(Exception ex){MessageBox.Show(ex.Message);}finally{this.sqlConnection1.Close();} }

    Retrieving Image

    Retrieving images from the database is the exact reverse process of saving images to the database.

  • First create command text to retrieve record. ?Collapse|?Copy Code SqlCommand cmdSelect = new SqlCommand("select Picture" + " from tblImgData where ID=@ID", this.sqlConnection1);
  • Create parameter for the query. ?Collapse|?Copy Code cmdSelect.Parameters.Add("@ID",SqlDbType.Int,4);
  • Provide value to the parameter. ?Collapse|?Copy Code cmdSelect.Parameters["@ID"].Value=this.editID.Text;
  • Open database connection and execute “ExecuteScalar” because we want only “IMAGE” column data back. ?Collapse|?Copy Code byte[] barrImg=(byte[])cmdSelect.ExecuteScalar();

    As the execute scalar returns data of “Object” data type, we cast it to?byte?array.

  • Save this data to a temporary file. ?Collapse|?Copy Code string strfn=Convert.ToString(DateTime.Now.ToFileTime()); FileStream fs=new FileStream(strfn,FileMode.CreateNew,FileAccess.Write); fs.Write(barrImg,0,barrImg.Length); fs.Flush(); fs.Close();
  • And display the image anywhere you want to display. ?Collapse|?Copy Code pictureBox1.Image=Image.FromFile(strfn);
  • Complete Image Retrieving Code

    ?Collapse|?Copy Code private void btnLoad_Click(object sender, System.EventArgs e) {try{SqlCommand cmdSelect=new SqlCommand("select Picture" + " from tblImgData where ID=@ID",this.sqlConnection1);cmdSelect.Parameters.Add("@ID",SqlDbType.Int,4);cmdSelect.Parameters["@ID"].Value=this.editID.Text;this.sqlConnection1.Open();byte[] barrImg=(byte[])cmdSelect.ExecuteScalar();string strfn=Convert.ToString(DateTime.Now.ToFileTime());FileStream fs=new FileStream(strfn, FileMode.CreateNew, FileAccess.Write);fs.Write(barrImg,0,barrImg.Length);fs.Flush();fs.Close();pictureBox1.Image=Image.FromFile(strfn);}catch(Exception ex){MessageBox.Show(ex.Message);}finally{this.sqlConnection1.Close();} }

    Bibliography

    • Retrieving Images from SQL Server in ASP.NET
    • Images, Thumbnails, SQL Server, and ASP.NET - Level 200

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found?here

    posted on 2014-04-23 23:05 NET未來之路 閱讀(...) 評論(...) 編輯 收藏

    轉載于:https://www.cnblogs.com/lonelyxmas/p/3684213.html

    總結

    以上是生活随笔為你收集整理的Storing and Retrieving Images from SQL Server using Microsoft .NET的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。