在连接string中使用单元格引用

我在我的Excel VBA中使用下面的连接string。 但是我需要设置一个数据库和服务器从像A $ 1 $这样的单元中挑选的方式。 所以我可以在需要时更改数据库的详细信息。

Function Connect(Server As String, _ Database As String) As Boolean Set CN = New ADODB.Connection On Error Resume Next With CN ' Create connecting string .ConnectionString = "Provider=SQLOLEDB.1;" & _ "Integrated Security=SSPI;" & _ "Server=" & Server & ";" & _ "Database=" & Database & ";" ' Open connection .Open End With ' Check connection state If CN.State = 0 Then Connect = False Else Connect = True End If End Function 

提前致谢

就像是:

 Sub check_database_connectivity() Dim server_name As String Dim database_name As String server_name = ActiveSheet.Range("A1").Value database_name = ActiveSheet.Range("A2").Value If Connect(server_name, database_name) = True Then 'do something End If End Sub