使用VB6.0访问ORACLE数据库

类别:Java 点击:0 评论: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

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

本文地址:http://com.8s8s.com/it/it15260.htm