(Answer in VB, but you should be able to convert it)
To use one query to load an entire dataset...
- Create your multi-result query.
- Make it a query for one of your dataset's tableadapters (to take advantage of the typed params, etc).
- Overload that query with a copy of the generated code.
- Replace the final fill(datatable) statement with a fill(dataset) statement.
- Run code to map tables to correct typed tables.
1) Write a stored procedure or sql statement that has multiple select statements that return results.
2) In one of your tableadapters in your dataset, add the new query.
3) Get the generated code for this query. The easiest way is to use the method for the query in code and then right click on the method and select "Go To Definition". Put that method in a separate cs or vb file. (See code-block B)
You need to get the namespace and tableadapter partial class for the method to work.
4) Add the dataset as a parameter to the overloaded function. That dataset will be the target of the new fill statement. (See code-block B)
5) Use the following code to match your dataset result tables.
Public Shared Sub DatasetAutoMerge(ByVal Source As Data.DataSet, ByVal Target As Data.DataSet)
Target.EnforceConstraints = False
For Each dtTarget As Data.DataTable In Target.Tables
For Each dtSource As Data.DataTable In Source.Tables
Dim dtMatch = dtSource
For Each dcTarget As Data.DataColumn In dtTarget.Columns
If Not dtSource.Columns.Contains(dcTarget.ColumnName) Then
'The source does not have a column we need by name, not a match'
dtMatch = Nothing
Exit For
End If
Next
If dtMatch IsNot Nothing Then
dtTarget.Merge(dtMatch, False, Data.MissingSchemaAction.Ignore)
Exit For
End If
Next
Next
Target.EnforceConstraints = True
End Sub
code-block B
(Sample of final version of override Method, VB)
Namespace dsMyDatasetTableAdapters
Partial Public Class Table1TableAdapter
Public Overridable Overloads Function Fill(ByVal dataset As dsMyDataset, ByVal OrderNumber As String) As Integer
Me.Adapter.SelectCommand = Me.CommandCollection(0)
If (RecordID Is Nothing) Then
Me.Adapter.SelectCommand.Parameters(0).Value = Global.System.DBNull.Value
Else
Me.Adapter.SelectCommand.Parameters(0).Value = CType(OrderNumber, String)
End If
'end autogenerated code'
'Start Custom Code'
Dim dsDump As New Data.DataSet
Me.Adapter.Fill(dsDump)
DatasetAutoMerge(dsDump, dataset)
End Function
End Class
End Namespace
I've used this process a few times. It is much better than writing separate queries if you know you want to load all this at the same time. I hope this helps you!