Use Connection Pooling - CSharp Database

CSharp examples for Database:Connection

Description

Use Connection Pooling

Demo Code


using System;//www . ja  va2 s.co  m
using System.Data.SqlClient;

class MainClass
    {
        public static void Main()
        {
            using (SqlConnection con = new SqlConnection())
            {
                // Configure the SqlConnection object's connection string.
                con.ConnectionString =
                    @"Data Source = .\sqlexpress;" +// local SQL Server instance
                    "Database = Northwind;" +       // the sample Northwind DB
                    "Integrated Security = SSPI;" + // integrated Windows security
                    "Min Pool Size = 5;" +          // configure minimum pool size
                    "Max Pool Size = 15;" +         // configure maximum pool size
                    "Connection Reset = True;" +    // reset connections each use
                    "Connection Lifetime = 600";    // set max connection lifetime

                con.Open();

                // Access the database...

            }

            using (SqlConnection con = new SqlConnection())
            {
                // Configure the SqlConnection object's connection string.
                con.ConnectionString =
                    @"Data Source = .\sqlexpress;" +//local SQL Server instance
                    "Database = Northwind;" +       //the sample Northwind DB
                    "Integrated Security = SSPI;" + //integrated Windows security
                    "Pooling = False";              //specify nonpooled connection

                con.Open();

                // Access the database...

            }

        }
    }

Result


Related Tutorials