萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> oracle教程 >> VB連接ORACLE數據庫

VB連接ORACLE數據庫

'打開數據庫

Public Sub OpenOraDB()
On Error GoTo ToExit
OraDB_Open = False
Set OraDB = New ADODB.Connection
OraConstr = "Provider=OraOLEDB.Oracle.1;Password=" & strOraPWD & ";User ID=" & strOraUser & ";Data Source=" & OraDBNetName & ";Persist Security Info=False"
OraDB.CursorLocation = adUseServer

OraDB.Open OraConstr
OraDB_Open = True

Exit Sub
ToExit:
'MsgBox "連接數據庫服務器錯誤,您可以在網絡正常後繼續使用。",vbInformation , "錯誤信息"
OraDB_Open = False
End Sub

'關閉數據庫

Public Sub CloseOraDB()
If OraDB_Open = True Then
If (OraDB.State = adStateOpen) Then
OraDB.Close
Set OraDB = Nothing
OraDB_Open = False
End If
End If
End Sub

'打開數據集,寫入數據

Set OraRS = New ADODB.Recordset
OraRS.ActiveConnection = OraDB
OraRS.CursorLocation = adUseServer
OraRS.LockType = adLockBatchOptimistic
strOraRS = "select * from " & OraDBtablename
OraRS.Open strOraRS, OraDB, adOpenStatic, adLockOptimistic
OraRS.AddNew
OraRS.Fields("PID") = strOraPID
OraRS.Fields("pname") = strName").Value
OraRS.Fields("psex") = strPsex
OraRS.Update

'關閉數據集

OraRS.Close
Set OraRS = Nothing
copyright © 萬盛學電腦網 all rights reserved