為ASP封裝的資料庫操作類

2021-04-23 04:12:09 字數 3542 閱讀 5683

'游標型別

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

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

%>

未完,待續....

asp資料庫操作類

class quickdb private conn,connstr private sqldatabasename,sqlpassword,sqlusername,sqllocalname,sqlnowstring public rs private sub class initialize sq...

資料庫操作類的封裝

public static string constring server localhost integrated security true database northwind 1.執行sql語句,返回受影響的行數 code string constring server localhost ...

ASP資料庫操作通用類

class quickdb private conn,connstr private sqldatabasename,sqlpassword,sqlusername,sqllocalname,sqlnowstring public rs private sub class initialize sq...