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[]:
DataTable[] dtOutputs = Generix.getGraphData(type);
//dtReturn[0]
DataTable dtOutput0 = dtOutputs[0];
//dtReturn[1]
DataTable dtOutput1 = dtOutputs[1];
//dtReturn[2]
DataTable dtOutput2 = dtOutputs[2];
Or if you need not only reference those table, but copy them, you can use .Copy() like:
//dtReturn[0]
DataTable dtOutput1 = dtOutputs[1].Copy;
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.)
public static DataSet getGraphData(Int32 type)
{
SqlConnection oConn = null;
DataSet dsReturn = null;
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();
//You don't need Dispose() - because the using will do that on sspObj
}
closeConnection(ref oConn);
}
catch (Exception xObj)
{
dsReturn = new DataSet("Empty");
}
return dsReturn ;
}
And calling the method like this:
DataSet dsOutput = Generix.getGraphData(type);
//Simply a reference or Copy() the DataSet's tables:
DataTable dtOutput0 = dsOutput.Tables[0];
DataTable dtOutput1 = dsOutput.Tables[1];
DataTable dtOutput2 = dsOutput.Tables[2];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With