使用返回多个记录集的SQL Server存储过程的结果填充gridview

时间:2012-08-20 13:33:16

标签: asp.net sql-server vb.net tsql stored-procedures

我有一个返回多个记录集的存储过程。它可以是1个记录集,2个记录集或更多。我不知道会有多少RS回来。

在stackoverflow,我找到了以下示例

    Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("myConnection").ConnectionString)
    Dim cmd As New SqlCommand
    Dim Reader As SqlDataReader
    cmd.Connection = conn
    cmd.Parameters.AddWithValue("@ACTNbr", tbACTNbr.Text.ToString.Trim)
    cmd.Parameters.AddWithValue("@WTHNbr", tbWTHNbr.Text.ToString.Trim)
    cmd.CommandText = "sp_search_def"
    cmd.CommandType = CommandType.StoredProcedure
    conn.Open()
    Reader = cmd.ExecuteReader()
'The next part is what I found here at stackoverflow
While Reader.Read() OrElse (Reader.NextResult() And Reader.Read())
  For i As Integer = 0 To Reader.FieldCount - 1
    Response.Write(Reader(i).ToString())
    Response.Write(" ")
  Next
  Response.Write("<br />")
End While

以上response.write显示了我需要的数据。但我需要把它放到Gridview中。即我需要将存储过程的结果(及其所有结果集)放入一个网格视图中。

我的gridview设置为AutoGenerateColumns = "true"

我试过了:

myGridview.DataSource = Reader
myGridview.DataBind()

当然我只能获得一个记录集。

存储过程的结果都是相同的格式 - 相同数量的列,标题等。

有人能指出我正确的方向吗?我一直试图解决这个问题,但放弃了,现在我在这里问。

我是新手。

谢谢。

4 个答案:

答案 0 :(得分:1)

使用DataAdapter填充DataSet而不是DataReader ,您可以轻松绑定到gridview,如下所示:

    Dim DS As DataSet
    Dim MyConnection As SqlConnection
    Dim MyDataAdapter As SqlDataAdapter

    'Create a connection to the SQL Server.
    MyConnection = New SqlConnection("server=(local);database=pubs;Trusted_Connection=yes")

    'Create a DataAdapter, and then provide the name of the stored procedure.
    MyDataAdapter = New SqlDataAdapter("GetAuthorsByLastName", MyConnection)

    'Set the command type as StoredProcedure.
    MyDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure

    'Create and add a parameter to Parameters collection for the stored procedure.
    MyDataAdapter.SelectCommand.Parameters.Add(New SqlParameter("@au_lname", _
   SqlDbType.VarChar, 40))

    'Assign the search value to the parameter.
    MyDataAdapter.SelectCommand.Parameters("@au_lname").Value = Trim(txtLastName.Text)

    'Create and add an output parameter to Parameters collection. 
    MyDataAdapter.SelectCommand.Parameters.Add(New SqlParameter("@RowCount", _
    SqlDbType.Int, 4))

    'Set the direction for the parameter. This parameter returns the Rows returned.
    MyDataAdapter.SelectCommand.Parameters("@RowCount").Direction = ParameterDirection.Output

    DS = New DataSet() 'Create a new DataSet to hold the records.
    MyDataAdapter.Fill(DS, "AuthorsByLastName") 'Fill the DataSet with the rows returned.

    'Get the number of rows returned, and then assign it to the Label control.
    'lblRowCount.Text = DS.Tables(0).Rows.Count().ToString() & " Rows Found!"
    lblRowCount.Text = MyDataAdapter.SelectCommand.Parameters(1).Value & " Rows Found!"

    'Set the data source for the DataGrid as the DataSet that holds the rows.
    Grdauthors.DataSource = DS.Tables("AuthorsByLastName").DefaultView

    'Bind the DataSet to the DataGrid. 
    'NOTE: If you do not call this method, the DataGrid is not displayed!
    Grdauthors.DataBind()

    MyDataAdapter.Dispose() 'Dispose of the DataAdapter.
    MyConnection.Close() 'Close the connection.

How to call SQL Server stored procedures in ASP.NET by using Visual Basic .NET

答案 1 :(得分:1)

您只能将GridView绑定到一个结果集。例如,将GridView绑定到包含多个DataTable的DataSet只会显示第一个 DataTable中的数据。

由于proc返回的所有结果集都具有相同的模式,因此需要合并它们并将结果集绑定到GridView

示例:

//Assumes your proc returns a dataset with more than one datatable
//Notice how all datables are merged into the first one [0]
for (int i = 1; i < ds.Tables.Count; i++)
{
    ds.Tables[0].Merge(ds.Tables[i]);  
}

grid.DataSource = ds;
grid.DataBind();

或等效地:

grid.DataSource = ds.Tables[0];//this DT has everything
grid.DataBind();

网格将显示所有数据表中的所有行

当DataTable具有不同的架构时,合并的DataTable将包含来自所有DataTable的所有列。

例如,将名为DataTable的{​​{1}}与另一个名为Col1的{​​{1}}列合并为DataTable将导致如下所示:

Col2

答案 2 :(得分:0)

您需要绑定到DataTable。 DataSet可以包含多个表。

DataSet.Tables Property

答案 3 :(得分:0)

使用@Kapil和@Icarus回复,我能够让它发挥作用。 感谢您抽出宝贵时间提供帮助。它指出了我正确的方向。

这就是我最终的结果:

  Protected Sub btnSearch_Click(sender As Object, e As System.EventArgs)
    Dim DS As DataSet
    Dim MyConnection As SqlConnection
    Dim MyDataAdapter As SqlDataAdapter
    Dim msg As String = ""

    'Create a connection to the SQL Server.
    MyConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("msSQLdb").ConnectionString)

    'Create a DataAdapter, and then provide the name of the stored procedure.
    MyDataAdapter = New SqlDataAdapter("sp_search_all_SQLservers", MyConnection)

    'Set the command type as StoredProcedure.
    MyDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure

    'Create and add a parameter to Parameters collection for the stored procedure.
    MyDataAdapter.SelectCommand.Parameters.Add(New SqlParameter("@ACTNbr", SqlDbType.VarChar, 10))
    MyDataAdapter.SelectCommand.Parameters.Add(New SqlParameter("@WTHNbr", SqlDbType.VarChar, 15))

    'Assign the search value to the parameter.
    MyDataAdapter.SelectCommand.Parameters("@ACTNbr").Value = tbACTNbr.Text.ToString.Trim
    MyDataAdapter.SelectCommand.Parameters("@WTHNbr").Value = tbWTHNbr.Text.ToString.Trim

    'Create a new DataSet to hold the records.
    DS = New DataSet()
    'Fill the DataSet with the rows returned.
    MyDataAdapter.Fill(DS, "proc_results")

    'Set the data source for the gridview as the DataSet that holds the rows.
    gv.DataSource = DS.Tables("proc_results").DefaultView
    For i = 1 To DS.Tables.Count - 1
      DS.Tables(0).Merge(DS.Tables(i))
    Next

    msg = DS.Tables(0).Rows.Count & " rows found"
    gv.Caption = DS.Tables(0).Rows.Count & " rows found"
    gv.DataSource = DS

    'Bind the DataSet to the DataGrid. 
    'NOTE: If you do not call this method, the DataGrid is not displayed!    
    gv.DataBind()
    If gv.Rows.Count <> 0 Then
      gv.Visible = True
    Else
      msg = "No data found"
    End If

    MyDataAdapter.Dispose() 'Dispose of the DataAdapter.
    MyConnection.Close() 'Close the connection.
    DS.Dispose()
    lblMsg.Text = msg
  End Sub
相关问题