使用VB6.0访问ORACLE数据库

其他 专栏收录该内容
18 篇文章 0 订阅

首先要安装ORACLE客户端或服务器端,建好DB以及SERVICE,连接成功后就可以开始了,以下是VB访问ORACLE的代码:

Option Explicit
'以下是一些变量以及参数的定义
'DB的名称
Public Const strdbname = "DBNAME"
'用户名
Public Const strusr = "BLUESKY35"
'密码
Public Const strpwd = "PASSWORD"
'OraSession
Public OraSession As Object
'OraDatabase
Public OraDB As OraDatabase
'OraDynaset
Public OraDyn As OraDynaset

'连接ORACLE的函数
'连接成功:true  连接失败:false

Public Function f_connect() As Boolean
    
    On Error GoTo ierr
        f_connect = False
        Set OraSession = CreateObject("OracleInProcServer.XOraSession")
        Set OraDB = OraSession.OpenDatabase(strdbname, strusr & "/" & strpwd, 0&)
        f_connect = True
    Exit Function
    
ierr:
        MsgBox Err.Description, vbOKOnly + vbCritical, "Error"
        f_connect = False
End Function

'通过传入SQL语句从数据库的表里取得记录
Public Function f_select(strsql As String) As OraDynaset

    On Error GoTo ierr
        Set OraDyn = OraDB.CreateDynaset(strsql, ORADYN_READONLY)
        Set f_select = OraDyn
    Exit Function
    
ierr:
        MsgBox Err.Description, vbOKOnly + vbCritical, "Error"
        Set f_select = Nothing
End Function

'执行更新语句,返回受本次更新影响的记录数
Public Function f_execute(strsql As String) As Boolean

    On Error GoTo ierr
        f_execute = False
        OraDB.BeginTrans
        f_execute = OraDB.ExecuteSQL(strsql)
    Exit Function
    
ierr:
        MsgBox Err.Description, vbOKOnly + vbCritical, "Error"
        OraDB.Rollback
End Function

'断开连接
'断开成功:true  断开失败:false

Public Function f_disconnect() As Boolean

    On Error GoTo ierr
        f_disconnect = False
        If Not OraDyn Is Nothing Then
            OraDyn.Close
        End If
        Set OraSession = Nothing
        OraDB.Close
        Set OraDB = Nothing
        f_disconnect = True
    Exit Function
    
ierr:
        MsgBox Err.Description, vbOKOnly + vbCritical, "Error"
        f_disconnect = False
End Function

将以上代码放入模块,直接调用即可.

  • 2
    点赞
  • 0
    评论
  • 5
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值