---
title: "Establish connection in SqlServer."  
description: "Hi.  In this blog, I will teach you that how to establish the connection to sql server in ado.net. This is first basic program through which you learn"  
author: "Anonymous User"  
published: 2011-05-14  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/171/establish-connection-in-sqlserver  
category: "database"  
tags: ["database"]  
reading_time: 4 minutes  

---

# Establish connection in SqlServer.

Hi.\

In this blog, I will teach you that how to establish the connection to [sql server](https://www.mindstick.com/articles/12999/what-is-table-valued-function-in-sql-server) in ado.net. This is first basic program through which you learn that how to establish connection.

1. For creating any program in ADO.Net basically, you need two namespaces. One is System.Data which contains classes for [disconnected](https://www.mindstick.com/blog/300962/how-modern-life-disconnected-from-nature) [architecture](https://www.mindstick.com/articles/249193/top-5-reasons-to-pursue-architecture) such as DataSet, [SqlDataAdapter](https://www.mindstick.com/interview/33991/what-is-sqldataadapter) etc and other one is System.Data.SqlClient [namespace](https://www.mindstick.com/articles/12552/namespace) which contains classes such as [SqlConnection](https://www.mindstick.com/forum/1209/sqlconnection-sqlcommand-sqldatareader-idisposable), SqlDataReader, [SqlCommand](https://www.mindstick.com/forum/91/difference-between-sqlcommand-and-sqlcommandbuilder) etc. For making this program please import these two namespaces.

```
using System.Data;             //Namespace required for ado.net programing.using System.Data.SqlClient;   //Namespace required for ado.net programing.
```

\
2. Now create a property named getConnectionString which will return [connection string](https://www.mindstick.com/articles/328/connection-strings) required to establish a connection from a database. A connection string is something like a string which contains all information required to establish the connection from the database. Such as DataBase name which is passed in InitialCatalog keyword, name of server which is passed in [DataSource](https://www.mindstick.com/blog/447/listing-the-databases-connected-to-the-datasource) keyword. Then create a property like this.

```
/// <summary>        /// This property will return connection string required to establish connection to desired server.        /// </summary>        public static string getConnectionString        {            get            {                return "Data Source=AAA-PC;User Id=sa;Password=aaaaa;Initial Catalog=WorkBook";            }        }
```

3. No finally create a method named establishConnection() which will check connection establish or not like following example demonstrate.

```
/// <summary>        /// This method will return boolean type value. If it returns true it means connection successfully        /// establish and if it returns false then it means some problem occurs while establishing connection to server.        /// </summary>        /// <returns></returns>        public static bool establishConnection()        {            bool status = false;             try            {                con = new SqlConnection();   //Create an object of SqlConnection class. This class will reside in System.Data.SqlClient namespace.                con.ConnectionString = getConnectionString;     //Pass connection string to SqlConnection class by calling ConnectionString property of SqlConnection class.                con.Open();                                     //Open connection by calling Open() method of SqlConnection class.                status = true;    //Make status to be true flag.            }            catch            {                status = false;   //If any exception is generated then make status to be false. This means connection is not established.            }            finally            {                if (con != null)                    con.Close();    //Finally close connection.            }            return status;        }
```

Now finally execute method and see the output.

\
Example of complete program

```
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;             //Namespace required for ado.net programing.using System.Data.SqlClient;   //Namespace required for ado.net programing.namespace ConnectionDemo{    class Program    {        /// <summary>        /// This property will return connection string required to establish connection to desired server.        /// </summary>        public static string getConnectionString        {            get            {                return "Data Source= AAA-PC;User Id=sa;Password=aaaaa;Initial Catalog=WorkBook";            }        }        /// <summary>        /// This method will return boolean type value. If it returns true it means connection successfully        /// establish and if it returns false then it means some problem occurs while establishing connection to server.        /// </summary>        /// <returns></returns>        public static bool establishConnection()        {            bool status = false;            try            {                con = new SqlConnection();                      //Create an object of SqlConnection class. This class will reside in System.Data.SqlClient namespace.                con.ConnectionString = getConnectionString;     //Pass connection string to SqlConnection class by calling ConnectionString property of SqlConnection class.                con.Open();                                     //Open connection by calling Open() method of SqlConnection class.                status = true;                                  //Make status to be true flag.            }            catch            {                status = false;                              //If any exception is generated then make status to be false. This means connection is not established.            }            finally            {                if (con != null)                    con.Close();                         //Finally close connection.            }            return status;        }        private static SqlConnection con = null;       //Create a reference variable of SqlConnection class.        static void Main(string[] args)        {            if (establishConnection())          //Check whether connection establish or not.                Console.WriteLine("Great! You have successfully establish connection...");       //Display success message.            else                 Console.WriteLine("Sorry! Unable to establish connection from server..\nPlease check connection string.");   //Display failure message.        }    }}
```

Thanks...

---

Original Source: https://www.mindstick.com/blog/171/establish-connection-in-sqlserver

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
