我正在使用类似于此支持/知识库文章的代码将多个记录集返回到我的 C# 程序。
但我不希望 C# 代码依赖于返回的记录集的物理顺序,以便完成它的工作。
所以我的问题是,“有没有办法确定我当前正在处理来自多记录集结果集中的哪组记录?”
我知道我可能可以通过查找唯一的列名或每个结果集的名称来间接破译这一点,但我认为/希望有更好的方法。
PS 我正在使用 Visual Studio 2008 Pro & SQL Server 2008 Express Edition。
我正在使用类似于此支持/知识库文章的代码将多个记录集返回到我的 C# 程序。
但我不希望 C# 代码依赖于返回的记录集的物理顺序,以便完成它的工作。
所以我的问题是,“有没有办法确定我当前正在处理来自多记录集结果集中的哪组记录?”
我知道我可能可以通过查找唯一的列名或每个结果集的名称来间接破译这一点,但我认为/希望有更好的方法。
PS 我正在使用 Visual Studio 2008 Pro & SQL Server 2008 Express Edition。
因为您明确说明了执行 SQL 语句的顺序,所以结果将以相同的顺序出现。在任何情况下,如果您想以编程方式确定您正在处理的记录集,您仍然必须识别结果中的某些列。
不,因为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