`
- 浏览:
2436491 次
- 性别:
- 来自:
上海
-
在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>
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
While ASP.NET 3.5 boasts server controls like the ListView and the incredibly flexible GridView, it also includes advancements in AJAX technology combined with JavaScript® debugging features in ...
Transact-SQL, or T-SQL, is Microsoft Corporation’s powerful implementation of the ANSI standard SQL database query language, which was designed to retrieve, manipulate, and add data to relational ...
ASP.NET MVC 2 Release Candidate Release Notes This document describes the Release Candidate release of the ASP.NET MVC 2 framework. Installation Notes 2 Documentation 2 Support 2 Upgrading an ASP...
VC开发SQL 字符串检索条件经典代码VC + + code in SQL string to retrieve the classical condition
retrieve :检索 dynamically generate:动态生成 specify :指定 render :呈现 folder :文件夹 placeholder :占位符 Previously :以前 icon :图标 entirety :整体 schema:概要,计划
Transact-SQL ... T-SQL is the native language of SQL Server. By seeing how the basic operations can be implemented in T-SQL and VB, you’ll gain a deeper under- standing of database programming.
Microsoft SQL Server 2008 MDX Step by Step Teach yourself the Multidimensional Expressions (MDX) query language—one step at a time. With this practical, learn-by-doing tutorial, you’ll build the ...
哈工大计算机研究生 信息检索课件 IR 介绍信息检索的技术,包括Google的MapReduce技术
Store and retrieve the images in sql server database
string userID = " "; string userPassword = " "; int flag = 0;... MessageBox.Show("Error: Failed to retrieve the required data from the DataBase.\n{0}", ex.Message); return; }
Retrieve email from POP3 server or IMAP4 server and parse email in ASP, VB, VBA, VC++, Managed C++, C#, VB.NET, JScript, ASP.NET, Delphi or other COM/.NET/.NET Core environment applications....
1、mywebservice v1.0为PB11.5开发的 webservice服务端对象...7、数据库为sqlserver2000,连接的是pubs库,可以在客户端的n_mywebclient对象的construcutr事件中设置; 8、作者:qq:17232341 乌蒙浪子 日期: 2010.09.17;
Microsoft’s implementation of the Structured Query Language (SQL) for SQL Server. T-SQL is the language that is most often used to extract or modify data stored in a SQL Server database, regardless ...
Welcome to .NET and Visual Basic .NET.As you already know, .NET is a name for a new strategy: a blueprint for building applications for the next decade. It’s actually even more than that. It’s ...
Query Retrieve SCP Emulator
安装浏览器: component install ndhoule/retrieve 节点: $ npm install retrieve应用程序接口retrieve(str : string, obj : Object) => * 在指定的、以点分隔的路径检索属性。 var person = { name : 'Bob Loblaw'...
此示例显示如何使用Microsoft Graph SDK将ASP .Net Web应用程序连接到Security API。 内容类型 技术 服务 创建日期 样本 微软图 安全 2018/4/6上午9:53:00 适用于ASP.NET 4.6(REST)的Microsoft Graph Security ...
information retrieve textbook from Standford university
描述PB的并发控制机制,详细解释Row changed between retrieve and update错误出现的原因,以及处理方法。对学习PB有一定的帮助。
恢复xp_cmdshell SQL Server阻止了对组件 'xp_cmdshell' 的过程'sys.xp_cmdshell' 启用