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

Asp.net 使用返回多个记录集的SQL Server存储过程的结果填充gridview,asp.net,sql-server,vb.net,tsql,stored-procedures,Asp.net,Sql Server,Vb.net,Tsql,Stored Procedures,我有一个返回多个记录集的存储过程。它可以是1个记录集、2个记录集或更多。我不知道有多少人会回来 在stackoverflow,我找到了以下示例 Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("myConnection").ConnectionString) Dim cmd As New SqlCommand Dim Reader As SqlDataReader cmd.C

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

在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
当然,我只有一个记录集

存储过程的结果都被格式化为相同的格式—相同数量的列、标题等

谁能给我指一下正确的方向吗?我一直想弄明白,但是放弃了,现在我在这里问

我是新来的


谢谢。

使用DataAdapter而不是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.

您需要绑定到数据表。一个数据集可以容纳多个表


您只能将GridView绑定到一个结果集。例如,将
网格视图
绑定到包含多个数据表的
数据集
,将只显示第一个
数据表
中的数据

由于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();
网格将显示所有数据表中的所有行

当数据表具有不同的模式时,合并的数据表将具有来自所有数据表的所有列

例如,将一个名为
Col1
列的
DataTable
与另一个名为
Col2
列的
DataTable
合并,将产生如下结果:

  +-----+------+
  |Col1 | Col2 |
  ------+------+
  |val1 |  null|
  +-----+------+
  |null |  val2|
  +-----+------+

通过使用@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
受保护的子BTN搜索\u单击(发件人作为对象,e作为系统.EventArgs)
Dim DS作为数据集
Dim MyConnection作为SqlConnection
将MyDataAdapter设置为SqlDataAdapter
Dim msg As String=“”
'创建到SQL Server的连接。
MyConnection=新的SqlConnection(ConfigurationManager.ConnectionString(“MSSQLSDB”).ConnectionString)
'创建DataAdapter,然后提供存储过程的名称。
MyDataAdapter=新的SqlDataAdapter(“sp\u搜索\u所有\u SQLServer”,MyConnection)
'将命令类型设置为StoredProcedure。
MyDataAdapter.SelectCommand.CommandType=CommandType.StoredProcess
'创建一个参数并将其添加到存储过程的参数集合中。
MyDataAdapter.SelectCommand.Parameters.Add(新的SqlParameter(“@ACTNbr”,SqlDbType.VarChar,10))
MyDataAdapter.SelectCommand.Parameters.Add(新的SqlParameter(“@WTHNbr”,SqlDbType.VarChar,15))
'将搜索值指定给参数。
MyDataAdapter.SelectCommand.Parameters(“@ACTNbr”)。Value=tbACTNbr.Text.ToString.Trim
MyDataAdapter.SelectCommand.Parameters(“@WTHNbr”).Value=tbWTHNbr.Text.ToString.Trim
'创建新数据集以保存记录。
DS=新数据集()
'用返回的行填充数据集。
MyDataAdapter.Fill(DS,“过程结果”)
'将gridview的数据源设置为保存行的数据集。
gv.DataSource=DS.Tables(“proc_结果”).DefaultView
对于i=1到DS.Tables.Count-1
DS.表(0).合并(DS.表(i))
下一个
msg=DS.Tables(0).Rows.Count&“找到行”
gv.Caption=DS.Tables(0).Rows.Count&“找到行”
gv.DataSource=DS
'将数据集绑定到DataGrid。
'注意:如果不调用此方法,则不会显示DataGrid!
gv.DataBind()
如果gv.Rows.Count为0,则
gv.Visible=True
其他的
msg=“未找到任何数据”
如果结束
MyDataAdapter.Dispose()'处理DataAdapter。
MyConnection.Close()'关闭连接。
DS.Dispose()
lblMsg.Text=msg
端接头

请将答案标记为正确。
  +-----+------+
  |Col1 | Col2 |
  ------+------+
  |val1 |  null|
  +-----+------+
  |null |  val2|
  +-----+------+
  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