簡體   English   中英

將圖像從SQL Server數據庫(圖像字段)綁定到Ad Rotator

[英]Bind image from SQL Server database (image field) to Ad Rotator

我已經在stackoverflow上搜索了答案,但是找不到任何可以幫助我的東西。

我正在嘗試獲得一個廣告旋轉器,它將顯示存儲在數據庫中的圖像。 針對不同的產品,將有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'  

因此,我有兩個問題:1.正確的做法是什么? 2.如何為Ad Rotator的XML文件中的ImageUrl字段分配正確的值?

預先感謝您的任何幫助!

您可以使用更少的代碼行,使用MemoryStream而不是File來嘗試這種方式:

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);
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM