English 中文(简体)
无法关闭VB.NET中Sybase数据库的OledbDataReader
原标题:
  • 时间:2008-09-24 11:51:20
  •  标签:

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

控制台应用程序只是挂在我试图关闭阅读器的位置。打开和关闭连接不是问题,因此有人知道是什么原因导致的吗?

最佳回答

我找到了答案!

之前

drSyBase.Close()

您需要调用Command对象的cancel方法

cmdSyBase.Cancel()

我认为这可能是Sybase数据库特有的

问题回答

这是一个很长的机会,但请尝试在try的Finally块中移动.Close()和.Dispose()行。这样地:


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

我已经有一段时间没有使用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 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




相关问题
热门标签