简体   繁体   English

存储过程返回多个记录集

[英]Stored Procedure Returns Multiple Recordsets

I am pretty new to c#. I have a page that requires multiple recordsets, and a single sproc that returns them.我是 c# 的新手。我有一个页面需要多个记录集,以及一个返回它们的存储过程。 I am using a repeater control for the main recordset.我正在为主记录集使用转发器控件。 How do I get to the next returned recordset?我如何到达下一个返回的记录集?


OK so the datasource is in the aspx page.好的所以数据源在 aspx 页面中。 I would have move it to the code behind page to use NextResult right?我会把它移到页面后面的代码以使用 NextResult 吗? Here is my code now.这是我的代码。 How do I move the datasource to the codebehind, implement a datareader so I can use nextresult?如何将数据源移动到代码隐藏,实现数据读取器以便我可以使用 nextresult?

<asp:SqlDataSource ID="AssetMgtSearch" runat="server" 
                ConnectionString="<%$ ConnectionStrings:OperationConnectionString %>" 
                SelectCommand="spAssetMgtItemList" SelectCommandType="StoredProcedure">
            </asp:SqlDataSource>
            <div class="contentListFullHeight">
                <table cellspacing="0" cellpadding="0" border="0" class="contentList">
                    <tr>
                        <th>ShipmentID/</td>
                        <th>MaterialID/</td>
                        <th>ItemID/</td>
                    </tr>
                    <asp:Repeater ID="Repeater1" runat="server" DataSourceID="AssetMgtSearch">
                        <ItemTemplate> 
                            <tr>
                                <td colspan="3" style="border-top:solid thin blue">&nbsp;</td>
                            </tr>
                            <tr>
                                <td><%#Container.DataItem(0)%></td>
                                <td><%#Container.DataItem(1)%></td>
                                <td><%#Container.DataItem(2)%></td>
                            </tr>
                        </ItemTemplate>                           
                    </asp:Repeater>
                </table>

Call the NextResult() method on your reader to move to the next result.在阅读器上调用 NextResult() 方法以移动到下一个结果。

No, you can't do this using SqlDataSource, you need to use codebehind or break up the procedure into separate queries.不,您不能使用 SqlDataSource 执行此操作,您需要使用代码隐藏或将过程分解为单独的查询。

Thanks for your answers everyone.谢谢大家的回答。 NextResult() works well provided you make quite a few changes going from the drag and drop control creation.如果您从创建拖放控件开始进行大量更改,NextResult() 将运行良好。 Here they are.他们来了。

  1. Remove the datasource from the aspx page从 aspx 页面中删除数据源
  2. Remove the DataSourceID property from the Repeater control从 Repeater 控件中删除 DataSourceID 属性
  3. Create a function in your codebehind that returns a datareader object. eg AstMgtDr()在返回数据读取器 object 的代码隐藏中创建一个 function。例如 AstMgtDr()
  4. On your page load set the datasource and databind properties for the Repeater control在您的页面加载上,为 Repeater 控件设置数据源和数据绑定属性

    Repeater1.DataSource = AstMgtDr();

    Repeater1.DataBind();

  5. At the top of your aspx page, add a page level directive to use the "System.Data.Common" namespace <%@ Import namespace="System.Data.Common" %>在您的 aspx 页面顶部,添加页面级指令以使用“System.Data.Common”命名空间<%@ Import namespace="System.Data.Common" %>

  6. To display your data:要显示您的数据:

this is the method with the best performance but it requires explicit typing这是性能最好的方法,但它需要显式输入

`<%#((DbDataRecord)Container.DataItem).GetInt32(0)%>`

this is another method using field names - more expensive than the previous method but faster than the default Eval.这是另一种使用字段名称的方法 - 比以前的方法更昂贵,但比默认的 Eval 更快。

`<%# ((DbDataRecord)Container.DataItem)["ShipmentID"] %>`

Hope this saves somebody else some time.希望这可以节省其他人一些时间。

This question has been viewed a bunch of times and I just wanted to submit my own solution.这个问题已经被看过很多次了,我只是想提交我自己的解决方案。
I know the original question was c#, but I'm supporting legacy systems and it's easy to convert the code.我知道最初的问题是 c#,但我支持遗留系统并且很容易转换代码。

This came up for me today.这是我今天想到的。 I needed a quick-and-dirty way to display the results from a stored procedure that returned 7 different datasets.我需要一种快速而简单的方法来显示返回 7 个不同数据集的存储过程的结果。

I went about this by filling a System.Data.DataSet with my stored procedure results.我通过用我的存储过程结果填充 System.Data.DataSet 来解决这个问题。 Then dynamically creating DataGrids, and adding them to a PlaceHolder.然后动态创建 DataGrids,并将它们添加到 PlaceHolder。

HTML Code: HTML 代码:

<html>
  <body>
  <form id="form1" runat="server">
    <asp:PlaceHolder ID="phMetrics" runat="server"/>
  </form>
  </body>
</html>

VB.NET Code: VB.NET 代码:

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    Dim ds As System.Data.DataSet = Me.GetMetrics()
    If ds IsNot Nothing Then
        For i As Integer = 0 To ds.Tables.Count - 1
            Dim _rpt As New System.Web.UI.WebControls.DataGrid()
            With _rpt
                .AutoGenerateColumns = True
                .Attributes.Add("name", "table_" & i + 1)
                .DataSource = ds.Tables(i)
                .DataBind()
            End With
            Me.phMetrics.Controls.Add(_rpt)
        Next
    End If
End Sub

Private Function GetMetrics() As System.Data.DataSet
    Dim dsMetrics As New System.Data.DataSet

    Using _sqlConn As New System.Data.SqlClient.SqlConnection(_sqlConnString)
        _sqlConn.Open()
        Dim _SqlCommand As New System.Data.SqlClient.SqlCommand("[dbo].[My_Stored_Procedure]", _sqlConn)
        With _SqlCommand
            .CommandType = System.Data.CommandType.StoredProcedure
            .Parameters.Add(New System.Data.SqlClient.SqlParameter("@ClientID", 101))
        End With
        Dim _sqlAdapter As New System.Data.SqlClient.SqlDataAdapter(_SqlCommand)
        With _sqlAdapter
            .Fill(dsMetrics)
            .Dispose()
        End With
        _sqlConn.Close()
        _sqlConn.Dispose()
    End Using

    Return dsMetrics
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM