处理自定义数据访问层引用

发布于 2024-09-26 05:08:32 字数 1250 浏览 3 评论 0原文

我们的应用程序几乎完全使用自定义 DataAccessLayer 类,并且在其中我们使用数据访问应用程序块(当前版本 2)。我们偶尔会遇到臭名昭著的“GetOrdinal”错误。我们没有使用方法外的连接。我们正在使用 DAAB 版本 2。下面是我们的 DAL 方法的典型示例:

Public Function MyDALMethod(ByVal Param1 As Integer, ByVal Param2 As Integer) As System.Data.IDataReader

        Dim db As Database = DatabaseFactory.CreateDatabase()
        Dim sqlCommand As String = "usp_MyProcedure"
        Dim dbCommand As DbCommand = db.GetStoredProcCommand(sqlCommand)
        db.AddInParameter(dbCommand, "Param1", DbType.Int32, MyParam1)
        db.AddInParameter(dbCommand, "Param2", DbType.Int32, MyParam2)

        Return db.ExecuteReader(dbCommand)

End Function

在我们的代码中,我们只是实例化一个 DAL 变量并调用所需的方法。使用 DataReader 后,引用代码将关闭、处置并将读取器设置为空。但是,没有对 DAL 的引用进行任何操作。我想知道这是否是我们问题的一部分。典型的方法会像这样使用我们的 DAL:

Dim DAL as New DataAccessLayer
Dim dbReader as system.data.idatareader

dbreader = DAL.MyDALMethod(parm1, parm2)

While dbreader.read
    i = dbreader.items("column1")
    j = dbreader.items("column2")
End While

dbreader.close()
dbreader.dispose()
dbreader = nothing

我的主要问题是这些 DAL 引用是否应该以某种方式处理?这是一个用 VB.NET 编写的自定义类,因此它没有实现 IDisposable,所以我不确定是否需要做任何事情,但我们确实有错误和问题(例如 GetOrdinal 问题),这些错误和问题似乎是加载的-相关,我想知道这是否是问题的一部分。

Our application uses a custom DataAccessLayer class almost exclusively, and within that we do use Data Access Application Block (currently version 2). We are getting the infamous "GetOrdinal" error sporadically. We are not using out-of-method connections. We are using DAAB version 2. Below is a typical example of our DAL methods:

Public Function MyDALMethod(ByVal Param1 As Integer, ByVal Param2 As Integer) As System.Data.IDataReader

        Dim db As Database = DatabaseFactory.CreateDatabase()
        Dim sqlCommand As String = "usp_MyProcedure"
        Dim dbCommand As DbCommand = db.GetStoredProcCommand(sqlCommand)
        db.AddInParameter(dbCommand, "Param1", DbType.Int32, MyParam1)
        db.AddInParameter(dbCommand, "Param2", DbType.Int32, MyParam2)

        Return db.ExecuteReader(dbCommand)

End Function

In our code we just instantiate a DAL var and call the desired method. After using the DataReader, the referencing code will close, dispose, and set the reader to nothing. However, nothing is done with the reference to the DAL. I've wondered if this is part of our problem. A typical method would use our DAL like this:

Dim DAL as New DataAccessLayer
Dim dbReader as system.data.idatareader

dbreader = DAL.MyDALMethod(parm1, parm2)

While dbreader.read
    i = dbreader.items("column1")
    j = dbreader.items("column2")
End While

dbreader.close()
dbreader.dispose()
dbreader = nothing

My main question is should these DAL references be disposed somehow? It's a custom class written in VB.NET, so it doesn't implement IDisposable so I'm not sure if there's anything to be done or not, but we do have errors and issues (like the GetOrdinal problem) which seem to be load-related, and I'm wondering if this is part of the problem.

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

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

发布评论

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

评论(1

娇妻 2024-10-03 05:08:32

如果 DAL 至少拥有一个 Disposable 成员变量(实现 IDisposable),那么它也应该实现 IDisposable。然后,这将向 DAL 的客户端指示应该调用 Dispose()。然后,DAL 的 Dispose() 将调用成员变量 Dispose()。

这是实现 IDisposable 的一般指南。

顺便说一句 - 没有必要 dbreader = Nothing - 它什么也实现不了。请参阅 Eric Lippert 的帖子

If the DAL holds at least one member variable that is Disposable (implements IDisposable), then it too should implement IDisposable. This would then indicate to DAL's client that Dispose() should be called. The DAL's Dispose() would then call the member variable(s) Dispose().

That is the general guidance for implementing IDisposable.

BTW - there is no need to dbreader = nothing - it achieves nothing. See Eric Lippert's post

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