我现在的惯例做法是,启动程序,打开连接,并保持连接直到程序退出。这么做方便,节省了打开关闭数据库连接开销,不足之处是一直占用了资源。在codeproject浏览ADO.NET的文章,看到这么一段In many earlier applications, the tendency was to open a connection when you start the application and not close the connection until the application terminates. It is an expensive and time-consuming operation to open and close a database connection. Most databases have a limit on the number of concurrent connections that they allow.For example: each connection consumes a certain amount of resources on the database server and these resources are not infinite. Most modern OLE DB providers (including SQL Server provider) implement connection pooling. If you create database connections, they are held in a pool. When you want a connection for an application, the OLE DB provider extracts the next available connection from the pool. When your application closes the connection, it returns to the pool and makes itself available for the next application that wants a connection.This means that opening and closing a database connection is no longer an expensive operation. If you close a connection, it does not mean you disconnect from the database. It just returns the connection to the pool. If you open a connection, it means it's simply a matter of obtaining an already open connection from the pool. It's recommended in many ADO.NET books not to keep the connections longer than you need to. Therefore, you should: * Open a connection when you need it, and
* Close it as soon as you have finished with it.
大致意思是数据库有连接池,打开关闭数据库连接花销不大,建议用的时候建立连接,不用的时候关闭连接。我想知道ADO是不是也是这个机制呢?不需要一直保持连接,用的时候建立连接,不用的时候关闭连接就行?THX~
* Close it as soon as you have finished with it.
大致意思是数据库有连接池,打开关闭数据库连接花销不大,建议用的时候建立连接,不用的时候关闭连接。我想知道ADO是不是也是这个机制呢?不需要一直保持连接,用的时候建立连接,不用的时候关闭连接就行?THX~
* Open a connection when you need it, and
* Close it as soon as you have finished with it. ADO是这个机制
这句话是说使用时连接,完成之后就关闭
不用时,除了关闭还要清除当前连接所用的全部资源,这里要代码控制才行
程序里面的conn.close 都是把连接放到连接池里,不是真的关闭。