Dynamic Connection Class In Windows Form


In this article we discuss the dynamic connection class in C# because in the beginner’s level most of students are clear about the inheritance and connection class, but can’t use this dynamic approach in a professional project. Basically object-oriented programming solves many issues and makes our programming much easier. When you read this article you'll have a good knowledge about object-oriented programming.

What is the benefit of Dynamic Connection Class?

  • The benefit of dynamic class is that when you make software you put a connection string respective to your computer.

  • When you integrate this software and database into another computer then it gives an error: why?

  • Because your PC name and your client's PC name is different.

  • In this way we make a dynamic class because when you make a dynamic class it automatically takes a PC name and sets  as a connection string.

Step 1: Now we start, first we create the Windows Form Based Project then after choosing Windows Form Project we press OK Button.

Windows Form Project

Step 2: After creating the project we add the class and change the name of the class and set the name as Connection Class. Left click on you project and add class and press add button.

Add class

Step 3: Now we create the database in Microsoft Access 2013 name as EmployeeDatabase and name the table Employee.


Step 4:
Now we add a connection string in App.config file you only give the accurate name of your database and nothing else.

App.config file

Step 5: After creating the class we make some method in class then we inherit this method in our program. First we add two namespaces in our program; if you are using Microsoft Access then add these two namespaces in your class. If you are using SQL Server then you add another two libraries which are similar to Microsoft Access  -- only change OLEDB to SQL.



Step 5: Left click on your reference which is shown in your project and then click on assemblies and add System.Configuration in your project then press Ok button.

Step 6: Now we design the user interface to insert the employee information by dragging and dropping some labels and textboxes and buttons


Step 6: Now double click on the Save  button and now we don't  need any open and closed connection we only make instance of class and then use it.


Step 7: Now we make the insert query and inherit all methods which we declare in class and use it.


  1. privatevoid button1_Click(object sender, EventArgs e)  
  2. {  
  3.     con = newDynamicConnection();  
  4.     con.mysqlconnection();  
  5.     con.sqlquery("INSERT INTO Employee([Name],[Father_Name],[Department],[Email_Address],[Phone_Number])VALUES(@Name,@Father_Name,@Department,@Email_Address,@Phone_Number)");  
  6.     con.cmd.Parameters.AddWithValue("@Name", textBox1.Text).ToString();  
  7.     con.cmd.Parameters.AddWithValue("@Father_Name", textBox2.Text).ToString();  
  8.     con.cmd.Parameters.AddWithValue("@Department", textBox3.Text).ToString();  
  9.     con.cmd.Parameters.AddWithValue("@Email_Address", textBox4.Text).ToString();  
  10.     con.cmd.Parameters.AddWithValue("@Phone_Number", textBox5.Text).ToString();  
  11.     con.nonqueryex();  
  12.     MessageBox.Show("Data Is Save");  
  13. }  
Step 8: Now when you code on a button then press the Run button and put the information all and press Save button, now the all the information are is saved inthe employee table and you can see also in the database table.

press Save button

You see in the database as well the record is inserted in database.


Read more articles on Windows Forms:

Up Next
    Ebook Download
    View all
    View all