Results 1 to 6 of 6

Thread: [RESOLVED] change connection string at run-time

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2006
    Location
    Jerusalem
    Posts
    39

    Resolved [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.

  2. #2
    VB Guru ganeshmoorthy's Avatar
    Join Date
    Dec 2005
    Location
    Sharjah, United Arab Emirates
    Posts
    3,031

    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...
    If an answer to your question has been helpful, then please, Rate it!

    Have done Projects in Access and Member management systems using BioMetric devices, Smart cards and BarCodes.


  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2006
    Location
    Jerusalem
    Posts
    39

    Re: change connection string at run-time

    Provider=MSDASQL.1;Persist Security Info=False;Data Source=Northwind;Initial Catalog=Form Designer

  4. #4
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: change connection string at run-time

    VB Code:
    1. 'Mark Refernce to Microsoft ADO 2.x Library from the Project Meu
    2. Option Explicit
    3. Private Sub CmdConnect_Click()
    4.     Dim db As New ADODB.Connection 'for making connection
    5.     Dim rs As New ADODB.Recordset   'for opening the record set
    6.     Dim sconnectionstring As String 'for connction string
    7.     sconnectionstring = "Provider=MSDASQL.1;Persist Security Info=False;Data Source=Northwind;Initial Catalog=Form Designer"
    8.     db.Open sconnectionstring 'open the connection
    9.     If db.State = 1 Then
    10.         MsgBox "Connection succesfull"
    11.     Else
    12.         MsgBox "Connction Failed"
    13.     End If
    14.  
    15. End Sub
    Please mark you thread resolved using the Thread Tools as shown

  5. #5
    Hyperactive Member Chathura's Avatar
    Join Date
    Nov 2005
    Location
    Sri Lanka
    Posts
    345

    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:
    1. sconnectionstring = "Provider=MSDASQL.1;Persist Security Info=False;Data Source=Northwind;Initial Catalog=Form Designer"
    As
    VB Code:
    1. dim sCatalog as string
    2.   sCatalog = combo1.text
    3.   sconnectionstring="Provider=MSDASQL.1;Persist Security Info=False;Data Source=Northwind;"
    4. sconnectionstring= sconnectionstring & "Initial Catalog= " & sCatalog & ""

  6. #6

    Thread Starter
    Member
    Join Date
    Nov 2006
    Location
    Jerusalem
    Posts
    39

    Re: change connection string at run-time

    thanx much.. problem solved

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width