Web跟WinForm不太一樣,所以需要不同的方法,這邊講的是用Http Handler的方法

首先我在Visual Studio 2008的Web專案裡先創一個新檔案叫做Generic Handler,然後檔名我取名是Get_Image.ashx

然後在ProcessRequest這個函式裡寫程式(最後一段有寫如何用這個Handler產生XML)

try
        {

            using (SqlConnection cn = new SqlConnection())
            {


                SqlCommand cmd = new SqlCommand();

                cn.ConnectionString = ConfigurationManager.ConnectionStrings["這是自己在App.Config設定的一些連線字串的名字"].ConnectionString.ToString();

                cn.Open();

                cmd.Connection = cn;

                cmd.CommandType = CommandType.StoredProcedure;

                cmd.CommandText = "StoredProcedure的名字";

                //這行可以傳入參數,context.Request.QueryString["ID"]的意思是接收下面ASP網頁傳來的ID這個變數的值
                cmd.Parameters.Add("@F_ID", SqlDbType.NVarChar).Value = context.Request.QueryString["ID"];
               
                SqlDataReader dr = cmd.ExecuteReader();


                if (dr.Read())
                {
                    context.Response.ContentType = "image/jpeg";
                    context.Response.BinaryWrite((byte[])dr["F_Image"]);
                }

                cn.Close();
            }
        }

       
        catch (Exception ex)
        {

        }

 

 

接下來是ASP.NET網頁的部分

先在ASP.NET這邊用個Sql的DataSource吧

<asp:SqlDataSource ID="DS" runat="server" ConnectionString="<%$ ConnectionStrings:連接字串的名字 %>"
  SelectCommand="StoredProcedure的名字"  SelectCommandType="StoredProcedure">
</asp:SqlDataSource>

--------------------------------------------------------------------------------

補充:如果上面這段要加參數給StoredProcedure的話

要這樣寫

  <asp:SqlDataSource ID="MyDS" runat="server" ConnectionString="<%$ ConnectionStrings:連接字串的名字 %>"
  SelectCommand="StoredProcedure的名字"  SelectCommandType="StoredProcedure">
  <SelectParameters>
  <asp:Parameter Name="a" Type="String" DefaultValue="aa"/>
  <asp:Parameter Name="b" Type="String" DefaultValue="bb"/>
  <asp:Parameter Name="c" Type="String" DefaultValue="cc"/>
  <asp:Parameter Name="d" Type="String" DefaultValue="dd "/>
  <asp:Parameter Name="e" Type="String" DefaultValue="ee"/>
  </SelectParameters>
  </asp:SqlDataSource>

---------------------------------------------------------------------------------

然後在image 的控制項這邊加上ImageUrl這行

 <asp:Image ID="Image1" runat="server" ImageUrl='<%#"~/Get_Image.ashx?ID="+Eval("F_ID")%>' />

上面的F_ID是ASP網頁這邊的DataSource抓回來資料庫的F_ID欄位,Get_Image.ashx?ID="+Eval("F_ID")的意思就是向

Get_Image.ashx這個Handler傳一個ID的變數給他,然後他會去對應的StoredProcedure裡面下類似這樣的語法

SELECT Image FROM 資料表 WHERE F_ID=@F_ID 這樣

去抓取那個F_ID的圖片的二進位檔

 

最後補一下App.config裡面的連接字串這樣

 <connectionStrings>
    <add name="取個名字要對應到上面講的" connectionString="Data Source=192.168.0.1;Initial Catalog=起始用哪個資料庫;User ID=SA;Password=打密碼"
        providerName="System.Data.SqlClient" />
  </connectionStrings>

 

 

 

再補充一下如果要產生出XML的話

要寫

context.Response.ContentType = "text/xml";

           

context.Response.ContentEncoding =Encoding.UTF8;

           

context.Response.Write(sb.ToString());

 

arrow
arrow
    全站熱搜

    welkingunther 發表在 痞客邦 留言(0) 人氣()