VB6 SQL connection string in an .ini file

Posted on 2006-04-11
Last Modified: 2013-12-25

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

Any code snipets would be greatly welcome

Question by:scubamikey
    LVL 9

    Expert Comment

    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.
    LVL 11

    Accepted Solution

    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;

    Author Comment


    LVL 11

    Expert Comment

    You're welcome.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
    Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now