我正在使用类似于this Support / KB article的代码将多个记录集返回到我的C#程序。
但是我不希望C#代码依赖于返回的记录集的物理序列,以便完成它的工作。
所以我的问题是,“有没有办法确定我正在处理的多记录集结果集中的哪一组记录?”
我知道我可以通过寻找一个独特的列名或每个结果集的内容来间接解读这个,但我认为/希望有更好的方法。
P.S。我正在使用Visual Studio 2008 Pro& SQL Server 2008 Express Edition。
答案 0 :(得分:0)
因为您明确说明执行SQL语句的顺序,结果将以相同的顺序出现。在任何情况下,如果您想以编程方式确定您正在处理的记录集,您仍然必须在结果中标识某些列。
答案 1 :(得分:0)
不,因为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