Click here to Skip to main content
12,550,111 members (42,396 online)
Click here to Skip to main content
Add your own
alternative version


17 bookmarked

SQL Server Connection Dialog

, 29 Oct 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Show you how to make a connection dialog developed completely from scratch.


Solve the ability to configure connection to SQL Server by using app.config or any other means.

Using the code

For use only need to import System.Configuration

This example gets the connectionstring of app.config for viewing and editing, creating a method in the client form.

Add section connectionStrings in app.config  

    <add name="conn" 
       connectionString="Data Source=;Initial Catalog=master; User Id=sa; Password=123456;Integrated Security=false" 
       providerName="System.Data.sqlclient" />

Opens the specified client configuration file as a Configuration object. 

Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)

Gets the ConnectionStringsSection data for the current application's default configuration.  

Dim connectionString as String = config.ConnectionStrings.ConnectionStrings("conn").Connection 

Save and refresh ConnectionString in current application's default configuration (app.config). 

config.ConnectionStrings.ConnectionStrings("conn").ConnectionString = CS

Complete code form method client. 

Public Function ConnectionDialog() As Boolean
    Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)

    Dim dialog As New SQLServerConnectionDialog()
    dialog.ConnectionString = config.ConnectionStrings.ConnectionStrings("conn").ConnectionString

    If dialog.ShowDialog = Windows.Forms.DialogResult.Cancel Then Return False

    Dim CS As String = dialog.ConnectionString

    config.ConnectionStrings.ConnectionStrings("conn").ConnectionString = CS


    Return True
End Function

To get the list of SQL Server instances registered on the network: 

Dim sqlSources As DataTable = SqlDataSourceEnumerator.Instance.GetDataSources
For Each datarow As DataRow In sqlSources.Rows
    Dim datasource As String = datarow("ServerName").ToString
    If Not datarow("InstanceName") Is DBNull.Value Then
        datasource &= String.Format("\{0}", datarow("InstanceName"))
    End If

To get the list of databases on SQL Server:

Using cn As SqlConnection = New SqlConnection(connString)
    Using cmd As SqlCommand = New SqlCommand()
        cmd.Connection = cn
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "sp_databases"

        Using myReader As SqlDataReader = cmd.ExecuteReader()
            While (myReader.Read())
            End While
        End Using
    End Using
End Using

We build a connectionstring via object SqlConnectionStringBuilder.

Dim conn As New SqlConnectionStringBuilder()
conn.DataSource = "ServerName"
conn.IntegratedSecurity = False
conn.UserID = "sa"
conn.Password = "123456"
conn.InitialCatalog = "DBName"


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Hernán Hegykozi
Team Leader
Argentina Argentina
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionNice article, the MS published SQL Connection Dialog is just to much. Pin
wknopf14-Aug-13 10:52
memberwknopf14-Aug-13 10:52 
AnswerRe: Nice article, the MS published SQL Connection Dialog is just to much. Pin
Hernán Hegykozi27-Aug-13 18:16
memberHernán Hegykozi27-Aug-13 18:16 
GeneralMy vote of 5 Pin
evoisard9-Nov-12 6:13
memberevoisard9-Nov-12 6:13 
GeneralRe: My vote of 5 Pin
Hernán Hegykozi14-Nov-12 6:20
memberHernán Hegykozi14-Nov-12 6:20 
GeneralEste muchacho es comañero mio!!! / This guy is my fellow!!! (orgullo / pride) Pin
Member 91909962-Nov-12 7:02
memberMember 91909962-Nov-12 7:02 
GeneralRe: Este muchacho es comañero mio!!! / This guy is my fellow!!! (orgullo / pride) Pin
Hernán Hegykozi2-Nov-12 7:45
memberHernán Hegykozi2-Nov-12 7:45 
GeneralMy vote of 5 Pin
Nicolas Trillo29-Oct-12 5:53
memberNicolas Trillo29-Oct-12 5:53 
GeneralRe: My vote of 5 Pin
Hernán Hegykozi29-Oct-12 5:55
memberHernán Hegykozi29-Oct-12 5:55 
QuestionNice!! Pin
Member 955484229-Oct-12 4:24
memberMember 955484229-Oct-12 4:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.161021.1 | Last Updated 29 Oct 2012
Article Copyright 2012 by Hernán Hegykozi
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid