开发者

Is this class using connection pool feature right now ? - MSSQL 2008 R2 - ASP.net 4.0

I looked over the web but i could not understand exactly connection pool thing. This is my query executing class. Every query is getting executed by this class.

Thank you.

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;
using System.IO;

public class DbConnection
{
    public static string srConnectionString = "server=localhost;database=mydb;uid=sa;pwd=mypw;";

    public DbConnection()
    {

    }

    public static DataSet db_Select_Query(string strQuery)
    {
        DataSet dSet = new DataSet();

        try
        {
            using (SqlConnection connection = new SqlConnection(srConnectionString))
            {
                connection.Open();
                SqlDataAdapter DA = new SqlDataAdapter(strQuery, connection);
                DA.Fill(dSet);
            }
            return dSet;

        }

        catch (Exception)
        {
            using (SqlConnect开发者_如何学编程ion connection = new SqlConnection(srConnectionString))
            {
                if (srConnectionString.IndexOf("select Id from tblAspErrors") != -1)
                {
                    connection.Open();
                    strQuery = strQuery.Replace("'", "''");
                    SqlCommand command = new SqlCommand("insert into tblSqlErrors values ('" + strQuery + "')", connection);
                    command.ExecuteNonQuery();
                }
            }
            return dSet;
        }
    }

    public static void db_Update_Delete_Query(string strQuery)
    {
        try
        {
            using (SqlConnection connection = new SqlConnection(srConnectionString))
            {
                connection.Open();
                SqlCommand command = new SqlCommand(strQuery, connection);
                command.ExecuteNonQuery();
            }
        }
        catch (Exception)
        {
            strQuery = strQuery.Replace("'", "''");
            using (SqlConnection connection = new SqlConnection(srConnectionString))
            {
                connection.Open();
                SqlCommand command = new SqlCommand("insert into tblSqlErrors values ('" + strQuery + "')", connection);
                command.ExecuteNonQuery();
            }
        }
    }
}


Yes, connection pools are created per connection string. To prevent memory leaks, you should also have a using statement around your SqlCommand and SqlDataAdapter objects as well.

SQL Server Connection Pooling (ADO.NET)

A connection pool is created for each unique connection string. When a pool is created, multiple connection objects are created and added to the pool so that the minimum pool size requirement is satisfied. Connections are added to the pool as needed, up to the maximum pool size specified (100 is the default). Connections are released back into the pool when they are closed or disposed.

Example using statements:

using (SqlConnection connection = new SqlConnection(srConnectionString))
{
    connection.Open();
    using(SqlDataAdapter DA = new SqlDataAdapter(strQuery, connection))
    {
        DA.Fill(dSet);
    }
}

using (SqlConnection connection = new SqlConnection(srConnectionString))
{
    if (srConnectionString.IndexOf("select Id from tblAspErrors") != -1)
    {
       connection.Open();
       strQuery = strQuery.Replace("'", "''");
       using(SqlCommand command = new SqlCommand("insert into tblSqlErrors values ('" + strQuery + "')", connection))
       {
          command.ExecuteNonQuery();
       }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