可以确定、控制多个 RecordSet ADO.Net 结果集中的 RecordSet 序列吗?

发布于 2024-09-03 22:07:35 字数 366 浏览 4 评论 0原文

我使用类似于此支持/知识库文章的代码将多个记录集返回到我的 C# 程序。

但我不希望 C# 代码依赖于返回的记录集的物理顺序来完成它的工作。

所以我的问题是,“有没有办法确定我当前正在处理多记录集结果集中的哪一组记录?”

我知道我可能可以通过查找每个结果集的唯一列名称或某些内容来间接破译这一点,但我认为/希望有更好的方法。

PS 我正在使用 Visual Studio 2008 Pro 和 Visual Studio 2008 Pro。 SQL Server 2008 Express 版。

I am using code similar to this Support / KB article to return multiple recordsets to my C# program.

But I don't want C# code to be dependant on the physical sequence of the recordsets returned, in order to do it's job.

So my question is, "Is there a way to determine which set of records from a multiplerecordset resultset am I currently processing?"

I know I could probably decipher this indirectly by looking for a unique column name or something per resultset, but I think/hope there is a better way.

P.S. I am using Visual Studio 2008 Pro & SQL Server 2008 Express Edition.

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

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

发布评论

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

评论(2

蓝海似她心 2024-09-10 22:07:35

不,因为 SqlDataReader 只能转发。据我所知,您能做的最好的事情就是使用 KeyInfo 打开阅读器并检查使用阅读器的 GetSchemaTable 方法创建的架构数据表(或者只检查字段,这更容易,但不太可靠)。

我花了几天时间在这上面。我最终只能忍受物理顺序的依赖。我用 !!!IMPORTANT!!! 对代码方法和存储过程进行了大量注释,并包含一个 #If...#End If 来输出结果集当需要验证存储过程输出时。

以下代码片段可能会对您有所帮助。

有用的代码

        Dim fContainsNextResult As Boolean
        Dim oReader As DbDataReader = Nothing

        oReader = Me.SelectCommand.ExecuteReader(CommandBehavior.CloseConnection Or CommandBehavior.KeyInfo)

#If DEBUG_ignore Then

        'load method of data table internally advances to the next result set
        'therefore, must check to see if reader is closed instead of calling next result

        Do
            Dim oTable As New DataTable("Table")
            oTable.Load(oReader)
            oTable.WriteXml("C:\" + Environment.TickCount.ToString + ".xml")
            oTable.Dispose()
        Loop While oReader.IsClosed = False

        'must re-open the connection
        Me.SelectCommand.Connection.Open()

        'reload data reader
        oReader = Me.SelectCommand.ExecuteReader(CommandBehavior.CloseConnection Or CommandBehavior.KeyInfo)

#End If

       Do

            Dim oSchemaTable As DataTable = oReader.GetSchemaTable

            '!!!IMPORTANT!!! PopulateTable expects the result sets in a specific order
            '   Therefore, if you suddenly start getting exceptions that only a novice would make
            '   the stored procedure has been changed!
            
            PopulateTable(oReader, oDatabaseTable, _includeHiddenFields)

            fContainsNextResult = oReader.NextResult

        Loop While fContainsNextResult

No, because the SqlDataReader is forward only. As far as I know, the best you can do is open the reader with KeyInfo and inspect the schema data table created with the reader's GetSchemaTable method (or just inspect the fields, which is easier, but less reliable).

I spent a couple of days on this. I ended up just living with the physical order dependency. I heavily commented both the code method and the stored procedure with !!!IMPORTANT!!!, and included an #If...#End If to output the result sets when needed to validate the stored procedure output.

The following code snippet may help you.

Helpful Code

        Dim fContainsNextResult As Boolean
        Dim oReader As DbDataReader = Nothing

        oReader = Me.SelectCommand.ExecuteReader(CommandBehavior.CloseConnection Or CommandBehavior.KeyInfo)

#If DEBUG_ignore Then

        'load method of data table internally advances to the next result set
        'therefore, must check to see if reader is closed instead of calling next result

        Do
            Dim oTable As New DataTable("Table")
            oTable.Load(oReader)
            oTable.WriteXml("C:\" + Environment.TickCount.ToString + ".xml")
            oTable.Dispose()
        Loop While oReader.IsClosed = False

        'must re-open the connection
        Me.SelectCommand.Connection.Open()

        'reload data reader
        oReader = Me.SelectCommand.ExecuteReader(CommandBehavior.CloseConnection Or CommandBehavior.KeyInfo)

#End If

       Do

            Dim oSchemaTable As DataTable = oReader.GetSchemaTable

            '!!!IMPORTANT!!! PopulateTable expects the result sets in a specific order
            '   Therefore, if you suddenly start getting exceptions that only a novice would make
            '   the stored procedure has been changed!
            
            PopulateTable(oReader, oDatabaseTable, _includeHiddenFields)

            fContainsNextResult = oReader.NextResult

        Loop While fContainsNextResult
壹場煙雨 2024-09-10 22:07:35

因为您明确指定了 SQL 语句的执行顺序,所以结果将以相同的顺序出现。无论如何,如果您想以编程方式确定正在处理哪个记录集,您仍然必须识别结果中的某些列。

Because you're explicitly stating in which order to execute the SQL statements the results will appear in that same order. In any case if you want to programmatically determine which recordset you're processing you still have to identify some columns in the result.

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