打印本文 打印本文 关闭窗口 关闭窗口
第二课 SqlConnection对象(翻译)
作者:武汉SEO闵涛  文章来源:敏韬网  点击数11089  更新时间:2007/11/14 13:12:47  文章录入:mintao  责任编辑:mintao
While循环从结果集的每一行中读取第一列,即CustomerID列。我们将在以后的课程中讨论SqlCommandSqlDataReader对象。现在,你需要着重了解的是这些对象使用SqlConnection对象,所以它们知道要操作哪个数据库。

When you are done using the connection object, you must close it.  Failure to do so could have serious consequences in the performance and scalability of your application.  There are a couple points to be made about how we closed the connection in Listing 1:  the Close() method is called in a finally block and we ensure that the connection is not null before closing it.

当你使用完连接对象以后必须关闭它。如果关闭失败则会对应用程序的性能和可测量性造成严重的后果。在Listing1中如何关闭连接有两点:在finally语句块中调用Close()方法,并且在关闭连接之前保证它不为null

Notice that we wrapped the ADO.NET code in a try/finally block.  As described in Lesson 15:  Introduction to Exception Handling of the C# Tutorial, finally blocks help guarantee that a certain piece of code will be executed, regardless of whether or not an exception is generated.  Since connections are scarce system resources, you will want to make sure they are closed in finally blocks.

注意我们将ADO.NET代码外覆一层try/finally语句块。在第15课:异常处理中,finally语句块有助于保证某些代码无论是否产生异常的情况下都被执行。因为连接是稀有的系统资源,你需要保证它们在finally语句块中被关闭掉。

Another precaution you should take when closing connections is to make sure the connection obj

上一页  [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]  ...  下一页 >> 

打印本文 打印本文 关闭窗口 关闭窗口