【问题标题】:Excel VBA to parse SQL Connection StringsExcel VBA 解析 SQL 连接字符串
【发布时间】:2016-09-20 23:57:02
【问题描述】:

我有一个使用连接字符串连接到 SQL DB 的 Excel VBA,但我想知道是否可以从单元格中解析为连接字符串?例如,如果我在单元格B2 中有SQLSVR1\Instance1,在B3 中有Databas1,我可以将其放入连接字符串中,如下所示:

objMyConn.ConnectionString = "Provider=sqloledb; Data Source=**"B2"**; Initial Catalog=**"B3"**; Integrated Security=SSPI;"

这可能吗?

提前致谢。

【问题讨论】:

    标签: sql excel vba connection-string oledb


    【解决方案1】:

    完全有可能:

    objMyConn.ConnectionString =  "Provider=sqloledb; Data Source=" & range("B2").value & "; Initial Catalog=" & range("B3").value &"; Integrated Security=SSPI;"
    

    这基本上将您的字符串与这些单元格中的值连接起来。

    Range.Value 文件。

    【讨论】:

      【解决方案2】:

      是的,正如 EoinS 提到的那样。但请注意确定您的工作表。对于您想在另一个工作表上定义连接字符串的情况。试试这样的:

      Dim wb as workbook, ws as worksheet
      
      Set wb = Thisworkbook
      Set ws = wb.Worksheets("TheNameofYourWorksheetsWithYourConnectionsDefined")
      
      ''Get Cnxn details
      sDsn = wsc.Cells(wsc.Range("YourLabeloftheCell").Row + 1,   wsc.Range("YourLabeloftheCell").Column).Value
      sUser = wsc.Cells(wsc.Range("YourLabeloftheCell").Row + 2, wsc.Range("YourLabeloftheCell").Column).Value
      sPwd = wsc.Cells(wsc.Range("YourLabeloftheCell").Row + 3, wsc.Range("YourLabeloftheCell").Column).Value
      
      sSql= GetRangeString(Worksheets("SQL").Range("A17:A22"))
      check = DbSelectQueryToSheet(sDsn, "", sUser, sPwd, sSql, Worksheets("TargetWorksheet").Range("A1"), True)
      

      希望,这会有所帮助!

      【讨论】:

      • 感谢您的帮助 - 非常感谢
      猜你喜欢
      • 1970-01-01
      • 2012-09-26
      • 2019-12-01
      • 2013-12-29
      • 1970-01-01
      • 2021-11-04
      • 2012-01-21
      • 1970-01-01
      • 2022-01-16
      相关资源
      最近更新 更多