Connection Class in C#

This is my first article. I don't know how to explain everything but I try my best and I am hoping for good comments to motivate me.

I have noticed that Usually whenever a newbie writes C# code to access a database he/she writes the database access code multiple times for various functions like they write the complete code for each insert, update, delete and so on.

So, I am here to teach you how to make a connection class that I learned from my great and respectable teacher "Sardar Ishtiaq Ahmed". You can use that class in any project where you want to access the database.

Note: You should have knowledge of DataGridView and SqlDataReader to understand its 2 methods. The purpose of this article is to teach you all how to avoid the repitition of the database access code.

OK. Here is the step-by-step method to code the connection class.

Step 1

Import the following namespaces.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Data;  
  4. using System.Data.SqlClient;  
  5. using System.Linq;  
  6. using System.Web;  
Step 2

Create a namespace and a class in it:
  1. namespace ConnectionNamespace  
  2. {  
  3.    public class Connection_Query  
  4.    {  
  5.       //Here put all the methods which I am defining below. . .   
  6.    }  
  7. }  
Step 3

Create a varriable string for saving the connection string and a SqlConnection object:
  1. string ConnectionString = "";  
  2. SqlConnection con;  
Step 4

Create a method to open the connecction. First I make a method OpenConnection() for accessing the database call at the very beginning to open the database connection.
  1. public void OpenConection()  
  2. {  
  3.    con = new SqlConnection(ConnectionString);  
  4.    con.Open();  
  5. }  
Step 5

Now create a CloseConnection method. After completing all the processes call this method to close the connection of the database that we opened in Step 4.
  1. public void CloseConnection()  
  2. {  
  3.    con.Close();  
  4. }  
Step 6

ExecuteQuries Method: Whenever you want to execute a query, like an insert, update or delete query then simply call this function using the object of a class and pass your query to the function.
  1. public void ExecuteQueries(string Query_)  
  2. {  
  3.     SqlCommand cmd = new SqlCommand(Query_,con);  
  4.     cmd.ExecuteNonQuery();  
  5. }  
Step 7

Now create a SqlDataReader Method. Learn how to use the SqlDataReader so you can understand how to use this class. Here I will explain the use of DataReader since you know my focus is the Connection class.

This function will return the data of type SqlDataReader. Make an object of SqlDataReader and call the method in it with the relevent query in it as an argument.
  1. public SqlDataReader DataReader(string Query_)  
  2. {  
  3.     SqlCommand cmd = new SqlCommand(Query_,con);  
  4.     SqlDataReader dr = cmd.ExecuteReader();  
  5.     return dr;  
  6. }  
Step 8

Now show the DataInGridView. As I said earlier, in the case of SqlDataReader you also should have the knowledge of the DataGridView. If you don't then learn about it so you can understant the method and its use.

It will return an object data type value as an object that is the parent of all the variables in C#.

We will initialize the datasource of the GridView with this method and a SQL Query in it as an argument.
  1. public object ShowDataInGridView(string Query_)  
  2. {  
  3.     SqlDataAdapter dr = new SqlDataAdapter(Query_, ConnectionString);  
  4.     DataSet ds = new DataSet();  
  5.     dr.Fill(ds);  
  6.     object dataum = ds.Tables[0];  
  7.     return dataum;  
  8. }  
The following is the complete code of the Connection class:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Data;  
  4. using System.Data.SqlClient;  
  5. using System.Linq;  
  6. using System.Web;  
  7.   
  8. namespace Connection_Class  
  9. {  
  10.     public class Connection_Query  
  11.     {  
  12.   
  13.         string ConnectionString = "";  
  14.         SqlConnection con;  
  15.   
  16.         public void OpenConection()  
  17.         {  
  18.             con = new SqlConnection(ConnectionString);  
  19.             con.Open();  
  20.         }  
  21.   
  22.   
  23.         public void CloseConnection()  
  24.         {  
  25.             con.Close();  
  26.         }  
  27.   
  28.   
  29.         public void ExecuteQueries(string Query_)  
  30.         {  
  31.             SqlCommand cmd = new SqlCommand(Query_,con);  
  32.             cmd.ExecuteNonQuery();  
  33.         }  
  34.   
  35.   
  36.         public SqlDataReader DataReader(string Query_)  
  37.         {  
  38.             SqlCommand cmd = new SqlCommand(Query_,con);  
  39.             SqlDataReader dr = cmd.ExecuteReader();  
  40.             return dr;  
  41.         }  
  42.   
  43.   
  44.         public object ShowDataInGridView(string Query_)  
  45.         {  
  46.             SqlDataAdapter dr = new SqlDataAdapter(Query_, ConnectionString);  
  47.             DataSet ds = new DataSet();  
  48.             dr.Fill(ds);  
  49.             object dataum = ds.Tables[0];  
  50.             return dataum;  
  51.         }  
  52.     }  
  53. }  
Note: the documentation file is also provided with it. 

Class Documentation

This is a class named "Connection_Class" to perform insert, update and delete and search to show the data in a DataGridView and also perform SqlDataReader operations.

Public Member Functions

  1. void OpenConection ()  
Call this method to open the connection.
  1. void CloseConnection ()  
Call this method to close the connection.
  1. void ExecuteQueries (string Query_)  
Call this method to perform insert, delete and update functions.
  1. SqlDataReader DataReader (string Query_)  
Call this method when to show data in a TextBox or Label.
  1. object ShowDataInGridView (string Query_)  
Call this method to show data in a DataGridView.

Member Function Documentation 
  1. void CloseConnection ()

    Call this method to close the connection.
  2. SqlDataReader DataReader (string Query_)

    Call this method when want to show data in a TextBox or Label.

    The following is the code to show how to use this method:
    1. SqlDataReader dr = ClassObject.DataReader("Select * From Student");  
    2. dr.Read();  
    3.   
    4. textBox1.Text = dr["Stdnt_Name"].tostring();  
    Parameters

    Query_ : Pass the query here.

    Returns

    Return SqlDataReader object

  3. void ExecuteQueries (string Query_)

    Call this method to perform insert, delete, update function.

    Parameters

    Query_: your required query.

  4. void OpenConection ()

    Call this method to open the connection.

  5. object ShowDataInGridView (string Query_)

    Call this method to show data in a DataGridView.

    The following is the code to descride how to show data in GridView using it.
    1. dataGridView1.datasource = ClassObject.ShowDataInGridView("Select * From Student")  
    Parameters

    Query_ : Place the required query here.

    Returns:

    return Object with data to Show in GridView

This class was genrated by HammadMaqbool.

Note : Use it with ASP.NET and C#.

Up Next
    Ebook Download
    View all
    Learn
    View all