Question: Actually my Database connection look like this..., Now i want to introduce Connection Pooling. May i know how to write for this..., 

```
public static string dbpath()
    {
        try
        {
            string strcon = @"Data Source=local\SQLEXPRESS;Initial Catalog=aniv;Integrated Security=True";
           SqlConnection con = new SqlConnection(strcon);
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
            con.Open();
            return strcon;
        }
        catch (SqlException ex)
        {
            MessageBox.Show(ex.Message);
            return "0";
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            return "0";
        }

    }

```

Answer: 
You don't need to do anything particular to get advantage of [connection pooling](https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/sql-server-connection-pooling) mechanism.  

However your code should be changed a bit:

First, you could remove from your code the check for the opened connection

```
SqlConnection con  = new SqlConnection(strcon);
// Has been created here, it is always closed
//if (con.State == ConnectionState.Open)
//{
//    con.Close();
//}

```

Second, return the connection object, not the connection string. (Notice that I have also removed the try/catch blocks, I think that it is better if the exceptional cases will be handled by a more top level procedure than this)

```
public static SqlConnection dbconnection()
{
   string strcon = @"Data Source=local\SQLEXPRESS;Initial Catalog=aniv;Integrated Security=True";
   SqlConnection con = new SqlConnection(strcon);
   con.Open();
   return con;

}

```

This will allow you to write the creation and opening of the connection with the [using statement](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement)
that will assure a proper closing and disposal of your connection object also in case of exceptions.  

So, supposing you need to load a DataTable of customers, you could write something like this

```
using(SqlConnection cn = dbconnection())
{
    string sqlText = "SELECT * FROM Customers ORDER BY CustomerName";
    SqlCommand cmd = new SqlCommand(sqlText, con);
    DataTable dtCustomers = new DataTable();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(dtCustomers);
    // Do what you want with the datatable (bind to a grid, return to do more processing...
}

```

this will keep the number of connection in the pool under the limits and your program will behave correctly in regards to the connection pool
More Reference: [ADO.NET Connection Pooling](http://www.codeproject.com/Articles/17768/ADO-NET-Connection-Pooling-at-a-Glance)

Every connection pool is associated with a distinct connection string and that too, it is specific to the application.

When a connection is first opened, a connection pool is created based on an exact matching algorithm that associates the pool with the connection string in the connection. 

Sample Connection String with Pooling Related Keywords:

```
initial catalog=Northwind; Data Source=localhost; Connection Timeout=30; 
User Id=MYUSER; Password=PASSWORD; Min Pool Size=20; Max Pool Size=200; 
Incr Pool Size=10; Decr Pool Size=5;

```

This way, new connections keep on adding to the pool till **Max Pool Size** is reached, after that when ADO.NET gets request for further connections, it waits for **Connection Timeout** time and then errors out. 

It is highly recommended that we close or dispose the connection once it has served the purpose.

Only connections with the same configuration can be pooled.

The pool is automatically cleared when a fatal error occurs, such as a failover.

Connection string plays a vital role in connection pooling.