VBScript 教程之数据库篇,以 vbscript DBHelper 类的方式,封装数据库连接、查询、基本的存储过程访问方法。
option Explicit
' 数据库读取选项
Public Const adOpenStatic = 3
Public Const adLockReadOnly = 1
Public Const adLockOptimistic = 3
Public Const adCmdStoredProc = 4
Public Const adInteger=3
Public Const adChar=129
Public Const adVarchar = 200
Public Const adDate=7
Public Const adParamInput=1
Public Const adParamReturnValue=4
Public Const LogTypeInfo = 0
Public Const LogTypeError = 1
Public Const LogTypeWarning = 2
Class DBHelper
    Private oConn
    Private Sub Class_Initialize
        Set oConn = Nothing
    End Sub
' ***************************************************************************
    ' 创建 ADODB.Connection 对象,连接数据库
    ' ***************************************************************************
    Public Function Connect(server, database, uid, password)
        Dim sDSNRef
        Dim sMsg
' 创建 ADO 数据库连接对象
        On Error Resume Next
        Set oConn = CreateObject("ADODB.Connection")
        If Err then
            ShowMessage "错误 - 无法创建 ADODB.Connection 对象, 不能查询 SQL Server: " & Err.Description & " (" & Err.Number & ")", LogTypeError
            Set Connect = Nothing
            Exit Function
        End If
        On Error Goto 0
' 构建连接字符串
        sDSNRef = "Provider=SQLOLEDB;OLE DB Services=0;Data Source=" & server & ";Initial Catalog=" & database
        sDSNRef = sDSNRef & ";User ID=" & uid & ";Password=" & password
' 正在连接数据库
        ShowMessage "Connecting to SQL Server using connect string: " & sDSNref, LogTypeInfo
        On Error Resume Next
        oConn.Open sDSNref
        If Err then
            sMsg = Err.Description & " (" & Err.Number & ")"
ShowMessage "error opening SQL connection: " & sMsg, LogTypeError
iRetVal = Failure
            ShowMessage "error opening SQL Connection: " & sMsg, LogTypeError
            For each objErr in oConn.Errors
                ShowMessage "  ADO error: " & objErr.Description & " (Error #" & objErr.Number & "; Source: " & objErr.Source & "; SQL State: " & objErr.SQLState & "; NativeError: " & objErr.NativeError & ")", LogTypeError
            Next
            Err.Clear
            Set Connect = Nothing
            Exit Function
        End If
        On Error Goto 0
ShowMessage "Successfully opened connection to database.", LogTypeInfo
' Return the connection to the caller
        Set Connect = oConn
    End Function
    ' ***************************************************************************
    ' 创建 ADODB.Recordset 对象,执行数据库查询,返回 Recordset 对象。
    ' ***************************************************************************
    Public Function Query(strSQL)
        Dim oRS
        ' Create ADO recordset object
        On Error Resume Next
        Set oRS = CreateObject("ADODB.Recordset")
        If Err then
            Set Query = Nothing
            ShowMessage "ERROR - Unable to create ADODB.Recordset object, impossible to query SQL Server: " & Err.Description & " (" & Err.Number & ")", LogTypeError
            Exit Function
        End If
        On Error Goto 0
' Issue the SQL statement
        ShowMessage "About to issue SQL statement: " & strSQL, LogTypeInfo
        On Error Resume Next
        oRS.Open strSQL, oConn, adOpenStatic, adLockReadOnly
        If Err then
            Set Query = Nothing
            ShowMessage "ERROR - Opening Record Set (Error Number = " & Err.Number & ") (Error Description: " & Err.Description & ").", LogTypeError
            For each objErr in oConn.Errors
                ShowMessage "  ADO error: " & objErr.Description & " (Error #" & objErr.Number & "; Source: " & objErr.Source & "; SQL State: " & objErr.SQLState & "; NativeError: " & objErr.NativeError & ")", LogTypeError
            Next
            oRS.Close
            Err.Clear
            Exit Function
        End If
        On Error Goto 0
ShowMessage "Successfully queried the database.", LogTypeInfo
        Set Query = oRS
    End Function
    ' ***************************************************************************
    ' 创建 ADODB.Command 对象,执行数据库存储过程,返回 Command 对象。
    ' 注意:存储过程要求,没有返回值对象,返回值以Select方式返回,存储过程需要使用
    ' set nocount on,禁用影响行数消息。
    ' ***************************************************************************
    Public Function ExecuteProc(strSQL)
        Dim oComm,oRS
        ' Create ADO recordset object
        On Error Resume Next
        Set oComm = CreateObject("ADODB.Command")
        If Err then
            Set ExecuteProc = Nothing
            ShowMessage "ERROR - Unable to create ADODB.Command object, impossible to query SQL Server: " & Err.Description & " (" & Err.Number & ")", LogTypeError
            Exit Function
        End If
        On Error Goto 0
        ' 分解参数
        Dim sql,cmd,parm,parms,p,index
        sql = Split(strSQL," ")
        If UBound(sql) = 1 Then
            cmd = sql(0)
            parm = Right(strSQL,Len(strSQL)-Len(cmd))
            parms = Split(parm,",")
            index = 0
            For Each p In parms
                p = LTrim(Replace(p,"'",""))
                Dim para
                Set para = CreateObject("ADODB.Parameter")
                para.Name = index
                para.Type = adVarchar
                para.Size = 1000
                para.Direction = adParamInput
                para.Value = p
                oComm.Parameters.Append para
                index = index + 1
            Next
        Else
            cmd = strSQL
        End If
