Guest User

Untitled

a guest
Nov 30th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. Attribute VB_Name = "dbSettings"
  2.  
  3. Public Function ConnectionString() As String
  4. Dim sqlSettings() As Variant
  5. Dim SqlServer, SqlDBName, SqlUserName, SqlPassword As String
  6.  
  7. 'read in sql parameters
  8. sqlSettings = getSettings
  9.  
  10. 'move parameters into readable variable names
  11. SqlServer = sqlSettings(0)
  12. SqlDBName = sqlSettings(1)
  13. SqlUserName = sqlSettings(2)
  14. SqlPassword = sqlSettings(3)
  15.  
  16. 'construct connection string
  17. ConnectionString = "Provider=sqloledb;data source=" & SqlServer _
  18. & ";initial catalog=" & SqlDBName _
  19. & ";user id=" & SqlUserName _
  20. & ";password=" & SqlPassword
  21. End Function
  22.  
  23. Private Function getSettings() As Variant
  24. Dim myFSO As New FileSystemObject
  25. Dim path As String
  26. Dim fileName As String
  27. Dim sqlSettings() As Variant
  28.  
  29. path = "C:\path\to\file\"
  30. fileName = "dbSettings.ini"
  31. i = 0
  32. x = 0
  33.  
  34. Set fso = myFSO.OpenTextFile(path + fileName)
  35.  
  36. 'ignore comments (begind with hyphen)
  37. 'accept setting as variable in array (begins after trailing space after colon)
  38. Do Until fso.AtEndOfStream
  39. txt = fso.ReadLine
  40. ReDim Preserve sqlSettings(i)
  41. For x = 1 To Len(txt)
  42. If Mid(txt, x, 1) = "-" Then
  43. Exit For
  44. End If
  45. If Mid(txt, x, 1) = ":" Then
  46. sqlSettings(i) = sqlSettings(i) + Mid(txt, x + 2, Len(txt))
  47. Exit For
  48. End If
  49. Next
  50. i = i + 1
  51. Loop
  52.  
  53. getSettings = sqlSettings
  54.  
  55. fso.Close
  56.  
  57. LogDiagnosticsMessage "Loaded SQL settings from file"
  58.  
  59. End Function
Add Comment
Please, Sign In to add comment