萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> sql server教程 >> 在SQLServer中保存和輸出圖片

在SQLServer中保存和輸出圖片

在sql server中保存和輸出圖片

cashcho(翻譯)

關鍵字 SQL Server images

出處 http://www.bipinjoshi.com/

介紹

  有時候我們需要保存一些binary data進數據庫。SQL Server提供一個叫做image的特殊數據類型供我們保存binary data。Binary data可以是圖片、文檔等。在這篇文章中我們將看到如何在SQL Server中保存和輸出圖片。

建表

  為了試驗這個例子你需要一個含有數據的table(你可以在現在的庫中創建它,也可以創建一個新的數據庫),下面是它的結構:

Column Name Datatype Purpose ID Integer identity column Primary key
IMGTITLE Varchar(50) Stores some user friendly title to identity the image IMGTYPE Varchar(50) Stores image content type. This will be same as recognized content types of ASP.NET
IMGDATA Image Stores actual image or binary data.
保存images進SQL Server數據庫

  為了保存圖片到table你首先得從客戶端上傳它們到你的Web服務器。你可以創建一個web form,用TextBox得到圖片的標題,用HTML File Server Control得到圖片文件。確信你設定了Form的encType屬性為multipart/form-data。

Stream imgdatastream = File1.PostedFile.InputStream;www.c hinaitpower.comlOPhU7

int imgdatalen = File1.PostedFile.ContentLength;www.c hinaitpower.comlOPhU7

string imgtype = File1.PostedFile.ContentType;www.c hinaitpower.comlOPhU7

string imgtitle = TextBox1.Text;www.c hinaitpower.comlOPhU7

byte[] imgdata = new byte[imgdatalen];www.c hinaitpower.comlOPhU7

int n = imgdatastream.Read(imgdata,0,imgdatalen);www.c hinaitpower.comlOPhU7

string connstr=www.c hinaitpower.comlOPhU7

((NameValueCollection)Context.GetConfigwww.c hinaitpower.comlOPhU7

("appSettings"))["connstr"];www.c hinaitpower.comlOPhU7

SqlConnection connection = new SqlConnection(connstr);www.c hinaitpower.comlOPhU7

SqlCommand command = new SqlCommandwww.c hinaitpower.comlOPhU7

("INSERT INTO ImageStore(imgtitle,imgtype,imgdata)www.c hinaitpower.comlOPhU7

VALUES ( @imgtitle, @imgtype,@imgdata )", connection );www.c hinaitpower.comlOPhU7

www.c hinaitpower.comlOPhU7

SqlParameter paramTitle = new SqlParameterwww.c hinaitpower.comlOPhU7

("@imgtitle", SqlDbType.VarChar,50 );www.c hinaitpower.comlOPhU7

paramTitle.Value = imgtitle;www.c hinaitpower.comlOPhU7

command.Parameters.Add( paramTitle);www.c hinaitpower.comlOPhU7

www.c hinaitpower.comlOPhU7

SqlParameter paramData = new SqlParameterwww.c hinaitpower.comlOPhU7

( "@imgdata", SqlDbType.Image );www.c hinaitpower.comlOPhU7

paramData.Value = imgdata;www.c hinaitpower.comlOPhU7

command.Parameters.Add( paramData );www.c hinaitpower.comlOPhU7

www.c hinaitpower.comlOPhU7

SqlParameter paramType = new SqlParameterwww.c hinaitpower.comlOPhU7

( "@imgtype", SqlDbType.VarChar,50 );www.c hinaitpower.comlOPhU7

paramType.Value = imgtype;www.c hinaitpower.comlOPhU7

command.Parameters.Add( paramType );www.c hinaitpower.comlOPhU7

www.c hinaitpower.comlOPhU7

connection.Open();www.c hinaitpower.comlOPhU7

int numRowsAffected = command.ExecuteNonQuery();www.c hinaitpower.comlOPhU7

connection.Close();www.c hinaitpower.comlOPhU7


從數據庫中輸出圖片

  現在讓我們從數據庫中取出我們剛剛保存的圖片,在這兒,我們將直接將圖片輸出至浏覽器。你也可以將它保存為一個文件或做任何你想做的。

private void Page_Load(object sender, System.EventArgs e)www.c hinaitpower.comlOPhU7

{www.c hinaitpower.comlOPhU7

string imgid =Request.QueryString["imgid"];www.c hinaitpower.comlOPhU7

string connstr=((NameValueCollection)www.c hinaitpower.comlOPhU7

Context.GetConfig("appSettings"))["connstr"];www.c hinaitpower.comlOPhU7

string sql="SELECT imgdata, imgtype FROM ImageStore WHERE id = "www.c hinaitpower.comlOPhU7

+ imgid;www.c hinaitpower.comlOPhU7

SqlConnection connection = new SqlConnection(connstr);www.c hinaitpower.comlOPhU7

SqlCommand command = new SqlCommand(sql, connection);www.c hinaitpower.comlOPhU7

connection.Open();www.c hinaitpower.comlOPhU7

SqlDataReader dr = command.ExecuteReader();www.c hinaitpower.comlOPhU7

if(dr.Read())www.c hinaitpower.comlOPhU7

{www.c hinaitpower.comlOPhU7

Response.ContentType = dr["imgtype"].ToString();www.c hinaitpower.comlOPhU7

Response.BinaryWrite( (byte[]) dr["imgdata"] );www.c hinaitpower.comlOPhU7

}www.c hinaitpower.comlOPhU7

connection.Close();www.c hinaitpower.comlOPhU7

}www.c hinaitpower.comlOPhU7



  在上面的代碼中我們使用了一個已經打開的數據庫,通過datareader選擇images。接著用Response.BinaryWrite代替Response.Write來顯示image文件。

  希望您喜歡這些文章,如有任何意見和建議請致信[email protected]

本文轉載自中國軟件(http://www.csdn.net)。

關鍵詞:

copyright © 萬盛學電腦網 all rights reserved