---
title: "ASP求取数据更简约轻便型"
author: "aksoft"
date: "2010-10-11T10:37:46Z"
tags: []
categories: ["编程语言"]
slug: "asp"
draft: false
lastmod: "2010-10-11T10:37:46Z"
id: 781115
url: "https://age.iteye.com/blog/781115"
---

做ASP好几年了，经常都在写重复的代码，重复率估计是每个程序员都无法避免的。
我们如何提高工作效率，把重复的时间放在学习其他内容上呢？那么我们只有用精简的语句达到最佳的效果。
以下是我用了N久的一种ASP 记录求取 function 函数结构。

'整体记录循环输出结构

```java
function getsql(a,b)
Set rs = conn.Execute(""&a&"")
if not rs.eof then
while not rs.eof
c=c&replace(replace(replace(b,"rs0",rs(0)),"rs1",rs(1)),"rs2",rs(2))
rs.movenext
wend
end if
rs.close
getsql=c
end function
```


'使用方法

```java
response.write getsql("select top 5 id,title,time from [table] order by id desc","<a href=""index_rs0.html"">rs1</a> rs2")
```


'函数说明
以上getsql区域 SQL+HTML样式
这个时候

```java
c=c&replace(replace(replace(b,"rs0",rs(0)),"rs1",rs(1)),"rs2",rs(2))
```


id 在function 被对应rs0 替换
title 在function 被对应rs1 替换
time 在function 被对应rs2 替换

function包中的rs(0),rs(1),rs(2)是对应的求取记录列名，并不是传递的函数，这个大家要注意，往往初学者容易把这里混淆。

HTML结构部分可以根据自己的循环需求去编写，并非固定的格式。SQL结构也可以根据自己的需求去调整，包括字段顺序、top条数、排序方法及where等。

最后我们得到的结果就是:

```java
<a href=""index_5.html"">标题1</a> 2010-10-9
<a href=""index_4.html"">标题2</a> 2010-10-8
<a href=""index_3.html"">标题3</a> 2010-10-7
<a href=""index_2.html"">标题4</a> 2010-10-6
<a href=""index_1.html"">标题5</a> 2010-10-5
```