1

从 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()

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

4

3 回答 3

3

我找到了答案!

drSyBase.Close()

需要调用Command对象的cancel方法

cmdSyBase.Cancel()

我相信这可能特定于 Sybase 数据库

于 2008-09-24T13:04:17.627 回答
0

这是一个长镜头,但请尝试将您的 .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
于 2008-09-24T12:20:46.747 回答
0

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

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

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

更新:在 VB.NET 2.0 中找到了关于 Using 语句的链接,希望对您有所帮助。

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
于 2008-09-24T13:09:31.530 回答