日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

在Asp.Net中从sqlserver检索(retrieve)图片

發布時間:2023/11/27 生活经验 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在Asp.Net中从sqlserver检索(retrieve)图片 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
介紹:
這篇文章是我寫的"如何把圖片存入sqlServer中"的后續。我建議你在讀這篇文章之前先看看那篇。
和存儲圖片相比,讀取圖片就要簡單多了。輸出一副圖片我們要做的就是使用Response對象的BinaryWrite方法。
同時設置圖片的格式。在這篇文章中,我們將討論如何從SqlServer中檢索圖片。
并將學習以下幾個方面的知識.
·如何設置圖片的格式?
·如何使用BinaryWrite方法。

我們已經在Person表中存儲了數據,那么我們就寫些代碼來從表中讀取數據。
下面的代碼檢索了所有的值從Person表中。

從sqlserver中讀取圖片的代碼.
Public Sub Page_Load(sender As Object, e As EventArgs)
????????Dim myConnection As New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
????????Dim myCommand As New SqlCommand("Select * from Person", myConnection)
????????Try
????????????myConnection.Open()
????????????Dim myDataReader as SqlDataReader
????????????myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)

????????????Do While (myDataReader.Read())
????????????????Response.ContentType = myDataReader.Item("PersonImageType")
????????????????Response.BinaryWrite(myDataReader.Item("PersonImage"))
????????????Loop

????????????myConnection.Close()
????????????Response.Write("Person info successfully retrieved!")
????????Catch SQLexc As SqlException
????????????Response.Write("Read Failed : " & SQLexc.ToString())
????????End Try
????End Sub

看看他是怎么工作的?
上面的例子很簡單。我們所作的就是執行一個sql語句,再循環讀取所有的記錄(looping through all the records).
在顯示圖片之前,我們先設置了圖片的contentType,然后我們使用BinaryWrite方法把圖片輸出到瀏覽器。

源代碼:
/// retriving.aspx

<%@ Page Language="vb" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<HTML>
??<HEAD>
????<title>Retrieving Image from the Sql Server</title>
????????<script runat=server>
????????????????????????Public Sub Page_Load(sender As Object, e As EventArgs)
????????????????????????????????????' Create Instance of Connection and Command Object
????????????????????????????????????Dim myConnection As New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
????????????????????????????????????Dim myCommand As New SqlCommand("Select * from Person", myConnection)
?????????????????????????????????????Try
???????????????????????????????????????????????myConnection.Open()
????????????????????????????????????????????????Dim myDataReader as SqlDataReader
????????????????????????????????????????????????myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
?????????????????????????????????????????

???????????????????????????????????????????????Do While (myDataReader.Read())
????????????????????????????????????????????????????????????Response.ContentType = myDataReader.Item("PersonImageType")
????????????????????????????????????????????????????????????Response.BinaryWrite(myDataReader.Item("PersonImage"))
???????????????????????????????????????????????Loop????????????????????????????????????????????

????????????????????????????????????????????????myConnection.Close()
????????????????????????????????????????????????Response.Write("Person info successfully retrieved!")
????????????????????????????????????Catch SQLexc As SqlException
????????????????????????????????????????????????Response.Write("Read Failed : " & SQLexc.ToString())
????????????????????????????????????End Try
????????????????????????End Sub????

????</script>
??</HEAD>
??<body style="font: 10pt verdana">
??</body>
</HTML>

總結

以上是生活随笔為你收集整理的在Asp.Net中从sqlserver检索(retrieve)图片的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。