首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

資料庫儲存文字/圖片檔案:二進位流讀寫方法備忘(三)

     編輯:關於C語言

圖片檔案從資料庫讀取並直接輸出至 HTTP 回應流,避免暫存檔。

圖片讀取輸出

private void ImgDataRead() {
    int imgId = Convert.ToInt32(Request.QueryString["id"]);
    using (SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"])) {
        string sql = "SELECT * FROM ImageStore WHERE ID = @ImageID";
        SqlCommand cmd = new SqlCommand(sql, con);
        cmd.Parameters.Add("@ImageID", SqlDbType.Int).Value = imgId;
        con.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        dr.Read();
        Response.ContentType = (string)dr["ImageContentType"];
        Response.OutputStream.Write((byte[])dr["ImageData"], 0, Convert.ToInt32(dr["ImageSize"]));
        Response.BufferOutput = true;
        con.Close();
    }
}

註解區展示替代寫法:讀入 MemoryStream 再建立 Bitmap 指派給 PictureBox。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved