国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

在ASP.NET中從SQL Server檢索圖片

2019-11-18 19:34:08
字體:
來源:轉載
供稿:網友

  和存儲圖片相比,讀取圖片就要簡單多了。輸出一副圖片我們要做的就是使用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> 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 昌吉市| 安泽县| 神池县| 德安县| 南宁市| 嵊州市| 玉树县| 鄄城县| 白银市| 榆社县| 台中市| 全州县| 云龙县| 滨海县| 徐州市| 亳州市| 隆化县| 聂拉木县| 湘潭县| 伊金霍洛旗| 重庆市| 勃利县| 安塞县| 湘阴县| 贺兰县| 朔州市| 南宫市| 阿拉善盟| 兴国县| 玉山县| 得荣县| 怀化市| 嘉善县| 康定县| 焦作市| 宁城县| 固原市| 金门县| 祁阳县| 阜南县| 奉新县|