如:
        Dim MyConnection As New SqlConnection("ConnectionString")
        Dim MyCommand As New SqlCommand("StoreProcedure", MyConnection)
        MyCommand.CommandType = CommandType.StoredProcedure
        Dim MyDataAdapter As New SqlDataAdapter(MyCommand)
        Dim MyDataSet As New DataSet上述的MyConnection、MyCommand、MyDataAdapter、MyDataSet需要显式的调用Close方法关闭或将其设置为Nothing吗?

解决方案 »

  1.   

    最好是显式地执行close()或dispose()方法,尽管C#有垃圾回收机制,但是他是在特定的时机进行回收的。有时候需要对回收后的情况进行操作的话,就不行了。因此,得自己手动来进行释放资料,不过这也是一个好的编程习惯。还是显式释放关闭资源的好。
    向你推荐一个好的视频教程吧!
    http://www.microsoft.com/china/msdn/events/webcasts/shared/webcast/episode.aspx?newsID=1242164
    是微软msdn特邀讲师讲的。比上培训班要强得多了。
      

  2.   

    最好是显式地执行close()或dispose()方法,尽管C#有垃圾回收机制,但是他是在特定的时机进行回收的。非常同意
      

  3.   

    对于 Connection 应该显示的 Close 掉。而无需 Nothing
      

  4.   

    如果使用SqlDataAdapter,不需要。
      

  5.   

    至少要将它CLOSE掉,减少占用的连接数。
      

  6.   

    严重同意上面的意见!
    特别提示:DataReader对象一定要显示的关闭
      

  7.   

    LZ声明对象最好和实例化对象分开用完的对象尽早Close(),其中DataReader对象必须显式关闭
      

  8.   

    谢谢各位。对于由过程返回的DataReader在什么时候关闭好?
    'Catalog.vb
    Public Class Catalog    Private Shared ReadOnly Property connectionString() As String
            Get
                Return ConfigurationSettings.AppSettings("ConnectionString")
            End Get
        End Property    Public Shared Function GetDepartments() As SqlDataReader
            Dim connection As New SqlConnection(connectionString)
            Dim command As New SqlCommand("GetDepartments", connection)
            command.CommandType = CommandType.StoredProcedure
            connection.Open()
            Return command.ExecuteReader(CommandBehavior.CloseConnection)
        End FunctionEnd Class'a.aspx
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not IsPostBack Then
            DataGrid1.DataSource = Catalog.GetDepartments()
            DataGrid1.DataBind()
        End If
    End Sub