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.
4 个解决方案
#1
5
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;
}
#2
0
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前进到第二个结果集。
#3
0
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等。
#4
0
* 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 As Integer
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
'follow方法获取gloabal数组excelSheetNames中的所有Excel工作表名称
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
#1
5
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;
}
#2
0
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前进到第二个结果集。
#3
0
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等。
#4
0
* 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 As Integer
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
'follow方法获取gloabal数组excelSheetNames中的所有Excel工作表名称
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