Dynamic Connection String of MS SQL Database in C#

Introduction

This article shows how to get a dynamic server name that is installed in the system and using that name show a list of the existing databases. Then you will use a connecting string dynamically in the project.

  1. First create a Windows Form application
  2. In your system if another version of Microsoft SQL is already installed then download the project and use DLL file in your project. Then, go to the References and right-click on References and click on Add Reference then go to the Browse button.

    Referenc Manager

    Figure 1:Refrence Manager

Go to the directory where both of the DLL files are and add them to the project.

Dll File

Figure 2: DLL Files

If you have installed Microsoft SQL then go to Assemblies and find the following:

Microsoft.SqlServer.ConnectionInfo
Microsoft.SqlServer.Management.Sdk.Sfc
Microsoft.SqlServer.Smo
Microsoft.SqlServer.SqlEnum

All Dll Files

Then create the following GUI application:

GUI Application

Figure 3: GUI Application

Add the following namespaces:

  1. using Microsoft.SqlServer.Management.Smo;  
  2. using System.Data.SqlClient;  
Then write the following code:
  1. public partial class Form1: Form   
  2. {  
  3.     string serverName;  
  4.     public SqlConnectionStringBuilder connectionString = new SqlConnectionStringBuilder();  
  5.   
  6.     public Form1()   
  7.     {  
  8.         InitializeComponent();  
  9.     }  
  10.   
  11.     private void Form_Load(object sender, EventArgs e)   
  12.     {  
  13.         DataTable dataTable = SmoApplication.EnumAvailableSqlServers(true);  
  14.         listBox1.ValueMember = "Name";  
  15.         listBox1.DataSource = dataTable;  
  16.     }  
  17.   
  18.     private void listBox1_SelectedIndexChanged(object sender, EventArgs e)   
  19.     {  
  20.   
  21.         listBox2.Items.Clear();  
  22.         if (listBox1.SelectedIndex != -1)   
  23.         {  
  24.             serverName = listBox1.SelectedValue.ToString();  
  25.   
  26.             Server server = new Server(serverName);  
  27.             try   
  28.             {  
  29.                 foreach(Database database in server.Databases)   
  30.                 {  
  31.                     listBox2.Items.Add(database.Name);  
  32.                 }  
  33.             } catch (Exception ex)   
  34.             {  
  35.                 string exception = ex.Message;  
  36.             }  
  37.         }  
  38.     }  
  39.   
  40.     private void submit_Click(object sender, EventArgs e)   
  41.     {  
  42.         connectionString.DataSource = serverName;  
  43.         if (listBox2.SelectedIndex != -1)   
  44.         {  
  45.             connectionString.InitialCatalog = listBox2.SelectedItem.ToString();  
  46.             connectionString.IntegratedSecurity = true;  
  47.             this.Hide();  
  48.             Splash frm = new Splash(connectionString);  
  49.             frm.Show();  
  50.   
  51.         } else   
  52.         {  
  53.             MessageBox.Show("Please Select Database Name");  
  54.             listBox2.Focus();  
  55.         }  
  56.   
  57.     }  
  58.   
  59.     private void Cancel_Click(object sender, EventArgs e)   
  60.     {  
  61.         Application.Exit();  
  62.     }  
Splash is our second Windows Form that prints connectingString. Here we want to tell you that you can use this connectingString in any Windows Forms form using the following code.

Add Namespace
  1. using System.Data.SqlClient;  
Make SqlConnectionStringBuilder
  1. public SqlConnectionStringBuilder cs;  
Add Method in Class:
  1. public Splash(SqlConnectionStringBuilder con)   
  2. {  
  3.     InitializeComponent();  
  4.     cs = con;  
  5. }  
Output

Figure 4 is the GIF image displaying the output.
 
Spalash Window

 

Figure 4: Splash Window

Thank you.