【问题标题】:Unable to close OledbDataReader to Sybase Database in VB.NET无法在 VB.NET 中关闭 OledbDataReader 到 Sybase 数据库
【发布时间】:2010-09-12 16:54:26
【问题描述】:

从 OledbDataReader 对象读取数据后,我似乎无法关闭它。这是相关代码-

Dim conSyBase As New OleDb.OleDbConnection("Provider=Sybase.ASEOLEDBProvider.2;Server Name=xx.xx.xx.xx;Server Port Address=5000;Initial Catalog=xxxxxxxxxx;User ID=xxxxxxxx;Password=xxxxxxxxx;")

conSyBase.Open()

Dim cmdSyBase As New OleDb.OleDbCommand("MySQLStatement", conSyBase)
Dim drSyBase As OleDb.OleDbDataReader = cmdSyBase.ExecuteReader

Try

    While drSyBase.Read
     /*Do some stuff with the data here */

    End While

Catch ex As Exception

    NotifyError(ex, "Read failed.")

End Try

drSyBase.Close() /* CODE HANGS HERE */
conSyBase.Close()
drSyBase.Dispose()
cmdSyBase.Dispose()
conSyBase.Dispose()

控制台应用程序在我尝试关闭阅读器时挂起。打开和关闭连接不是问题,因此有人对可能导致此问题的原因有任何想法吗?

【问题讨论】:

    标签: vb.net sybase data-access


    【解决方案1】:

    这是一个长镜头,但请尝试将 .Close() 和 .Dispose() 行移到 Try 的 finally 块中。像这样:

    
    Dim conSyBase As New OleDb.OleDbConnection("Provider=Sybase.ASEOLEDBProvider.2;Server Name=xx.xx.xx.xx;Server Port Address=5000;Initial Catalog=xxxxxxxxxx;User ID=xxxxxxxx;Password=xxxxxxxxx;")
    conSyBase.Open()
    Dim cmdSyBase As New OleDb.OleDbCommand("MySQLStatement", conSyBase)
    Dim drSyBase As OleDb.OleDbDataReader = cmdSyBase.ExecuteReader
    Try
      While drSyBase.Read
       /*Do some stuff with the data here */
      End While
    Catch ex As Exception 
      NotifyError(ex, "Read failed.")
    Finally
      drSyBase.Close() 
      conSyBase.Close()
      drSyBase.Dispose()
      cmdSyBase.Dispose()
      conSyBase.Dispose()
    End Try
    

    【讨论】:

    • 嗨 Mikey,我尝试将 close 和 dispose 行放在 finally 块中,但这并没有解决问题
    【解决方案2】:

    我找到了答案!

    之前

    drSyBase.Close()
    

    需要调用Command对象的cancel方法

    cmdSyBase.Cancel()
    

    我相信这可能是 Sybase 数据库特有的

    【讨论】:

      【解决方案3】:

      我使用 VB.NET 已经有一段时间了,但在 C# 中处理这个问题最安全的方法是使用“using”语句。

      这就像一个隐式try-catch,它确保所有资源在“使用”结束时都被关闭/取消和处置。

      using (OleDb.OleDbConnection connection = new OleDb.OleDbConnection(connectionString)) 
      {
          DoDataAccessStuff();
      } // Your resource(s) are killed, disposed and all that
      

      更新:找到了一个关于Using statement in VB.NET 2.0的链接,希望对你有帮助。

      Using conSyBase As New OleDb.OleDbConnection("Provider=Sybase.ASEOLEDBProvider.2;Server Name=xx.xx.xx.xx;Server Port Address=5000;Initial Catalog=xxxxxxxxxx;User ID=xxxxxxxx;Password=xxxxxxxxx;"), _
           cmdSyBase As New OleDb.OleDbCommand("MySQLStatement", conSyBase) 
      
          conSyBase.Open()
          Dim drSyBase As OleDb.OleDbDataReader = cmdSyBase.ExecuteReader
      
          Try
              While drSyBase.Read()
      
                  '...'
      
              End While
          Catch ex As Exception
              NotifyError(ex, "Read failed.")
          End Try
      
          cmdSyBase.Cancel()
      End Using
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-18
        • 1970-01-01
        • 2014-02-01
        • 1970-01-01
        • 2015-07-08
        • 2016-07-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多