当前位置: 首页 > 网络学院 > 服务端脚本教程 > ADO > ADO提升执行速度

ADO
ADO DataTypes
ADO 摘要
ADO 实例

ADO 中的 ADO提升执行速度


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 829 ::
收藏到网摘: n/a

Use the GetString() method to speed up your ASP script (instead of using multiple Response.Write's).
我们可以使用GetString()来提升ASP脚本的运行速度(以此替代对“Response.Write”指令的多次使用)。


Examples
案例

Using GetString()
使用GetString()
How to use GetString() to display data from a recordset in an HTML table.
如何使用GetString()来显示一个HTML表单中一条记录所包含的数据信息。


Multiple Response.Write's
多次使用“Response.Write”指令

The following example demonstrates one way of how to display a database query in an HTML table:
下面这个例子演示了在HTML表单中显示数据库查询信息的方法:

<html>
<body>
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "c:/webdata/northwind.mdb"
set rs = Server.CreateObject("ADODB.recordset")
rs.Open "SELECT Companyname, Contactname FROM Customers", conn
%>
<table border="1" width="100%">
<%do until rs.EOF%>
<tr>

<td><%Response.Write(rs.fields("Companyname"))%></td>
<td><%Response.Write(rs.fields("Contactname"))%></td>
</tr>

<%rs.MoveNext
loop%>
</table>
<%
rs.close
conn.close
set rs = Nothing
set conn = Nothing
%>
</body>
</html>

For a large query, this can slow down the script processing time, since many Response.Write commands must be processed by the server.
如果查询的信息量比较大,上述代码会增加脚本的运行时间,因为Response.Write命令是在服务器端执行的。

The solution is to have the entire string created, from <table> to </table>, and then output it - using Response.Write just once.
上述问题的解决办法就是将争端字符串写入<table>和</table>之间,然后输出它,这样的话你只要使用一次“Response.Write”指令。


The GetString() Method
GetString()方法

The GetString() method allows you to display the string with only one Response.Write. It also eliminates the do...loop code and the conditional test that checks if the recordset is at EOF.
GetString()方法允许你只使用一次“Response.Write”指令来显示字符串。它将不再使用“do...loop”代码,不再对是否位于记录的EOF[末尾]进行条件测试。

Syntax
语法

str = rs.GetString(format,rows,coldel,rowdel,nullexpr)

To create an HTML table with data from a recordset, we only need to use three of the parameters above (all parameters are optional):
我们只需用到上述的所有参数中的三个(上述所有参数都是可选择使用的)就可以创建一个带有记录数据的HTML表:

  • coldel - the HTML to use as a column-separator
    coldel –作为列分隔符使用的HTML代码
  • rowdel - the HTML to use as a row-separator
    rowdel -作为行分隔符使用的HTML代码
  • nullexpr - the HTML to use if a column is NULL
    nullexpr – 如果纵列为空时使用的HTML代码

Note: The GetString() method is an ADO 2.0 feature. You can download ADO 2.0 at http://www.microsoft.com/data/download.htm.
注意:GetString()仅在ADO 2.0中获得支持。你可以通过下列网址下载ADO2.0:http://www.microsoft.com/data/download.htm

In the following example we will use the GetString() method to hold the recordset as a string:
在下面的案例当中,我们将使用GetString()以字符串的的形式获取记录值:

<html>
<body>
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "c:/webdata/northwind.mdb"
set rs = Server.CreateObject("ADODB.recordset")
rs.Open "SELECT Companyname, Contactname FROM Customers", conn
str=rs.GetString(,,"</td><td>","</td></tr><tr><td>","&nbsp;")
%>
<table border="1" width="100%">

<tr>
<td><%Response.Write(str)%></td>
</tr>
</table>
<%
rs.close
conn.close
set rs = Nothing
set conn = Nothing
%>
</body>
</html>

The str variable above contains a string of all the columns and rows returned by the SQL SELECT statement. Between each column the HTML </td><td> will appear, and between each row, the HTML </td></tr><tr><td> will appear. This will produce the exact HTML we need with only one Response.Write.
上述的“Str”变量包含了通过SQL SELECT语句返回的列字符串和行字符串。每个纵列之间将包含“</td><td>”的HTML代码;在每个横行之间将包含“</td></tr><tr><td>”的HTML代码。这样一来,我们只需要使用一次“Response.Write”指令就可以创作出精确的HTML代码了。

评论 (0) All

登陆 | 还没注册?