【VB.NET】ストアドプロシージャの複数の結果を扱う

ストアドプロシージャの複数の結果を扱う

 * SqlDataAdapterクラスを使用する

サンプル

ストアド / SelectCompanyInfo

CREATE PROCEDURE SelectCompanyInfo AS
  SELECT * FROM Company
  SELECT * FROM Brunch

VB / WebForm1.aspx

 * GridView x 2しただけ。

VB / WebForm1.aspx.vb

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim da As SqlDataAdapter = New SqlDataAdapter()
    Dim cmd As SqlCommand = New SqlCommand("SelectCompanyInfo")
    cmd.CommandType = CommandType.StoredProcedure
    Dim connect As SqlConnection = New SqlConnection("Data Source=localhost\SQLEXPRESS;Initial Catalog=XxxxDB;Integrated Security=True")
    cmd.Connection = connect
    da.SelectCommand = cmd

    Dim ds As DataSet = New DataSet()
    da.Fill(ds)

    Me.GridView1.DataSource = ds.Tables(0)
    Me.GridView1.DataBind()
    Me.GridView2.DataSource = ds.Tables(1)
    Me.GridView2.DataBind()
End Sub