VB6 SQL connection string in an .ini file


Does anyone remember how to read a ini file for a connection string to SQL Server.

Any code snipets would be greatly welcome

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Include the library 'Microsoft Scripting Runtime' in your project by clicking Project->References

then add the following code to read your .ini file.

Public fso As New FileSystemObject
Public MyFile As TextStream
Public strData as String

Public Sub ReadFile(strFileName As String)
    Set MyFile = fso.OpenTextFile(strFileName, ForReading, , TristateUseDefault)
    Do Until MyFile.AtEndOfStream
        strData = MyFile.Read(FileLen(strFileName))
End Sub

for example if u want to read constr.ini then u can use the following code

ReadFile "<complete path>\constr.ini"

Now strData would consists the content of ur constr.ini file.
I hope this will help you.
YourString = GetIniSetting(strINIFile, "DATABASE", "ConnectionString")

In a module, put the following:

Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

Public Function GetIniSetting(ByVal pstrFileName As String, ByVal pstrSection As String, ByVal pstrKey As String) As String
  Dim lngResult As Long
  Dim strReturn As String
  On Error GoTo TrapIt
  strReturn = Space$(255)
  GetPrivateProfileString pstrSection, pstrKey, vbNullString, strReturn, 254, pstrFileName

  strReturn = Trim$(strReturn)
  If Right$(strReturn, 1) = Chr$(0) Then
    strReturn = Left$(strReturn, Len(strReturn) - 1)
  End If
  GetIniSetting = strReturn
  Exit Function
    msgbox("Error #" & err.number & " occured (" & err.description & ")"
    Exit Function
End Function

In your ini file, it would be setup something like this:

ConnectionString=Provider=sqloledb;Data Source=Aron1;Initial Catalog=pubs;User Id=sa;Password=asdasd;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
scubamikeyAuthor Commented:

You're welcome.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.