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;
}