eExecuteDataSet for system.data.common.dbCommand?
I am using system.data.common.dbcommand for database communications but I couldn't find ExecuteDataSet in it. Probably it is not supported. Plea开发者_JS百科se advice is there some way that I can read multiple datatable in one go (my stored procedure will return multiple selects).
Thanks
Loading data into a data-set is really the job of a data-adapter, so spin up an appropriate data-adapter and use that to load the data. You can also use dataSet.Load(reader)
.
However, please consider: are data-sets really the best metaphor for what you are doing?
public DataSet ExecuteDataSet(string procName,
params IDataParameter[] procParams)
{
SqlCommand cmd;
return ExecuteDataSet(out cmd, procName, procParams);
}
public DataSet ExecuteDataSet(out SqlCommand cmd, string procName,
params IDataParameter[] procParams)
{
SqlConnection cnx = null;
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
cmd = null;
try
{
//Setup command object
cmd = new SqlCommand(procName);
cmd.CommandType = CommandType.StoredProcedure;
if (procParams != null)
{
for (int index = 0; index < procParams.Length; index++)
{
cmd.Parameters.Add(procParams[index]);
}
}
da.SelectCommand = (SqlCommand)cmd;
System.Diagnostics.Trace.Write(da.SelectCommand);
//Determine the transaction owner and process accordingly
if (_isOwner)
{
cnx = new SqlConnection(GetConnectionString());
cmd.Connection = cnx;
cnx.Open();
}
else
{
cmd.Connection = _txn.Connection;
cmd.Transaction = _txn;
}
//Fill the dataset
da.Fill(ds);
}
catch
{
throw;
}
finally
{
if (da != null) da.Dispose();
if (cmd != null) cmd.Dispose();
if (_isOwner)
{
cnx.Dispose(); //Implicitly calls cnx.Close()
}`enter code here`
}
return ds;
}
精彩评论