无法在 VB.NET 中关闭 Sybase 数据库的 OledbDataReader

发布于 2024-07-05 20:11:59 字数 766 浏览 10 评论 0原文

我似乎无法在读取数据后关闭 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()

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

I don't seem to be able to close the OledbDataReader object after reading data from it. Here is the relevant code -

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

The console application just hangs at the point at which I try to close the reader. Opening and closing a connection is not a problem, therefore does anyone have any ideas for what may be causing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绿光 2024-07-12 20:11:59

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

It's been a while since I used VB.NET, but the most safe way to handle this in C# is to use a "using" statement.

It's like an implicit try-catch and it makes sure all resources are closed/cancelled and disposed when the "using" ends.

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

Update: Found a link about Using statement in VB.NET 2.0, hope it helps.

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
暗地喜欢 2024-07-12 20:11:59

这是一个不太可能的方案,但请尝试将 .Close() 和 .Dispose() 行移到 Try 的 Final 块中。 像这样:


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

This is a long-shot, but try moving your .Close() and .Dispose() lines in a Finally block of the Try. Like this:


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
冷默言语 2024-07-12 20:11:59

我找到了答案!

之前

drSyBase.Close()

在你需要调用Command对象的cancel方法

cmdSyBase.Cancel()

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

I found the answer!

Before

drSyBase.Close()

You need to call the cancel method of the Command object

cmdSyBase.Cancel()

I believe that this may be specific to Sybase databases

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文