BCA / B.Tech 12 min read

SQL Connection Object

SQL Connection Object in .NET with C# in Hindi 


  • The SQL Connection Object is an object that is used to create and manage the connection between a .NET application and a database.
  • It is found in the System.Data.SqlClient library of the .NET Framework.
  • By using the SQL Connection, we can perform operations like reading, writing, updating, or deleting data in an SQL Server database.
Key Points:

  • Always use the using statement so that the connection is automatically closed.
  • Do not forget to close the connection after opening it.
  • Use try-catch to handle errors.
  • Provide the correct information in the connection string.
  • This process shows a simple and effective use of the SQL Connection Object.
1. Role of the SQL Connection Object: 

  • The main task of the SQL Connection object is to establish a connection between the database and the application.
  • While using it: You have to take care of opening and closing the connection.
  • It works through a connection string, which contains information related to the server name, database name, and security.
2. How to create an SQL Connection Object?

To create an SQL Connection object, we have to use the SqlConnection class. For this, the following steps are taken:

(a) Create a connection string: The connection string provides the necessary information to connect to the database. An example of this is:
string connectionString = "Data Source=SERVER_NAME;Initial Catalog=DATABASE_NAME;User ID=USER_NAME;Password=PASSWORD;";

(b) Create an SQL Connection Object:

using System.Data.SqlClient;

SqlConnection sqlConnection = new SqlConnection(connectionString);

3. How to use SQL Connection in .NET?

To read or update data using an SQL Connection, follow these steps:

(a) Open the SQL connection: sqlConnection.Open();

(b) Use an SQL Command: The SqlCommand object is used to execute an SQL query on the database.

SqlCommand sqlCommand = new SqlCommand("SELECT * FROM Employees", sqlConnection);

(c) Get the data: SqlDataReader is used to read the data.

SqlDataReader reader = sqlCommand.ExecuteReader();

while (reader.Read())
{
    Console.WriteLine(reader["EmployeeName"]);
}

(d) Close the connection:

sqlConnection.Close();
Complete code of the SQL Connection Object:

using System;
using System.Data.SqlClient;

class Program
{
    static void Main()
    {
        string connectionString = "Data Source=SERVER_NAME;Initial Catalog=DATABASE_NAME;User ID=USER_NAME;Password=PASSWORD;";

        // Creating an SQL Connection Object
        using (SqlConnection sqlConnection = new SqlConnection(connectionString))
        {
            try
            {
                // Opening the connection
                sqlConnection.Open();
                Console.WriteLine("Connection Opened Successfully!");

                // Creating and executing an SQL Command
                SqlCommand sqlCommand = new SqlCommand("SELECT * FROM Employees", sqlConnection);
                SqlDataReader reader = sqlCommand.ExecuteReader();

                // Reading the data
                while (reader.Read())
                {
                    Console.WriteLine("Employee Name: " + reader["EmployeeName"]);
                }

                reader.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }
            finally
            {
                // Closing the connection
                sqlConnection.Close();
                Console.WriteLine("Connection Closed.");
            }
        }
    }
}