Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1272
  • Last Modified:

VB6 SQL connection string in an .ini file

Hello,

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

Any code snipets would be greatly welcome
Thanks,

SM
0
scubamikey
Asked:
scubamikey
  • 2
1 Solution
 
pradapkumarCommented:
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))
    Loop
    MyFile.Close
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.
0
 
leclairmCommented:
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
 
TrapIt:
    msgbox("Error #" & err.number & " occured (" & err.description & ")"
    Exit Function
End Function

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

[DATABASE]
ConnectionString=Provider=sqloledb;Data Source=Aron1;Initial Catalog=pubs;User Id=sa;Password=asdasd;
0
 
scubamikeyAuthor Commented:
Perfect,
Thanks,

SM
0
 
leclairmCommented:
You're welcome.
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now