• 数据库链接Connection和DataReader的关闭


    数据库链接Connection和DataReader的关闭 
    在使用ASP编程的时候,我们就已经知道,在使用数据库连接以后,一定要将连接关闭,然后设置为NoThing。在Asp.NET中,我们仍然需要这样使用,不过,在ASP.NET中,由于使用了ADO.NET,所以,在一些相关的处理方面,实际还是有一些细微的区别,而这些区别,往往也就是我们设计的时候最需要注意的。现在,我们通过举例,来看看在常见的ADO.NET操作中,需要注意哪些问题。 
    (1)举例一 
    Dim myConnection As SqlConnection = new SqlConnection(ConfigurationSettings.AppSettings("DSN_pubs")) 
    Dim myCommand As SqlCommand = new SqlCommand("Select pub_id, pub_name From publishers", myConnection) 
    Dim myDataReader As SqlDataReader 
    Try 
    myConnection.Open() 
    myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection) 
    DropDownList1.DataSource = myDataReader 
    DropDownList1.DataBind() 
    Catch myException As Exception 
    Response.Write("An error has occurred: " & myException.ToString()) 
    Finally 
    If Not myDataReader Is Nothing Then 
    '关闭DataReader 
    myDataReader.Close() 
    End If 
    End Try 
    在以上的举例中,我们注意到,这里只关闭了DataReader,并没有关闭Connection。为什么呢?仔细观察以上的ExecuteReader方法,原来,设置了ExecuteReader参数,当执行完ExecuteReader以后,会自动关闭Connection。所以,这样设置以后,就没有必要再手动关闭Connection了。 
  • 相关阅读:
    poj 1584
    poj 1113 & poj 2187
    pku 1321 棋盘问题
    poj 1408
    pku 2251 Dungeon Master
    sdut oj 2218 Give Me an E
    Android工程 单元测试
    Android Timer编写方式
    去除工程的.svn隐藏文件夹
    Android 绑定远程服务出现 Not Allowed to bind service
  • 原文地址:https://www.cnblogs.com/leeairw/p/138962.html
Copyright © 2020-2023  润新知