Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

connecting to SQL server using .ini file

Status
Not open for further replies.

shaminda

Programmer
Jun 9, 2000
170
US
I have heard that you can connect to the SQL Server using a .ini file. So if I have 10 applications that connect to 10 databases on the SQL Server 2000, by writing one .ini file can connect all 10 applications. This way I don’t have cave to create 10 ODBC’s on PCs. So my question is how do I write an .ini file and how do I call it from Visual Basic 6.0 and how do I connect to the server?
 
Hi,

One way would be to just place the connection strings in the ini file and then read them into your app when it loads. I have done this and it works ok if you can trust the users not to mess with the ini file. Another way is to use the registry to store the same infomation. I will see if I can dig up some code for you. If you choose to battle wits with the witless be prepared to lose.
[machinegun][hammer]

[cheers]
 
Hi,

Here is what I do.

I have created a text file which has the .Ini extension. It contains the following line:

127.0.0.1,sa,

The IP address could be replaced with the remote server name and you can add the database password after the last comma.

I use the following code to connect my application to the database server:

Call this sub first. It will call the other function it self.

Sub Establish_Connection()
On Error GoTo Err_Handler

Dim mResponse As String
Set dbConnection = New ADODB.Connection

dbConnection.Open getDBString

If dbConnection.State = adStateOpen Then
frmSplash.Show
Else
mResponse = MsgBox("Unable to open connection.", vbCritical, "Critical Error")
End If

Exit Sub

Err_Handler:
MsgBox Err.Number & Chr(13) & Err.Description & Chr(13) & "Try to run the application again", vbExclamation + vbOKOnly, "Connection: Connection Failure"
End
End Sub

Public Function getDBString() As String
On Error GoTo Err_Handler

Dim fso As New FileSystemObject
Dim fileObj As TextStream
Dim locAry As Variant

If fso.FileExists(App.Path & "\" & "Hrs.ini") Then

Set fileObj = fso_OpenTextFile(App.Path & "\" & "database.ini", ForReading)

'DB Name = 0
'User ID = 1
'Password = 2

locAry = fileObj.ReadAll
locAry = Split(locAry, ",")

getDBString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=dbHRS;Data Source=" & Trim(locAry(0)) & "; User ID=" & Trim(locAry(1)) & ";Password=" & Trim(locAry(2))
End If

Exit Function

Err_Handler:
MsgBox Err.Number & Chr(13) & Err.Description & Chr(13), vbExclamation + vbOKOnly, "Connection: getDBString"

End Function

You will have to include Microsoft Scripting Run Time liabrary to make it work.

This example works with Sql Server only. You will need to change the connection string in your vb code a bit to connnect you appliaction to other Databases.

Send me an email if you need further help.

Regards,

- krshaikh -


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top