为ASP封装的数据库操作类

<%

'site: http://blog.youkuaiyun.com/yagas

'email: yagas60@21cn.com

'author: yagas


'游标类型
Const adOpenForwardOnly     = 0
Const adOpenKeyset          = 1
Const adOpenDynamic         = 2
Const adOpenStatic          = 3
'锁类型
Const adLockReadOnly        = 1
Const adLockPessimistic     = 2
Const adLockOptimistic      = 3
Const adLockBatchOptimistic = 4

Class Database
 Private conn, dbStr
 
 '**
 ' 构造函数
 Private Sub Class_Initialize()
  Set conn = Server.CreateObject("ADODB.Connection")
 End Sub
 
 '**
 ' 析构函数
 Private Sub Class_Terminate()
  Set rest = Nothing
  Set conn = Nothing
 End Sub

 '**
 ' 设置数据库路径
 Public Sub connStr(FilePath)
  dbStr = "provider=microsoft.jet.oledb.4.0;data source=" & server.mappath(FilePath)
 End Sub
 
 '**
 ' 链接数据库
 Public Sub open()
  conn.open dbStr
 End Sub
 
 '**
 ' 获取单条数据
 Public Function find(table, conditions, order, fields)
  Dim SQL, rest
  Set rest = Server.CreateObject("ADODB.Recordset")
  
  If fields="" Then fields = "*" End If

  SQL = "SELECT TOP 1 " & fields & " FROM [" & table & "]"
  If IsArray(conditions) Then
   SQL = SQL & " WHERE " & Join(conditions, " AND ")
  ElseIf conditions<>"" Then
   SQL = SQL & " WHERE " & conditions
  End If
  
  If order<>"" Then
   SQL = SQL & " ORDER BY " & order
  End If

  rest.open SQL, conn, adOpenStatic, adLockReadOnly
  
  Set rs = rest
  'rest.Close
  Set find = rs
 End Function

 '**
 ' 分页查询数据
 Public Function findAll(table, conditions, order, page, rows, fields)
  Dim rest, Format, SQL, Where, OutRows, Params(3), Params1(1)  
  Set rest = Server.CreateObject("ADODB.Recordset")
  
  Format    = "SELECT TOP %s %s FROM [%s]"
  Where     = " WHERE %s "

  OutRows = page * rows
  Params(0) = rows
  Params(1) = fields
  Params(2) = table
  '基础查询
  SQL = sprintf(Format, Params)
  '组合查询条件
  If IsArray(conditions) Then
   Params1(0) = Join(conditions, " AND ")
   SQL = SQL & sprintf(Where, Params1)
  ElseIf conditions<>"" Then
   Params1(0) = conditions
   SQL = SQL & sprintf(Where, Params1)
  End If
  '如果不是第一页的内容
  If OutRows>0 Then
   Params(0) = OutRows
   Params(1) = "[id]"
   Params(2) = table
   If IsArray(conditions) Or conditions<>"" Then
    SQL = SQL & " AND [id] NOT IN (" & sprintf(Format, Params) & ") "
   Else
    SQL = SQL & " WHERE [id] NOT IN (" & sprintf(Format, Params) & ") "
   End If
  End If


  '是否进行排序
  If order<>"" Then
   SQL = SQL & " ORDER BY " & order
  End If
  
  rest.open SQL, conn, adOpenStatic, adLockReadOnly
  Set findAll = rest
 End Function


End Class

 

 

 

'**
' 格式化字符串
Function sprintf(Format, params)
 Dim strArr, num, newStr
 strArr = Split(Format, "%s")
 If(UBound(strArr)<>UBound(params)) then
  sprintf = Format
  Exit Function
 End If

 For num = LBound(strArr) To UBound(strArr)
  newStr = newStr & strArr(num) & params(num)
 Next
 sprintf = newStr
End Function
%>

 

---------------------------------------------------------------

'使用方法

<%
Dim db
Dim news

Set db = New Database
db.connStr "#database.mdb"
db.open


Set news = db.find("ieb_articles", "", "", "*")
If news.BOF and news.EOF Then
 Response.Write "没有数据"
Else
 Response.Write news("title") & VBCRLF
End If
news.Close

 

Set news = db.findAll("ieb_articles", "", "", 2, 3, "*")
if news.BOF and news.EOF Then
 Response.Write "没有数据"
 news.Close
Else
 Do While Not news.EOF
  Response.Write news("title") & VBCRLF
  news.MoveNext
 Loop
End If
%>

 

 

未完,待续....

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yagas

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值