' Issue the SQL statement
        ShowMessage "About to issue SQL statement: " & strSQL, LogTypeInfo
        On Error Resume Next
        oComm.CommandType = adCmdStoredProc
        oComm.ActiveConnection = oConn
        oComm.CommandText = cmd
        Set oRS = oComm.Execute
        If Err then
            Set ExecuteProc = Nothing
            ShowMessage "ERROR - Opening Command (Error Number = " & Err.Number & ") (Error Description: " & Err.Description & ").", LogTypeError
            For each objErr in oConn.Errors
                ShowMessage "  ADO error: " & objErr.Description & " (Error #" & objErr.Number & "; Source: " & objErr.Source & "; SQL State: " & objErr.SQLState & "; NativeError: " & objErr.NativeError & ")", LogTypeError
            Next
            oComm.Close
            Err.Clear
            Set ExecuteProc = Nothing
            Exit Function
        End If
        On Error Goto 0
ShowMessage "Successfully queried the database.", LogTypeInfo
        Set ExecuteProc = oRS
    End Function
End Class

转载于:https://blog.51cto.com/qijinchao/263270

VBScript 教程之数据库篇相关推荐

  1. [Qt教程] 第24篇 数据库(四)SQL查询模型QSqlQueryModel

    [Qt教程] 第24篇 数据库(四)SQL查询模型QSqlQueryModel 楼主  发表于 2013-5-21 14:33:47 | 查看: 869| 回复: 1 SQL查询模型QSqlQuery ...

  2. [Qt教程] 第25篇 数据库(五)SQL表格模型QSqlTableModel

    [Qt教程] 第25篇 数据库(五)SQL表格模型QSqlTableModel 楼主  发表于 2013-5-21 20:36:22 | 查看: 923| 回复: 7 SQL表格模型QSqlTable ...

  3. [Qt教程] 第26篇 数据库(六)SQL关系表格模型QSqlRelationalTableModel

    [Qt教程] 第26篇 数据库(六)SQL关系表格模型QSqlRelationalTableModel 楼主  发表于 2013-5-21 20:54:13 | 查看: 677| 回复: 6 SQL关 ...

  4. [Qt教程] 第23篇 数据库(三)利用QSqlQuery类执行SQL语句

    [Qt教程] 第23篇 数据库(三)利用QSqlQuery类执行SQL语句 楼主  发表于 2013-5-15 22:39:29 | 查看: 813| 回复: 0 利用QSqlQuery类执行SQL语 ...

  5. [Qt教程] 第22篇 数据库(二)编译MySQL数据库驱动

    [Qt教程] 第22篇 数据库(二)编译MySQL数据库驱动 楼主  发表于 2013-5-13 21:28:02 | 查看: 1616| 回复: 12 编译MyQSL数据库驱动 版权声明 该文章原创 ...

  6. [Qt教程] 第21篇 数据库(一)Qt数据库应用简介

    [Qt教程] 第21篇 数据库(一)Qt数据库应用简介 楼主  发表于 2013-5-13 20:56:39 | 查看: 1403| 回复: 13 Qt数据库应用简介 版权声明 该文章原创于作者yaf ...

  7. MySQL数据库篇---对数据库,数据库中表,数据库中表的记录进行添修删查操作---保姆级教程

    MySQL数据库知识点整理,保姆级教程 MySQL数据库存储方式 sql简介 SQL分类 DDL: 数据定义语言 DCL: 数据控制语言 DML:数据操控语言 DQL: 数据查询语言 SQL的使用 S ...

  8. Systemd 入门教程:实战篇

    Systemd 入门教程:实战篇 原文出处: 阮一峰(@ruanyf)   http://blog.jobbole.com/98671/?utm_source=blog.jobbole.com& ...

  9. [Qt教程] 第27篇 XML(一)使用DOM读取XML文档

    [Qt教程] 第27篇 XML(一)使用DOM读取XML文档 楼主  发表于 2013-5-21 21:14:28 | 查看: 1001| 回复: 14 使用DOM读取XML文档 版权声明 该文章原创 ...

最新文章

  1. QT学习第8课:QT计算器界面实现
  2. WPF中的容器控件——Grid
  3. 劝大家逃离互联网!某前互联网员工自述:从互联网到传统行业,工资多,不加班,有户口,能买房!...
  4. 阿里短信 ajax,阿里大于 短信 注册验证 ajax返回数据的问题
  5. 为RHEL5安装JDK和配置tomcat
  6. 天猫不搞双十一“开玩笑”
  7. Sqlmap爆库命令的简单使用
  8. Confluence 6 在升级过程中查看合并日志
  9. 我的Java之路(7)
  10. windows 配置squid反向代理服务器
  11. UltraCompare 22 for Mac(文件比较工具)
  12. ASP.net发布项目引用了C++DLL后页面提示找不到指定模块的异常
  13. 脚本小子_Lua深入了解函数
  14. iOS第三方支付——银联支付
  15. 【华人学者风采】杨鸣波 四川大学
  16. CAD命令栏窗口跑到屏幕外面怎么找回来
  17. 双减背景下小学中年级语文单元整体作业设计的实践研究
  18. NEO源码分析之UTXO全局资产
  19. 计算机基础与程序设计(基于C语言)学习笔记
  20. shell编程入门(一天掌握shell编程)

热门文章

  1. Visual Studio “Orcas” Beta 2 开始发布多语种版本 包含简体中文
  2. 使用Solr 增加索引以及检索
  3. 典型环节的matlab仿真分析,典型环节的MATLAB仿真.doc
  4. 6个座位办公室最佳位置_一天中6个最佳的护肤时间,你知道几个?
  5. hdfs安全模式退出_浅谈HDFS(二)之NameNode与SecondaryNameNode
  6. Zookeeper基于Java 访问-权限控制的案例演示
  7. ArrayBlockingQueue原理分析-take方法
  8. springboot集成rocketmq消费者
  9. Java 扫描并加载包路径下class文件
  10. 注解_自定义注解_元注解