①connection pooling
在sql server、ole db和.net框架结构中的data provider中,都提供了隐式的连接池连接支持。你可以在connectionstring中指定不同的参数值控制连接池的行为。比如下面的例子使ole db的连接池无效并自动地进行事务处理:
provider=sqloledb;ole db services=-4;data source=localhost;integrated security=sspi;
在sql server.net data provider中提供了以下参数设置控制连接池的行为:connection lifttime、connection reset、enlist、max pool size、min pool size和pooling。
②使用dataadapter最优化连接
使用dataadpater的fill和update方法时会自动地打开相应的连接。如果fill或者update打开一个连接,在它操作完成后它会关闭此连接。最好的执行方式是在你需要的时候才建立连接。同时减少多个操作时打开和关闭连接的次数。
推荐你在仅仅执行一个fill或者update时,允许fill或者update方法隐式地打开和关闭连接;如果你要执行多个fill或者update,建议你显式地建立连接、执行fill或者update操作然后显式地关闭连接。
额外地,在我们执行事务处理时,在开始事务之前应该显式地建立连接,并在事务结束后显式地关闭连接。举例:
‘visual basic
public sub runsqltransaction(da as sqldataadapter, myconnection as sqlconnection, ds as dataset)
myconnection.open()
dim mytrans as sqltransaction = myconnection.begintransaction()
mycommand.transaction = mytrans
try
da.update(ds)
mytrans.commit()