简体   繁体   English

ADO.NET数据访问层

[英]ADO.NET data access layer

I'm maintaining an existing C# application which uses classic ADO.NET as a data access technology.The existing code creates a new instance of the SqlConnection and SqlCommand objects EVERY single time some interaction with the database is required. 我保持它采用经典的ADO.NET为现有的代码数据访问technology.The创建新实例的现有C#应用程序SqlConnectionSqlCommand对象,必须与数据库交互的一些一次。 To make things simpler I wrote a little class to simplify this process to prevemt code repetition but I'm not an expert at ADO.NET so I wanted to ask if you can review my code and let me know if I missed any ADO.NET best practices or if the code below may impact DB operations negatively in anyway: 为简化起见,我编写了一个小类来简化此过程以防止代码重复,但是我不是ADO.NET的专家,所以我想问一下是否可以查看我的代码,并让我知道是否错过了ADO.NET。最佳做法,或者下面的代码无论如何可能对数据库操作产生负面影响:

using System;
using System.Data;
using System.Data.SqlClient;

namespace MyApp.DataAccess
{
    public class DataAccessLayer : IDisposable
    {
        private SqlConnection _connection = new SqlConnection();
        private SqlCommand _command = new SqlCommand();

        public string ConnectionString 
        {
            get { return DBConfig.ConnectionString; }
        }

        public SqlCommand Command 
        {
            get { return _command; }
            set { _command = value; }
        }

        public SqlConnection SQLConnection 
        {
            get
            {
                if(_connection == null || _connection.State == ConnectionState.Closed)
                {
                    _connection = new SqlConnection(ConnectionString);
                    _connection.Open();
                }
                return _connection;
            }
        }

        public void SetCommand(string commandText,CommandType commandType)
        {
            _command = new SqlCommand(commandText, SQLConnection);
            _command.CommandType = commandType;
        }

        public void Dispose()
        {
            if (_connection != null)
                _connection.Dispose();

            if (_command != null)
                _command.Dispose();
        }
    }
}

As Tim explained, I would rather code this with the using statement. 正如Tim所解释的,我宁愿使用using语句对此进行编码。 This will automatically close and dispose your SqlConnection instance. 这将自动关闭并释放您的SqlConnection实例。

Take a look at this example here from the MSDN page about the SqlConnection class : 在MSDN页面上关于SqlConnection类的示例看一下:

using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
        // Do work here; connection closed on following line.
    }


And what model_dialog explained, is also correct. 而model_dialog解释的也是正确的。 If you invoke the Open method on the SqlConnection class the framework will not necesserily really open a new connection. 如果您在SqlConnection类上调用Open方法,则该框架不一定会真正打开新连接。

Instead it will check whether there is already an appropiate connection available in the connection pool. 相反,它将检查连接池中是否已经存在适当的连接。 It does this by checking the connection string. 它通过检查连接字符串来做到这一点。 If it founds a suitable connection, the connection pool will just return this connection with almost no performance impact. 如果找到合适的连接,则连接池将仅返回该连接而几乎不影响性能。 The connection pool is a catching component for SqlConnections so to speak. 可以这么说,连接池是SqlConnections的捕获组件。

See here for more details: SQL Server Connection Pooling (ADO.NET) 有关更多详细信息,请参见此处: SQL Server连接池(ADO.NET)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM