连接到SQL数据库的最佳实践是什么?

时间:2020-11-25 20:07:46

I want to know what is a good practice of connecting to database, i design a Connection Class, and always use it for connect to SQL Server :

我想知道连接数据库的一个好习惯是什么,我设计了一个连接类,并且总是将它用于连接SQL Server:

public  class Connection
{
    private SqlConnection conn;
    private static Connection myConnection; 

    private Connection()
    {
        try
        {
            conn = new SqlConnection("Data Source=.;Initial Catalog=Padideh;Integrated Security=True");
            conn.Open();
        }
        catch (Exception ex)
        {
            new ErrorHandler().ErrorLoging("", "Connection", ex.Message, "Cannt Connect To DB.");
        }

    }
    public static SqlConnection GetConnection
    {
        get 
        {
            if (myConnection == null || myConnection.conn.State==System.Data.ConnectionState.Closed)
            {
                myConnection = new Connection();
            }
            return myConnection.conn;
        }
    }

    ~Connection()
    {
        try
        {
            myConnection.conn.Close();
        }
        catch (Exception ex)
        {
            new ErrorHandler().ErrorLoging("", "~Connection", ex.Message, "Cannt Close DB Connection.");
        }
    }
}

It's a singleton Class and i used sqlConnection, and i always use this class to connect to SQLserver, i want to know it's a good way and i can use it in every project? and what is a good practice of connecting to database?

它是一个单例类,我使用sqlConnection,我总是使用这个类来连接SQLserver,我想知道这是一种很好的方式,我可以在每个项目中使用它吗?连接数据库的一个好习惯是什么?

1 个解决方案

#1


4  

I would not create a singleton as shown.

我不会创建一个singleton。

Just create a new Connection per usage context, use using to make dealing with the IDisposable nature easier, and rely on Connection Pooling - which already takes care of the appropriate details.

只需在每个使用上下文中创建一个新的连接,使用use使处理IDisposable属性变得更容易,并依赖连接池——它已经处理了适当的细节。

Overall, I believe this use of a singleton will make dealing the connections - even without threading - harder overall with no/minimal gain.

总的来说,我相信单例的使用将使处理连接(即使没有线程)变得更加困难,并且没有/最小的增益。

While I do advocate the use of DI/IoC (although a simple static helper method will do in a pinch), this should be used to obtain a new Connection per usage context (or the appropriate lifetime) which is disposed of promptly when done.

虽然我确实提倡使用DI/IoC(尽管在必要时使用一个简单的静态助手方法),但这应该用于在每次使用上下文(或适当的生命周期)中获取一个新的连接,该连接在完成后立即被处理。

#1


4  

I would not create a singleton as shown.

我不会创建一个singleton。

Just create a new Connection per usage context, use using to make dealing with the IDisposable nature easier, and rely on Connection Pooling - which already takes care of the appropriate details.

只需在每个使用上下文中创建一个新的连接,使用use使处理IDisposable属性变得更容易,并依赖连接池——它已经处理了适当的细节。

Overall, I believe this use of a singleton will make dealing the connections - even without threading - harder overall with no/minimal gain.

总的来说,我相信单例的使用将使处理连接(即使没有线程)变得更加困难,并且没有/最小的增益。

While I do advocate the use of DI/IoC (although a simple static helper method will do in a pinch), this should be used to obtain a new Connection per usage context (or the appropriate lifetime) which is disposed of promptly when done.

虽然我确实提倡使用DI/IoC(尽管在必要时使用一个简单的静态助手方法),但这应该用于在每次使用上下文(或适当的生命周期)中获取一个新的连接,该连接在完成后立即被处理。