Ну вот пара функций, которые обеспечать подключение/отключение с сервером Oracle:
- Код: Выделить всё
Private cnnOra As ADODB.Connection
Private dbOpen As Boolean
Private Enum ConnectionMethodEnum
cmODBC
cmMicrosoftOLEDB
cmOracleOLEDB
cmOLEDBforODBC
End Enum
Private Sub oraConnect(ByVal UserName As String, ByVal UserPassword As String, ByVal DBName As String, Optional ByVal Method As ConnectionMethodEnum = cmMicrosoftOLEDB, Optional ByVal ConnectionLogin As Boolean = False)
Dim str As String
str = ""
Select Case Method
Case ConnectionMethodEnum.cmODBC
str = str & "Driver={Microsoft ODBC for Oracle};"
str = str & "Server=" & DBName & ";"
If ConnectionLogin Then
str = str & "Uid=" & UserName & ";"
str = str & "Pwd=" & UserPassword & ";"
End If
Case ConnectionMethodEnum.cmMicrosoftOLEDB
str = str & "Provider=MSDAORA;"
str = str & "Data Source=" & DBName & ";"
If ConnectionLogin Then
str = str & "User Id=" & UserName & ";"
str = str & "Password=" & UserPassword & ";"
End If
Case ConnectionMethodEnum.cmOracleOLEDB
str = str & "Provider=OraOLEDB.Oracle;"
str = str & "Data Source=" & DBName & ";"
If ConnectionLogin Then
str = str & "User Id=" & UserName & ";"
str = str & "Password=" & UserPassword & ";"
End If
Case ConnectionMethodEnum.cmOLEDBforODBC
str = str & "Provider=MSDASQL;"
str = str & "Driver={Microsoft ODBC for Oracle};"
str = str & "Server=" & DBName & ";"
If ConnectionLogin Then
str = str & "Uid=" & UserName & ";"
str = str & "Pwd=" & UserPassword & ";"
End If
End Select
Set cnnOra = New ADODB.Connection
On Error Resume Next
If ConnectionLogin Then
cnnOra.Open ConnectionString:=str
Else
cnnOra.Open ConnectionString:=str, UserID:=UserName, Password:=UserPassword
End If
dbOpen = (Err = 0)
On Error GoTo 0
End Sub
Private Sub oraDisconnect()
If Not dbOpen Then Exit Sub
If cnnOra.State = ADODB.adStateOpen Then
cnnOra.Close
Set cnnOra = Nothing
dbOpen = False
End If
End Sub
Только учти, если ты хочешь ИЗМЕНЯТЬ данные, то провайдер Microsoft, используемый по умолчанию, не подойдет. Используй другой (лучше Oracle)
Можно добавлять данные как с помощью запросов (INSERT INTO ... VALUES ...), так и с помощью рекордсета (rs.AddNew ... rs.Update)
Ну а вот пример работы:
- Код: Выделить всё
Private Sub ...
Dim I As Long
Dim rsData As ADODB.Recordset, SQL As String
oraConnect "user1", "pwd1", "myoradb", cmOracleOLEDB
If Not dbOpen Then Exit Sub
'Первый способ (через запрос)
SQL = "insert into user1.item_list (item_id, item_name) values (1, 'test 1');"
cnnOra.Execute SQL
'Второй способ (через рекордсет)
Set rsData = cnnOra.Execute("item_list",,adCmdTable)
rsData.AddNew
rsData.Fields("item_id") = 2
rsData.Fields("item_name") = "test 2"
rsData.Update
rsData.Close
Set rsData = Nothing
oraDisconnect
End Sub
Lasciate ogni speranza, voi ch'entrate.