How can I read multiple tables into a dataset?

帅比萌擦擦* 提交于 2020-01-01 11:47:09

问题


I have a stored procedure that returns multiple tables. How can I execute and read both tables?

I have something like this:


SqlConnection conn = new SqlConnection(CONNECTION_STRING);
SqlCommand cmd = new SqlCommand("sp_mult_tables",conn);
cmd.CommandType = CommandType.StoredProcedure);

IDataReader rdr = cmd.ExecuteReader();

I'm not sure how to read it...whats the best way to handle this type of query, I am guessing I should read the data into a DataSet? How is the best way to do this?

Thanks.


回答1:


Adapted from MSDN:

using (SqlConnection conn = new SqlConnection(connection))
{
    SqlDataAdapter adapter = new SqlDataAdapter();
    adapter.SelectCommand = new SqlCommand(query, conn);
    adapter.Fill(dataset);
    return dataset;
}



回答2:


If you want to read the results into a DataSet, you'd be better using a DataAdapter.

But with a DataReader, first iterate through the first result set, then call NextResult to advance to the second result set.




回答3:


the reader will deal with the result sets in the order returned; when done processing the first result set, call rdr.NextResult() to set for the next one

note also that a table adapter will automatically read all result sets into tables in a dataset on fill, but the datatables will be untyped and named Table1, Table2, etc.




回答4:


* Reading All Excel sheet names and adding multiple sheets into single dataset with table names as sheet names.*

'Global variables

Dim excelSheetNames As String()

Dim DtSet As System.Data.DataSet = New DataSet()

Private Sub btnLoadData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadData.Click

Dim MyConnection As OleDbConnection

Dim da As System.Data.OleDb.OleDbDataAdapter

Dim i As Integer

MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;

data source=SStatus.xls;Extended Properties=""Excel 8.0;HDR=NO;IMEX=1"" ")

'following method gets all the Excel sheet names in the gloabal array excelSheetNames

GetExcelSheetNames("SStatus.xls")

        For Each str As String In excelSheetNames
            da = New OleDbDataAdapter("select * from [" & str & "]", MyConnection)
            da.Fill(DtSet, excelSheetNames(i))
            i += 1
        Next           
        DataGridView1.DataSource = DtSet.Tables(0)          

End Sub

Public Function GetExcelSheetNames(ByVal excelFileName As String)

    Dim con As OleDbConnection = Nothing

    Dim dt As DataTable = Nothing

    Dim conStr As String = ("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=") + excelFileName & ";Extended Properties=Excel 8.0;"

    con = New OleDbConnection(conStr)
    con.Open()
    dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
    excelSheetNames = New String(dt.Rows.Count - 1) {}
    Dim i As Integer = 0

    For Each row As DataRow In dt.Rows
        excelSheetNames(i) = row("TABLE_NAME").ToString()
        i += 1
    Next
End Function


来源:https://stackoverflow.com/questions/170455/how-can-i-read-multiple-tables-into-a-dataset

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!