2015-11-11 27 views
0

我已經搜索了stackoverflow的答案,但找不到任何幫助我的東西。從SQL Server數據庫(圖像字段)綁定圖像到Ad Rotator

我想獲得一個Ad Rotator,它將顯示存儲在數據庫中的圖像。對於不同的產品將會有5個不同的廣告。產品存儲在cookie中,每次訂購後都會有所不同。我知道我必須從數據庫檢索二進制文件並將其轉換爲圖像。這是我曾嘗試(只是測試一個產品,現在我就可以使用foreach循環或東西,想先得到正確的想法。):

using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(strconn)) 
    { 
     SqlCommand cmdSelect = new SqlCommand("select Image from Products where ProductID = '1'", con); 
     con.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(); 
     Image IMG = Image.FromFile(strfn); 
    } 

但這代碼不起作用。我得到一個錯誤信息說

Error 1 'System.Web.UI.WebControls.Image' does not contain a definition for 'FromFile' 

所以我我有2個問題: 1.什麼是這樣做的正確方法? 2.如何將正確的值分配給Ad Rotator的XML文件中的ImageUrl字段?

非常感謝您的幫助!

回答

0

你可以試試這個方法,用較少的代碼行,使用MemoryStream的,而不是文件:

using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(strconn)) 
{ 
     SqlCommand cmdSelect = new SqlCommand("select Image from Products where ProductID = '1'", con); 
     con.Open(); 
     byte[] barrImg = (byte[])cmdSelect.ExecuteScalar(); 
     MemoryStream ms = new MemoryStream(barrImg); 
     Image IMG = Image.FromStream(strfn); 
    } 
相關問題