Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Attribute VB_Name = "dbSettings"
- Public Function ConnectionString() As String
- Dim sqlSettings() As Variant
- Dim SqlServer, SqlDBName, SqlUserName, SqlPassword As String
- 'read in sql parameters
- sqlSettings = getSettings
- 'move parameters into readable variable names
- SqlServer = sqlSettings(0)
- SqlDBName = sqlSettings(1)
- SqlUserName = sqlSettings(2)
- SqlPassword = sqlSettings(3)
- 'construct connection string
- ConnectionString = "Provider=sqloledb;data source=" & SqlServer _
- & ";initial catalog=" & SqlDBName _
- & ";user id=" & SqlUserName _
- & ";password=" & SqlPassword
- End Function
- Private Function getSettings() As Variant
- Dim myFSO As New FileSystemObject
- Dim path As String
- Dim fileName As String
- Dim sqlSettings() As Variant
- path = "C:\path\to\file\"
- fileName = "dbSettings.ini"
- i = 0
- x = 0
- Set fso = myFSO.OpenTextFile(path + fileName)
- 'ignore comments (begind with hyphen)
- 'accept setting as variable in array (begins after trailing space after colon)
- Do Until fso.AtEndOfStream
- txt = fso.ReadLine
- ReDim Preserve sqlSettings(i)
- For x = 1 To Len(txt)
- If Mid(txt, x, 1) = "-" Then
- Exit For
- End If
- If Mid(txt, x, 1) = ":" Then
- sqlSettings(i) = sqlSettings(i) + Mid(txt, x + 2, Len(txt))
- Exit For
- End If
- Next
- i = i + 1
- Loop
- getSettings = sqlSettings
- fso.Close
- LogDiagnosticsMessage "Loaded SQL settings from file"
- End Function
Add Comment
Please, Sign In to add comment