动态Oracle连接字符串

时间:2018-08-17 09:09:34

标签: vba excel-vba

我有一个Excel工作表,该工作表具有到VBA中的oracle数据库的硬编码连接,我想更改为动态连接。下面的代码显示了有效的连接字符串。

Private Function ConnectToSQLDB() As Boolean
Set m_oConnectionObject = New ADODB.Connection

On Error GoTo ErrHandler:

With m_oConnectionObject

.ConnectionString = "Provider=ORAOLEDB.Oracle;" & _
"Data Source=Test; " & _
"User Id=user12345;Password=password12345"    '* Create connecting string
.Open


End With

If m_oConnectionObject.State = 0 Then
ConnectToSQLDB = False
Else
ConnectToSQLDB = True
End If

Exit Function
ErrHandler:
MsgBox "ConnectToSQLDB()"
MsgBox Err.Description
Resume Next
End Function
  

数据库名称=测试用户名= user12345密码= password12345

我尝试将数据库名称,用户名和密码设置为变量,然后通过文本输入框分配变量,但这对我来说并不成功。任何人都有更好的主意。

为进一步澄清,我尝试了以下代码,但对我而言不起作用;

Private Function ConnectToSQLDB() As Boolean
Set m_oConnectionObject = New ADODB.Connection
Dim YourPasswordVariable As String
Dim YourDBNameVariable As String
Dim YourUserIdVariable As String



On Error GoTo ErrHandler:
YourDBNameVariable = InputBox("Enter DB name")
YourUserIdVariable = InputBox("Enter DB username")
YourPasswordVariable = InputBox("Enter DB password")
With m_oConnectionObject

.ConnectionString = "Provider=ORAOLEDB.Oracle;Data Source=" & YourDBNameVariable & "; User Id=" & YourUserIdVariable & ";Password=" & YourPasswordVariable


End With

If m_oConnectionObject.State = 0 Then
    ConnectToSQLDB = False
Else
    ConnectToSQLDB = True
End If

Exit Function
ErrHandler:
MsgBox "ConnectToSQLDB()"
MsgBox Err.Description
Resume Next
End Function

0 个答案:

没有答案