这个类现在使用连接池功能吗? - MSSQL 2008 R2 - ASP.net 4.0

发布于 2024-12-06 06:26:09 字数 2242 浏览 1 评论 0原文

我浏览了网络,但我无法准确理解连接池的事情。这是我的查询执行类。每个查询都由此类执行。

谢谢。

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 (SqlConnection 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();
            }
        }
    }
}

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 (SqlConnection 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();
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柒夜笙歌凉 2024-12-13 06:26:09

是的,连接池是根据连接字符串创建的。为了防止内存泄漏,您还应该在 SqlCommandSqlDataAdapter 对象周围有一个 using 语句。

SQL Server 连接池 (ADO.NET)

为每个唯一的连接字符串创建一个连接池。当一个
创建池,创建多个连接对象并添加到
池,以满足最小池大小要求。
根据需要将连接添加到池中,直至最大池数
指定大小(默认为 100)。连接被释放回来
当它们关闭或处置时进入池中。

使用语句示例:

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();
       }
    }
}

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();
       }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文