[RESOLVED] change connection string at run-time
hi,
im new at this.. so excuse my ignorance. my application is connected to an sql server database (northwind) through a data environment. Is there a way I can change my connection string at run-time?
for ex. I have a comboBox containing databse names " northwind, pubs, dbo ... " when i choose one at run time , i want the connection string to change to that database.. I hope thats clear enough. thanks in advance.
Re: change connection string at run-time
Welcome to VBF !
you can very well do it by closing and opening the connection at any point of time...
if you post your existing connection string then we can give you the code...
Re: change connection string at run-time
Provider=MSDASQL.1;Persist Security Info=False;Data Source=Northwind;Initial Catalog=Form Designer
Re: change connection string at run-time
VB Code:
'Mark Refernce to Microsoft ADO 2.x Library from the Project Meu
Option Explicit
Private Sub CmdConnect_Click()
Dim db As New ADODB.Connection 'for making connection
Dim rs As New ADODB.Recordset 'for opening the record set
Dim sconnectionstring As String 'for connction string
sconnectionstring = "Provider=MSDASQL.1;Persist Security Info=False;Data Source=Northwind;Initial Catalog=Form Designer"
db.Open sconnectionstring 'open the connection
If db.State = 1 Then
MsgBox "Connection succesfull"
Else
MsgBox "Connction Failed"
End If
End Sub
Re: change connection string at run-time
If you want to change database at runtime (select from a combo box) you must change the following line in danasegarane's code
Quote:
Originally Posted by danasegarane
VB Code:
sconnectionstring = "Provider=MSDASQL.1;Persist Security Info=False;Data Source=Northwind;Initial Catalog=Form Designer"
As
VB Code:
dim sCatalog as string
sCatalog = combo1.text
sconnectionstring="Provider=MSDASQL.1;Persist Security Info=False;Data Source=Northwind;"
sconnectionstring= sconnectionstring & "Initial Catalog= " & sCatalog & ""
Re: change connection string at run-time
thanx much.. problem solved