论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > Asp教程
Tag:入门,文摘,实例,技巧,iis,表单,对象,上传,数据库,记录集,session,cookies,存储过程,注入,分页,安全,优化,xmlhttp,fso,jmail,application,防盗链,stream,组件,md5,乱码,缓存,加密,验证码,算法,ubb,正则表达式,水印,,日志,压缩,url重写,控件,函数,破解,触发器,socket,ADO,初学,聊天室,留言本,视频教程

asp数据库操作类

文章类别:Asp | 发表日期:2010-1-26 13:53:08

示例:
复制内容到剪贴板代码:
<%
Dim DB,rs,i,temp
Set DB = New DB_
'感谢小秦同学的回复,之前竟忘连接数据库了。-_!!
DB.Connection("acsql") = "data.mdb"
'执行sql查询
Set rs = DB("select * from [tableName] where [条件]")
Set rs = Nothing
'分页,默认每页20条记录
DB.pagesize = 30
Set rs = DB("page:select * from [tableName]")
For i = 0 To 29
    If rs.Eof Then Exit For
Next
Set rs = Nothing
'输出默认分页样式
Response.Write(DB.pager())
'插入新记录,无需判断数据类型,自动生成sql语句
DB("fieldName") = value
DB("fieldName2") = value
DB("insert:tableName")
'更新记录
DB("fieldName") = value
DB("fieldName2") = value
DB("where:id=1")
DB("update:tableName")
'删除记录
DB("where:id=1")
DB("delete:tableName")
Set DB = Nothing
%>
复制内容到剪贴板代码:
<%
Class DB_
    Private conn,rs,i,length,tablePrefixKey,tablePrefixValue,temp
    Private sql,sqlSet,sqlItemKey,sqlItemValue,sqlWhere
    Public rowsAffected
    Public pagesize,pageID,page,pagesCount
    Private pageDatasCount,pageCookiesID
    Private Sub Class_Initialize()
        i = 0
        length = 0
        tablePrefixKey = ""
        tablePrefixValue = "^o^"
        temp = ""
        sql = ""
        sqlSet = ""
        sqlItemKey = ""
        sqlItemValue = ""
        pagesize = 20
        pageID = "page"
        page = 1
        pageCookiesID = ""
    End Sub
    Private Sub class_terminate()
        If IsObject(rs) Then rs.Close : Set rs = Nothing
        If IsObject(conn) Then conn.Close : Set conn = Nothing
    End Sub
    Public Property Let Prefix(Byval k,Byval v)
        tablePrefixKey = k
        tablePrefixValue = v
    End Property
    Public Property Let Connection(Byval k,Byval v)
        Select Case LCase(k)
            Case "acsql","access"
                Dim connstr
                connstr = "Provider=Microsoft.Jet.OLEdb.4.0;Data Source=" & Server.MapPath(v)
                Set conn = Server.CreateObject("Adodb.connection")
                conn.Open connstr
            Case "mssql"
                Dim connstr
                temp = Split(v,":")
                connstr = "Provider=Sqloledb;Data Source=" & temp(0) & ";Initial Catalog=" & temp(1) & ";User ID=" & temp(2) & ";Password=" & temp(3) & ";"
                Set conn = Server.CreateObject("Adodb.Connection")
                conn.Open connstr
        End Select
    End Property
    Public Property Let Default(Byval k,Byval v)
        temp = VarType(v)
        sqlItemKey = sqlItemKey & "," & k
        Select Case temp
            Case 1,2,3,4,5,6,11
                sqlSet = sqlSet & "," & k & "=" & v
                sqlItemValue = sqlItemValue & "," & v
            Case 7
                sqlSet = sqlSet & "," & k & "=#" & v & "#"
                sqlItemValue = sqlItemValue & ",#" & v & "#"
            Case Else
                sqlSet = sqlSet & "," & k & "='" & Replace(v,"'","''") & "'"
                sqlItemValue = sqlItemValue & ",'" & Replace(v,"'","''") & "'"
        End Select
    End Property
    Private Function Query(Byval s)
        s = Replace(s,tablePrefixKey,tablePrefixValue,1,1,1)
        Set Query = conn.Execute(s,rowsAffected,&H0001)
    End Function
    Private Function Alter(Byval s,Byval type_)
        Alter = False
        If Trim(s) = "" Then Exit Function
        sqlSet = Replace(sqlSet,",","",1,1)
        sqlItemKey = Replace(sqlItemKey,",","",1,1)
        sqlItemValue = Replace(sqlItemValue,",","",1,1)
        If sqlWhere <> "" Then
            If LCase(Left(sqlWhere,3)) = "and" Then
                sqlWhere = " where " & Replace(sqlWhere,"and","",1,1,1)
            Else
                sqlWhere = " where " & sqlWhere
            End If
        End If
        Select Case type_
            Case "insert" : sql = "INSERT INTO " & s & "(" & sqlItemKey & ") VALUES(" & sqlItemValue & ")"
            Case "update" : sql = "UPDATE " & s & " SET " & sqlSet & sqlWhere
            Case "delete" : sql = "DELETE * FROM " & s & sqlWhere
        End Select
        sqlSet = ""
        sqlItemKey = ""
        sqlItemValue = ""
        sqlWhere = ""
        Query(sql)
        Alter = True
    End Function
    Public Default Property Get Default(Byval s)
        temp = LCase(Left(Trim(s),6))
        Select Case True
            Case temp = "insert" : Default = Alter(Replace(s,"insert:","",1,1,1),"insert")
            Case temp = "update" : Default = Alter(Replace(s,"update:","",1,1,1),"update")
            Case temp = "delete" : Default = Alter(Replace(s,"delete:","",1,1,1),"delete")
            Case temp = "where:" : sqlWhere = Replace(s,"where:","",1,1,1)
            Case InStr(temp,"page:")=1 : Set Default = ExecPage(Replace(s,"page:","",1,1,1))
            Case Else : Set Default = Query(s)
        End Select
    End Property
    Private Function ExecPage(Byval s)
        If IsObject(rs) Then rs.Close : Set rs = Nothing
        pageCookiesID = pageCookiesID & pageID
        pageDatasCount = Request.Cookies("O:temp:page")(pageCookiesID)
        page = Request.QueryString(pageID)
        If Not IsNumeric(page) Or page = "" Then page = 1
        page = Int(page)
        If Not IsNumeric(pageDatasCount) Or pageDatasCount = "" Then pageDatasCount = 0
        Set rs = Server.CreateObject("Adodb.RecordSet")
        rs.Open Replace(s,tablePrefixKey,tablePrefixValue,1,1,1),conn,1,1,&H0001
        If pageDatasCount = 0 Then
            Response.Cookies("O:temp:page")(pageCookiesID) = rs.RecordCount
            pageDatasCount = Request.Cookies("O:temp:page")(pageCookiesID)
        End If
        If Not rs.Eof Then
            pagesCount = -Int(-pageDatasCount / pagesize)
            If page > pagesCount Then page = pagesCount
            rs.AbsolutePosition = Abs((page-1)) * pagesize + 1
            If page > pagesCount Then page = pagesCount
            If page < 1 Then page = 1
        End If
        Set ExecPage = rs
    End Function
    Public Function Pager()
        Dim return_,pageStart,pageEnd,pageMarge
        pageStart = 1 : pageMarge = 11
        If page > pageMarge / 2 Then pageStart = Int(page + 1 - pageMarge / 2)
        PageEnd = pageStart + pageMarge - 1
        If pageEnd > pagesCount Then pageEnd = pagesCount
        return_ = "<div class=""page""><em>" & pageDatasCount & "|Page:" & page & "/" & pagesCount & "</em>"
        If page > 1 Then
            return_ = return_ & "<a href=""?" & pageID & "=1"">&laquo;</a><a href=""?" & pageID & "=" & page - 1 & """>&lt;</a>"
        End If
        For i = pageStart To pageEnd
            If i = page Then
                return_ = return_ & "<span>" & i & "</span>"
            Else
                return_ = return_ & "<a href=""?" & pageID & "=" & i & """>" & i & "</a>"
            End If
        Next
        If page < pagesCount Then
            return_ = return_ & "<a href=""?" & pageID & "=" & page + 1 & """>&gt;</a><a href=""?" & pageID & "=" & pagesCount & """>&raquo;</a>"
        End If
        return_ = return_&"<kbd><input type=""text"" size=""3"" style=""width:40px; text-align:center;"" onkeydown=""if(event.keyCode==13 && !(this.value=='')) {window.location='?" & pageID & "='+this.value; return false;}"" /></kbd></div>"
        Pager = return_
    End Function
End Class
%>
数据库操作类中分页部分总数据cookies名请自行更改,我最初用的是MD5加密后的sql语句
视频教程列表
文章教程搜索
 
Asp推荐教程
Asp热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058