`
goldbull
  • 浏览: 27180 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

ASP数据库操作

    博客分类:
  • WEB
阅读更多
<%
'==========================================================================
'文件名称:clsDbCtrl.asp
'功  能:数据库操作类
'作  者:coldstone (coldstone[在]qq.com)
'程序版本:v1.0.5
'完成时间:2005.09.23
'修改时间:2007.10.30
'版权声明:可以在任意作品中使用本程序代码,但请保留此版权信息。
'          如果你修改了程序中的代码并得到更好的应用,请发送一份给我,谢谢。
'轉自:http://www.ezsaler.com/Blog/post/158.html
'==========================================================================

Dim a : a = CreatConn(0, "master", "localhost", "sa", "")    'MSSQL数据库
'Dim a : a = CreatConn(1, "Data/%TestDB%.mdb", "", "", "")    'Access数据库
'Dim a : a = CreatConn(1, "E:\MyWeb\Data\%TestDB%.mdb", "", "", "mdbpassword")
Dim Conn
'OpenConn()    '在加载时就建立的默认连接对象Conn,默认使用数据库a
Sub OpenConn : Set Conn = Oc(a) : End Sub
Sub CloseConn : Co(Conn) : End Sub

Function Oc(ByVal Connstr)
    On Error Resume Next
    Dim objConn
    Set objConn = Server.CreateObject("ADODB.Connection")
    objConn.Open Connstr
    If Err.number <> 0 Then
        Response.Write("<div id=""DBError"">数据库服务器端连接错误,请与网站管理员联系。</div>")
        'Response.Write("错误信息:" & Err.Description)
        objConn.Close
        Set objConn = Nothing
        Response.End
    End If
    Set Oc = objConn
End Function

Sub Co(obj)
    On Error Resume Next
    Set obj = Nothing
End Sub

Function CreatConn(ByVal dbType, ByVal strDB, ByVal strServer, ByVal strUid, ByVal strPwd)
    Dim TempStr
    Select Case dbType
        Case "0","MSSQL"
            TempStr = "driver={sql server};server="&strServer&";uid="&strUid&";pwd="&strPwd&";database="&strDB
        Case "1","ACCESS"
            Dim tDb : If Instr(strDB,":")>0 Then : tDb = strDB : Else : tDb = Server.MapPath(strDB) : End If
            TempStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&tDb&";Jet OLEDB:Database Password="&strPwd&";"
        Case "3","MYSQL"
            TempStr = "Driver={mySQL};Server="&strServer&";Port=3306;Option=131072;Stmt=; Database="&strDB&";Uid="&strUid&";Pwd="&strPwd&";"
        Case "4","ORACLE"
            TempStr = "Driver={Microsoft ODBC for Oracle};Server="&strServer&";Uid="&strUid&";Pwd="&strPwd&";"
    End Select
    CreatConn = TempStr
End Function


Class dbCtrl
    Private debug
    Private idbConn
    Private idbErr
   
    Private Sub Class_Initialize()
        debug = true                    '调试模式是否开启
        idbErr = "出现错误:"
        If IsObject(Conn) Then
            Set idbConn = Conn
        End If
    End Sub
   
    Private Sub Class_Terminate()
        Set idbConn = Nothing
        If debug And idbErr<>"出现错误:" Then Response.Write(idbErr)
    End Sub
   
    Public Property Let dbConn(pdbConn)
        If IsObject(pdbConn) Then
            Set idbConn = pdbConn
        Else
            Set idbConn = Conn
        End If
    End Property
   
    Public Property Get dbErr()
        dbErr = idbErr
    End Property
   
    Public Property Get Version
        Version = "ASP Database Ctrl V1.0 By ColdStone"
    End Property

    Public Function AutoID(ByVal TableName)
        On Error Resume Next
        Dim m_No,Sql, m_FirTempNo
        Set m_No=Server.CreateObject("adodb.recordset")
        Sql="SELECT * FROM ["&TableName&"]"
        m_No.Open Sql,idbConn,3,3
        If m_No.EOF Then
            AutoID=1
        Else
            Do While Not m_No.EOF
                m_FirTempNo=m_No.Fields(0).Value
                m_No.MoveNext
                  If m_No.EOF Then
                        AutoID=m_FirTempNo+1
                  End If
            Loop
        End If
        If Err.number <> 0 Then
            idbErr = idbErr & "无效的查询条件!<br />"
            If debug Then idbErr = idbErr & "错误信息:"& Err.Description
            Response.End()
            Exit Function
        End If
        m_No.close
        Set m_No = Nothing
    End Function

    Public Function GetRecord(ByVal TableName,ByVal FieldsList,ByVal Condition,ByVal OrderField,ByVal ShowN)
        On Error Resume Next
        Dim rstRecordList
        Set rstRecordList=Server.CreateObject("adodb.recordset")
            With rstRecordList
            .ActiveConnection = idbConn
            .CursorType = 3
            .LockType = 3
            .Source = wGetRecord(TableName,FieldsList,Condition,OrderField,ShowN)
            .Open
            If Err.number <> 0 Then
                idbErr = idbErr & "无效的查询条件!<br />"
                If debug Then idbErr = idbErr & "错误信息:"& Err.Description
                .Close
                Set rstRecordList = Nothing
                Response.End()
                Exit Function
            End If   
        End With
        Set GetRecord=rstRecordList
    End Function
   
    Public Function wGetRecord(ByVal TableName,ByVal FieldsList,ByVal Condition,ByVal OrderField,ByVal ShowN)
        Dim strSelect
        strSelect="select "
        If ShowN > 0 Then
            strSelect = strSelect & " top " & ShowN & " "
        End If
        If FieldsList<>"" Then
            strSelect = strSelect & FieldsList
        Else
            strSelect = strSelect & " * "
        End If
        strSelect = strSelect & " from [" & TableName & "]"
        If Condition <> "" Then
            strSelect = strSelect & " where " & ValueToSql(TableName,Condition,1)
        End If
        If OrderField <> "" Then
            strSelect = strSelect & " order by " & OrderField
        End If
        wGetRecord = strSelect
    End Function

    Public Function GetRecordBySQL(ByVal strSelect)
        On Error Resume Next
        Dim rstRecordList
        Set rstRecordList=Server.CreateObject("adodb.recordset")
            With rstRecordList
            .ActiveConnection =idbConn
            .CursorType = 3
            .LockType = 3
            .Source = strSelect
            .Open
            If Err.number <> 0 Then
                idbErr = idbErr & "无效的查询条件!<br />"
                If debug Then idbErr = idbErr & "错误信息:"& Err.Description
                .Close
                Set rstRecordList = Nothing
                Response.End()
                Exit Function
            End If   
        End With
        Set GetRecordBySQL = rstRecordList
    End Function

    Public Function GetRecordDetail(ByVal TableName,ByVal Condition)
        On Error Resume Next
        Dim rstRecordDetail, strSelect
        Set rstRecordDetail=Server.CreateObject("adodb.recordset")
        With rstRecordDetail
            .ActiveConnection =idbConn
            strSelect = "select * from [" & TableName & "] where " & ValueToSql(TableName,Condition,1)
            .CursorType = 3
            .LockType = 3
            .Source = strSelect
            .Open
            If Err.number <> 0 Then
                idbErr = idbErr & "无效的查询条件!<br />"
                If debug Then idbErr = idbErr & "错误信息:"& Err.Description
                .Close
                Set rstRecordDetail = Nothing
                Response.End()
                Exit Function
            End If
        End With
        Set GetRecordDetail=rstRecordDetail
    End Function

    Public Function AddRecord(ByVal TableName, ByVal ValueList)
        On Error Resume Next
        DoExecute(wAddRecord(TableName,ValueList))
        If Err.number <> 0 Then
            idbErr = idbErr & "写入数据库出错!<br />"
            If debug Then idbErr = idbErr & "错误信息:"& Err.Description
            'DoExecute "ROLLBACK TRAN Tran_Insert"    '如果存在添加事务(事务滚回)
            AddRecord = 0
            Exit Function
        End If
        AddRecord = AutoID(TableName)-1
    End Function
   
    Public Function wAddRecord(ByVal TableName, ByVal ValueList)
        Dim TempSQL, TempFiled, TempValue
        TempFiled = ValueToSql(TableName,ValueList,2)
        TempValue = ValueToSql(TableName,ValueList,3)
        TempSQL = "Insert Into [" & TableName & "] (" & TempFiled & ") Values (" & TempValue & ")"
        wAddRecord = TempSQL
    End Function

    Public Function UpdateRecord(ByVal TableName,ByVal Condition,ByVal ValueList)
        On Error Resume Next
        DoExecute(wUpdateRecord(TableName,Condition,ValueList))
        If Err.number <> 0 Then
            idbErr = idbErr & "更新数据库出错!<br />"
            If debug Then idbErr = idbErr & "错误信息:"& Err.Description
            'DoExecute "ROLLBACK TRAN Tran_Update"    '如果存在添加事务(事务滚回)
            UpdateRecord = 0
            Exit Function
        End If
        UpdateRecord = 1
    End Function

    Public Function wUpdateRecord(ByVal TableName,ByVal Condition,ByVal ValueList)
        Dim TmpSQL
        TmpSQL = "Update ["&TableName&"] Set "
        TmpSQL = TmpSQL & ValueToSql(TableName,ValueList,0)
        TmpSQL = TmpSQL & " Where " & ValueToSql(TableName,Condition,1)
        wUpdateRecord = TmpSQL
    End Function

    Public Function DeleteRecord(ByVal TableName,ByVal IDFieldName,ByVal IDValues)
        On Error Resume Next
        Dim Sql
        Sql = "Delete From ["&TableName&"] Where ["&IDFieldName&"] In ("
        If IsArray(IDValues) Then
            Sql = Sql & "Select ["&IDFieldName&"] From ["&TableName&"] Where " & ValueToSql(TableName,IDValues,1)
        Else
            Sql = Sql & IDValues
        End If
        Sql = Sql & ")"
        DoExecute(Sql)
        If Err.number <> 0 Then
            idbErr = idbErr & "删除数据出错!<br />"
            If debug Then idbErr = idbErr & "错误信息:"& Err.Description
            'DoExecute "ROLLBACK TRAN Tran_Delete"    '如果存在添加事务(事务滚回)
            DeleteRecord = 0
            Exit Function
        End If
        DeleteRecord = 1
    End Function
   
    Public Function wDeleteRecord(ByVal TableName,ByVal IDFieldName,ByVal IDValues)
        On Error Resume Next
        Dim Sql
        Sql = "Delete From ["&TableName&"] Where ["&IDFieldName&"] In ("
        If IsArray(IDValues) Then
            Sql = Sql & "Select ["&IDFieldName&"] From ["&TableName&"] Where " & ValueToSql(TableName,IDValues,1)
        Else
            Sql = Sql & IDValues
        End If
        Sql = Sql & ")"
        wDeleteRecord = Sql
    End Function

    Public Function ReadTable(ByVal TableName,ByVal Condition,ByVal GetFieldNames)
        On Error Resume Next
        Dim rstGetValue,Sql,BaseCondition,arrTemp,arrStr,TempStr,i
        TempStr = "" : arrStr = ""
        '给出SQL条件语句
        BaseCondition = ValueToSql(TableName,Condition,1)
        '读取数据
        Set rstGetValue = Server.CreateObject("ADODB.Recordset")
        Sql = "Select "&GetFieldNames&" From ["&TableName&"] Where "&BaseCondition
        rstGetValue.Open Sql,idbConn,3,3
        If rstGetValue.RecordCount > 0 Then
            If Instr(GetFieldNames,",")>0 Then
                arrTemp = Split(GetFieldNames,",")
                For i = 0 To Ubound(arrTemp)
                    If i<>0 Then arrStr = arrStr &Chr(112)&Chr(112)&Chr(113)
                    arrStr = arrStr & rstGetValue.Fields(i).Value
                Next
                TempStr = Split(arrStr,Chr(112)&Chr(112)&Chr(113))
            Else
                TempStr = rstGetValue.Fields(0).Value
            End If
        End If
        If Err.number <> 0 Then
            idbErr = idbErr & "获取数据出错!<br />"
            If debug Then idbErr = idbErr & "错误信息:"& Err.Description
            rstGetValue.close()
            Set rstGetValue = Nothing
            Exit Function
        End If
        rstGetValue.close()
        Set rstGetValue = Nothing
        ReadTable = TempStr
    End Function

    Public Function C(ByVal ObjRs)
        ObjRs.close()
        Set ObjRs = Nothing
    End Function
   
    Private Function ValueToSql(ByVal TableName, ByVal ValueList, ByVal sType)
        Dim StrTemp
        StrTemp = ValueList
        If IsArray(ValueList) Then
            StrTemp = ""
            Dim rsTemp, CurrentField, CurrentValue, i
            Set rsTemp = Server.CreateObject("adodb.recordset")
            With rsTemp
                .ActiveConnection = idbConn
                .CursorType = 3
                .LockType = 3
                .Source ="select * from [" & TableName & "] where 1 = -1"
                .Open
                For i = 0 to Ubound(ValueList)
                    CurrentField = Left(ValueList(i),Instr(ValueList(i),":")-1)
                    CurrentValue = Mid(ValueList(i),Instr(ValueList(i),":")+1)
                    If i <> 0 Then
                        Select Case sType
                            Case 1
                                StrTemp = StrTemp & " And "
                            Case Else
                                StrTemp = StrTemp & ", "
                        End Select
                    End If
                    If sType = 2 Then
                        StrTemp = StrTemp & "[" & CurrentField & "]"
                    Else
                        Select Case .Fields(CurrentField).Type
                            Case 7,133,134,135,8,129,200,201,202,203
                                If sType = 3 Then
                                    StrTemp = StrTemp & "'"&CurrentValue&"'"
                                Else
                                    StrTemp = StrTemp & "[" & CurrentField & "] = '"&CurrentValue&"'"
                                End If
                            Case 11
                                If UCase(cstr(Trim(CurrentValue)))="TRUE" Then
                                    If sType = 3 Then
                                        StrTemp = StrTemp & "1"
                                    Else
                                        StrTemp = StrTemp & "[" & CurrentField & "] = 1"
                                    End If
                                Else
                                    If sType = 3 Then
                                        StrTemp = StrTemp & "0"
                                    Else
                                        StrTemp = StrTemp & "[" & CurrentField & "] = 0"
                                    End If
                                End If
                            Case Else
                                If sType = 3 Then
                                    StrTemp = StrTemp & CurrentValue
                                Else
                                    StrTemp = StrTemp & "[" & CurrentField & "] = " & CurrentValue
                                End If
                        End Select
                    End If
                Next
            End With
            If Err.number <> 0 Then
                idbErr = idbErr & "生成SQL语句出错!<br />"
                If debug Then idbErr = idbErr & "错误信息:"& Err.Description
                rsTemp.close()
                Set rsTemp = Nothing
                Exit Function
            End If
            rsTemp.Close()
            Set rsTemp = Nothing
        End If
        ValueToSql = StrTemp
    End Function

    Private Function DoExecute(ByVal sql)
        Dim ExecuteCmd
        Set ExecuteCmd = Server.CreateObject("ADODB.Command")
        With ExecuteCmd
            .ActiveConnection = idbConn
            .CommandText = sql
            .Execute
        End With
        Set ExecuteCmd = Nothing
    End Function
End Class
%>
分享到:
评论

相关推荐

    ASP 数据库操作 类

    本文将围绕一个名为“ASP数据库操作类”的组件展开讨论,该组件提供了一种简化数据库操作的方式,旨在减少开发者的编码负担,提高开发效率。 #### 二、核心功能 根据提供的描述,“ASP数据库操作类”主要提供了...

    ASP数据库操作类 -ASP源码.zip

    在这个"ASP数据库操作类 - ASP源码.zip"压缩包中,我们可以期待找到一些与ASP相关的数据库操作源代码,这些代码可能用于与后端数据库进行交互,例如数据的查询、添加、修改和删除。 ASP中的数据库操作通常涉及以下...

    一个ASP数据库操作类 代码下载.rar

    这个"ASP数据库操作类 DbCtrl"是一个专为ASP设计的数据库管理工具,旨在简化和加速数据库交互过程。在ASP中,直接进行数据库操作可能会涉及大量的SQL语句和连接处理,而使用DbCtrl类可以有效地封装这些细节,提高...

    asp数据库操作类

    本文将深入探讨"asp数据库操作类"的相关知识点。 一、数据库连接 在ASP中,与数据库建立连接通常使用ADO(ActiveX Data Objects)组件。`ADODB.Connection`对象是实现这一功能的核心。首先,我们需要在页面顶部引入...

    asp数据库操作 读取 连接 修改 增加 删除

    asp数据库操作 读取 连接 修改 增加 删除

    ASP数据库操作解析

    ### ASP数据库操作解析 #### 1. 认识数据库 数据库是一种按照特定格式组织和存储数据的集合。在日常生活中,个人通讯录、公司的账目、客户名单等都可以被视为数据库的一部分。这些数据库不仅具备固定的格式和特性...

    ASP数据库操作类.chm

    ASP数据库操作类.chm &lt;br&gt;欢迎访问我的博客: http://workhelper.blogbus.com

    ASP数据库操作示范-做一个驾驶员考试的网站

    在"ASP数据库操作示范-做一个驾驶员考试的网站"这个项目中,我们将学习如何使用ASP来构建一个模拟驾驶员考试的平台。这个平台的核心功能可能包括题目随机抽取、答案判断以及考试成绩统计等。 首先,我们需要理解ASP...

    ASP数据库操作类源码.rar

    一个ASP数据库操作类源码,这个类可以生成SQL语句、连接数据库、读取库内容、查询数据库、删除指定内容等操作,使用数据库类来操作数据库,是比较方便的,而且修改代码也很方便,ASP中的数据库操作类而且见的也不多...

    asp对数据库操作类

    总的来说,这个ASP数据库操作类提供了一个方便的工具,帮助开发者更高效地进行数据库操作,并通过分页功能优化了数据展示。学习和理解这个类的内部机制,对于提升ASP编程技能,特别是数据库交互部分,将大有裨益。

    ASP操作数据库简单实例

    首先,要进行数据库操作,我们需要建立一个连接。在ASP中,我们可以使用ADO(ActiveX Data Objects)库中的Connection对象来实现。创建Connection对象时,我们需要提供正确的数据库连接字符串,例如: ```vbscript ...

    asp.net数据库操作类

    asp.net数据库操作类asp.net数据库操作类asp.net数据库操作类asp.net数据库操作类asp.net数据库操作类asp.net数据库操作类asp.net数据库操作类asp.net数据库操作类asp.net数据库操作类asp.net数据库操作类asp.net...

    mysql仿asp的数据库操作类

    【MySQL仿ASP数据库操作类详解】 在PHP中,我们可以创建自定义类来简化数据库操作,类似于ASP中的方式。本文将介绍一个PHP类——MySQLDB,它用于处理MySQL数据库的常见操作,如连接、选择数据库、执行SQL语句、处理...

    一款ASP连接数据库的通用操作类

    这款"ASP连接数据库的通用操作类"就是为了简化这个过程而设计的,它提供了封装好的函数和方法,使得开发者能够更加高效、方便地进行数据库操作。 首先,我们要理解什么是数据库连接类。在编程中,连接类通常负责...

    ASP操作数据库教程.rar

    本教程的PDF部分会详细讲解以上概念,并提供实例代码,帮助读者掌握ASP数据库操作。"下载说明.txt"可能包含获取更多资源或使用教程的指引,而"A5下载- 更全的站长资源平台.url"可能是一个链接,指向一个提供更广泛...

    ASP操作数据库教程

    ### ASP操作数据库教程知识点解析 #### 一、前置知识与要求 在开始学习如何使用ASP进行数据库操作之前,有几个基础知识是必要的: ...希望这些信息能够帮助你快速掌握ASP数据库操作的基本技能。

    ASP操作数据库分页,实现增 删 改 查

    ASP(Active Server Pages)是一种微软开发的服务器端...理解这些概念和实践代码,对于初学者来说,是掌握ASP数据库操作的关键。通过不断的练习和实践,你可以更好地理解和运用这些知识,从而在Web开发领域更加熟练。

    ASP.NET数据库操作大全

    ASP.NET数据库操作是Web开发中的核心技能之一,它允许开发者与数据库进行交互,实现数据的存储、检索、修改和删除等功能。以下是对标题和描述中所述知识点的详细说明: 1. **ASP.NET数据库连接**: ASP.NET 提供了...

    ASP.NET数据库操作源码

    这个“ASP.NET数据库操作源码”提供了实现数据库交互的基本功能,包括增、删、改、查以及数据绑定等核心操作。以下是对这些关键知识点的详细说明: 1. **数据库连接与ADO.NET**: ASP.NET通常使用ADO.NET库来与...

Global site tag (gtag.js) - Google Analytics