代码之家  ›  专栏  ›  技术社区  ›  Shiva

多记录集ADO.NET结果集中的记录集序列是否可以确定、控制?

  •  1
  • Shiva  · 技术社区  · 15 年前

    我使用的代码类似于 this Support / KB article 将多个记录集返回到“我的C”程序。

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

    所以我的问题是,“有没有一种方法可以确定我当前正在处理多个记录集resultset中的哪一组记录?”

    我知道我可以通过寻找一个唯一的列名或者每个结果集来间接地破译这个,但是我认为/希望有更好的方法。

    另外,我使用的是Visual Studio 2008 Pro&SQL Server 2008速成版。

    2 回复  |  直到 15 年前
        1
  •  0
  •   AMissico    15 年前

    不,因为 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
    
        2
  •  0
  •   Raj    15 年前

    因为您显式地声明了执行SQL语句的顺序,所以结果将以相同的顺序出现。在任何情况下,如果要以编程方式确定正在处理的记录集,您仍然需要在结果中标识一些列。