3 Replies - 9231 Views - Last Post: 13 April 2012 - 11:27 AM Rate Topic: -----

#1 tbzserge  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 26-January 11

VB6:Connect to SQL server 2005 via connection string in .INI textfile

Posted 13 April 2012 - 02:55 AM

Hi there.I was trying to connect to sql server 2005 via text file( with .ini extension) using vb6.I didn't want to hard-code it in my program so that i can use different connection string on different place.
Below is the connection string that i have written in SETTINGS.INI file.

[Connection_String]
Provider = sqloledb
Data_Source= GF\SQLEXPRESS
Initial_Catalog= C1
Integrated_Security= SSPI
User_ID =
Password =

Here is what i have done so far,
    
Dim MyConnect As New ADODB.Connection
Dim MyRecSet As New ADODB.Recordset
Dim strSQL As String
Dim COMMFilePath As String
COMMFilePath = GetINISetting("COMM", "FilePath", App.Path & "\")

           MyConnect.Open _
           "Provider = sqloledb;" & _
           "Data Source = Trim(GetINISetting("Connection_String", "Data_Source", App.Path & "\SETTINGS.INI"));" & _
           "Initial Catalog = Trim(GetINISetting("Connection_String", "Initial_Catalog", App.Path & "\SETTINGS.INI"));" & _
           "Integrated Security = Trim(GetINISetting("Connection_String", "Integrated_Security", App.Path & "\SETTINGS.INI"));" & _
           "User ID = Trim(GetINISetting("Connection_String", "User_ID", App.Path & "\SETTINGS.INI"));" & _
           "Password = Trim(GetINISetting("Connection_String", "Password", App.Path & "\SETTINGS.INI"));"
'My other code below....



I have tried using a textbox to display the string from the INI file and it works using the Trim function.
So,the only thing i could figure is there is syntax error in my vb6 code and i have searched high and low for it but still couldn't find it.

Anyone that can guide or help me on this? Any help would be much appreciated.Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: VB6:Connect to SQL server 2005 via connection string in .INI textfile

#2 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: VB6:Connect to SQL server 2005 via connection string in .INI textfile

Posted 13 April 2012 - 08:28 AM

You're confusing strings and functions. Each function call has to be separated from each string segment, and you've messed that up. So, for example:
"Initial Catalog = Trim(GetINISetting("Connection_String", "Initial_Catalog", App.Path & "\SETTINGS.INI"));" & _
should read
"Initial Catalog = " & Trim(GetINISetting("Connection_String", "Initial_Catalog", App.Path & "\SETTINGS.INI")) & ";" & _


To avoid this sort of problem (which is common), it helps to visualize the result:
"Initial Catalog = myDatabase;"
Now, pull myDatabase out of that string using concatenation:
"Initial Catalog = " & "myDatabase" & ";"
Then, substitute your function call for myDatabase:
"initial Catalog = " & GetMyDatabaseString(arg1, arg2, whatever) & ";"
See? You'll need to fix the other ones where you've messed them up as well and you should be fine.

This post has been edited by BobRodes: 13 April 2012 - 08:29 AM

Was This Post Helpful? 0
  • +
  • -

#3 tbzserge  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 26-January 11

Re: VB6:Connect to SQL server 2005 via connection string in .INI textfile

Posted 13 April 2012 - 11:24 AM

Hi BobRodes,

Thanks for helping me pointing that out. Yes,i am finally able to call the connection string from text file perfectly now.
Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: VB6:Connect to SQL server 2005 via connection string in .INI textfile

Posted 13 April 2012 - 11:27 AM

You're most welcome.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1