I have a wrapper that I use to query a DB2 database. The way I have the wrapper set up, the connections are created and disposed within the query method. This way the consumers of my wrapper do not have to worry about managing (opening and closing) connections. Is there a way to do this with a stored procedure? Some of my users send in outbound parameters, is there a way to turn those parameters into a datatable, like I do above in my query?
/// <summary>
/// Query A database and return a DataTable of strings with no null
/// </summary>
/// <param name="txtQuery">The Query</param>
/// <param name="list">the paramaters for the query</param>
/// <returns>Datatable with results</returns>
public DataTable Query(string txtQuery, params string[] list)
{
//create return ovject
DataTable dt = new DataTable();
//pull dbconnection out of pool
using (var conn = new DB2Connection(connectionstring))
{
//open connection
if (!OpenConn(conn))
{
throw new Exception(“failed to connect”);
}
try
{
//query db
using (DB2Command cmd = new DB2Command())
{
cmd.Connection = conn;
cmd.CommandText = txtQuery;
for (int i = 0; i < list.Length; i++)
{
DB2Parameter param = new DB2Parameter(i.ToString(), list[i]);
cmd.Parameters.Add(param);
}
//fill datatable
using (DB2DataAdapter adap = new DB2DataAdapter())
{
adap.SelectCommand = cmd;
adap.Fill(dt);
}
}
}
catch (Exception ex)
{
throw ex;
}
}
return dt;
}
Its not going to be as easy to do it but it is possible. I’m assuming the DB2Parameter is essentially the same as the SqlParameter in this answer…
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparameter.aspx and related pages will be useful reading after (or during) what I’m saying here.
Essentially when calling a procedure with output parameters you call it in the same way as you normally would. For the output parameters you pass an object in as normal but there is a
Directionproperty on the parameter object that allows you to specify that it is an output parameter.Once you have called the appropriate execute method on the procedure then the values of those output parameters will be populated and can be retrieved from the parameter object.
Putting them into a DataTable is then up to you. You can create a datatable, adding a column to it for each output parameter and then add a row in with all the values.
The main problem with this is of course that when setting up the database call you need to know which parameters are goign to return something so you can set their direction appropriately. While you are passing just a string array then this is almost certainly not going to be possible.
The best approach to this would be to as others have suggested in comments return all data as a dataset. Any existing procedures that currently use output parameters could have wrapper procedures created that just capture the output parameters and then put them into a select statement that you can process as above.
The other option would be to have the caller of your
Querymethod pass in more details about the parameters they are using (such as in or out). Personally I prefer returning all data as selects rather than using output parameters though because it makes this sort of code simpler.