我有一个用于查询 DB2 数据库的包装器。我设置包装器的方式是在查询方法中创建和处理连接。这样,我的包装器的消费者不必担心管理(打开和关闭)连接。有没有办法用存储过程做到这一点?我的一些用户发送出站参数,有没有办法将这些参数转换为数据表,就像我在上面的查询中所做的那样?
/// <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;
}