I have Stored Procedure which returns Multiple tables as ResultSet. I Stored it in DataTable and Pass that DataTable Object to Another Form Which Print Tables In MY Web Page.
My Question is How Can i Stored Multiple ResultSet Returned from Stored Procedure into SINGLE DataTable Object Which I Can return to another function.
public static DataTable[] getGraphData(Int32 type)
{
SqlConnection oConn = null;
DataSet dsReturn = null;
DataTable[] dtReturn=new DataTable[2];
try
{
getConnection(ref oConn, 1);
using (SqlStoredProcedure sspObj = new SqlStoredProcedure("dbo.usp_getGraphData", oConn, CommandType.StoredProcedure))
{
sspObj.AddParameterWithValue("@Type", SqlDbType.Int, 0, ParameterDirection.Input, type);
dsReturn = sspObj.ExecuteDataSet();
dtReturn[0] = dsReturn.Tables[0];
dtReturn[1] = dsReturn.Tables[1];
dtReturn[2] = dsReturn.Tables[2];
sspObj.Dispose();
}
closeConnection(ref oConn);
}
catch (Exception xObj)
{
//dtReturn[] = new DataTable();
}
return dtReturn;
}
Function Which Takes the all three DataTables
DataTable dtOutput = Generix.getGraphData(type);
How to get Each DataTable From Here ? Means 0th Element to dtOutput1 , 1st Element to dtOutput2…..Like wise
Why don’t you use a dataset as podiluska mentioned, and then access the DataSet.Tables property to gets your three table one by one.
If the three table structure is difderent does not make sense to foece them into a table. For this the DataSet is the better way. It is kind of collection of your table.
EDIT for the question about the DataTable[]:
Or if you need not only reference those table, but copy them, you can use .Copy() like:
I still think that the best way would be returning a DataSet by the query (it doesn’t make sense to make a DataTable query from that.)
And calling the method like this: