如何在连接数组string(Excel VBA)中指定数据库用户凭据

请帮助修改连接数组string(Excel 2010),以便能够接收来自字段UID=;PWD=Initial Catalog的单元格引用的input。

 .Connection= Array("OLEDB;Provider=MSDASQL.1;Persist Security Info=True;Extended Properties=""DSN=NZSQL;Database=consumerdb;Servername=192.54.97.102;", "UID=USERNAME;PWD=****;Port=5480;ReadOnly=0;SQLBitOneZero=0;LegacySQLTables=0;NumericAsChar=0;ShowSystemTables=0;LoginTimeout=0;QueryTimeout=0;DateFormat=1;SecurityLevel=preferredUnSecured;CaCertFile="";Initial Catalog=CONSUMERDB_USERNAME") 

Connectionstring是一个简单的string数组。 您需要find包含用户名和密码的那个,然后将该部分分开并插入单元格中的值。 把连接string的部分放在不同的行上来帮助分解它。 分解每个部分,如果它有助于隔离您要操纵的子string。

 .Connection = Array("OLEDB;Provider=MSDASQL.1;Persist Security Info=True;" & _ "Extended Properties=""DSN=NZSQL;Database=consumerdb;Servername=192.54.97.102;", _ "UID=USERNAME;PWD=****;" & _ "Port=5480;ReadOnly=0;SQLBitOneZero=0;LegacySQLTables=0;NumericAsChar=0;" & _ "ShowSystemTables=0;LoginTimeout=0;QueryTimeout=0;DateFormat=1;" & _ "SecurityLevel=preferredUnSecured;CaCertFile="";Initial Catalog=CONSUMERDB_USERNAME" _ ) 

现在,如果很容易find用户名和密码,并将值连接到该部分。

 Dim usr As String, pwd As String usr = Range("A1").Value pwd = Range("B1").Value .Connection = Array("OLEDB;Provider=MSDASQL.1;Persist Security Info=True;" & _ "Extended Properties=""DSN=NZSQL;Database=consumerdb;Servername=192.54.97.102;", _ "UID=" & usr & ";PWD=" & pwd & ";" & _ "Port=5480;ReadOnly=0;SQLBitOneZero=0;LegacySQLTables=0;NumericAsChar=0;" & _ "ShowSystemTables=0;LoginTimeout=0;QueryTimeout=0;DateFormat=1;" & _ "SecurityLevel=preferredUnSecured;CaCertFile="";Initial Catalog=CONSUMERDB_USERNAME" _ )