C# 如何将多个表读入数据集?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/170455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:33:14  来源:igfitidea点击:

How can I read multiple tables into a dataset?

c#ado.netdataset

提问by mmattax

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?

我不确定如何阅读它...处理此类查询的最佳方法是什么,我猜我应该将数据读入 DataSet?最好的方法是如何做到这一点?

Thanks.

谢谢。

采纳答案by Sklivvz

Adapted from MSDN:

改编自MSDN

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

回答by Joe

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

如果要将结果读入 DataSet,最好使用 DataAdapter。

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

但是对于 DataReader,首先遍历第一个结果集,然后调用 NextResult 前进到第二个结果集。

回答by Steven A. Lowe

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

读者将按照返回的顺序处理结果集;处理完第一个结果集后,调用 rdr.NextResult() 设置下一个结果集

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.

另请注意,表适配器将在填充时自动将所有结果集读入数据集中的表中,但数据表将是无类型的并命名为 Table1、Table2 等。

回答by Sklivvz

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

*读取所有Excel工作表名称并将多个工作表添加到单个数据集中,表名作为工作表名称。*

'Global variables

'全局变量

Dim excelSheetNames As String()

Dim excelSheetNames As String()

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

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

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

Dim MyConnection As OleDbConnection

Dim MyConnection 为 OleDbConnection

Dim da As System.Data.OleDb.OleDbDataAdapter

Dim da As System.Data.OleDb.OleDbDataAdapter

Dim i As Integer

Dim i 作为整数

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

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"" ")

数据源=SStatus.xls;扩展属性=""Excel 8.0;HDR=NO;IMEX=1"" ")

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

'以下方法获取全局数组excelSheetNames中的所有Excel工作表名称

GetExcelSheetNames("SStatus.xls")

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)

公共函数 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