Results 1 to 3 of 3

Thread: Runtime database connection config

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2004
    Posts
    8

    Runtime database connection config

    Is there a way to create/configure a database connection at runtime, using a visual interface? Is there a readily available component that does that?

    Eg. using something like the control panel Data Sources, but within the application itself

  2. #2
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: Runtime database connection config

    What are you tring to achieve? IF you can be more specific, probably there might be a better way of doing that.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Runtime database connection config

    That functionality is actually built into windows. You can create an empty file with a ".udl" extension and then pass its path to Process.Start. This will open Data Link Properties dialogue for that file. The user can then configure the connection as desired and the result will be saved to the file when they press OK. The file is just a text file so you can open it in Notepad to see what it contains. Once the user has saved their desired settings you can read the file using a StreamReader and copy the connection string it contains to the ConnectionString property of an appropriate Connection object. If you want to preset some of the parts of the connection string then you can write something to the file before calling Process.Start. Here's some example code to get you started:
    VB Code:
    1. Dim tempPath As String = IO.Path.Combine(Application.StartupPath, "temp.udl")
    2.  
    3.         'Create an empty file.
    4.         Dim sw As New IO.StreamWriter(tempPath)
    5.         sw.Close()
    6.  
    7.         'Open the Data Link dialogue.
    8.         Dim p As Process = Process.Start(tempPath)
    9.  
    10.         'Wait for the user to finish setting the properties.
    11.         p.WaitForExit()
    12.  
    13.         'Show the contents of the file.
    14.         MessageBox.Show(My.Computer.FileSystem.ReadAllText(tempPath))
    15.  
    16.         'Delete the file.
    17.         IO.File.Delete(tempPath)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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