在ASP.NET中从SQL Server检索图片未知

在ASP.NET中从SQL Server检索图片p> 和存储图片相比,读取图片就要简单多了。输出一副图片我们要做的就是使用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











Retrieving Image from the Sql 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
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值