无法在VB.NET中关闭OledbDataReader到Sybase数据库
-
02-07-2019 - |
题
从中读取数据后,我似乎无法关闭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#中处理此问题最安全的方法是使用“使用”言。
它就像一个隐式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
不隶属于 StackOverflow