Enterprise Library 2.0 -- Data Access Application Block (补充)
前几天一位热心的朋友询问如何在DataAccess Application Block中将参数单独写成一个方法,以方便频繁调用一些存储过程,也可以将这些参数装入缓存以提高程序执行效率,下面将实现该功能的代码贴出来,希望对正在研究这方面知识的朋友有所帮助.
/// <summary>
/// 该方法用于返回一个存储过程的参数数组
/// 从而方便了我们在程序中反复调用
/// 而且我们可以将返回的参数数组装入缓存,提高程序执行效率
/// </summary>
/// <param name="db">数据库连接</param>
/// <param name="spName">存储过程名称</param>
/// <returns>返回参数数组</returns>
public DbParameter[] GetDBCommandParameters(Database db , string spName)
{
//建立一个存储过程的DBCommand
DbCommand dbcomm = db.GetStoredProcCommand(spName);
//根据字面意思可以看到执行该方法是发现DBCommand的参数,即得到该DBCommand的参数
db.DiscoverParameters(dbcomm);
//测试了一下,不管存储过程有没有返回值,在这个参数数组的0位置都是一个@return_value参数,奇怪,将其移出
dbcomm.Parameters.RemoveAt(0);
//定义一个新的参数数组,并将DBCommand中的参数复制到该数组中
DbParameter[] result = new DbParameter[dbcomm.Parameters.Count];
dbcomm.Parameters.CopyTo(result,0);
![](/Images/OutliningIndicators/InBlock.gif)
//将参数的初值设为空
foreach (DbParameter dp in result)
{
dp.Value = DBNull.Value;
}
![](/Images/OutliningIndicators/InBlock.gif)
return result;
}
然后我们在写一个给参数数组赋值的方法,如下:
/// <summary>
/// 对参数数组赋值
/// </summary>
/// <param name="commandParameters">参数数组</param>
/// <param name="parameterValues">对应值数组</param>
public void AssignParameters(DbParameter[] commandParameters, object[] parameterValues)
{
if ((commandParameters == null) || (parameterValues == null))
{
return;
}
![](/Images/OutliningIndicators/InBlock.gif)
if (commandParameters.Length != parameterValues.Length)
{
throw new ArgumentException("参数数组长度不匹配!");
}
![](/Images/OutliningIndicators/InBlock.gif)
for (int i = 0, j = commandParameters.Length; i < j; i++)
{
commandParameters[i].Value = parameterValues[i];
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
}
然后我们就可以在程序中按照如下方法调用存储过程了:
[TestMethod]
public void SelectPersonBySex()
{
string sex = "男";
![](/Images/OutliningIndicators/InBlock.gif)
//创建默认的数据库实例
Database db = DatabaseFactory.CreateDatabase();
string spName = "SelectPersonBySex";
![](/Images/OutliningIndicators/InBlock.gif)
//DbCommand dbcomm = db.GetStoredProcCommand(spName);
![](/Images/OutliningIndicators/InBlock.gif)
//得到该存储过程的参数数组
DbParameter[] parameters = this.GetDBCommandParameters(db, spName);
object[] strValues = new object[] { sex };
//对参数数组赋值
this.AssignParameters(parameters, strValues);
![](/Images/OutliningIndicators/InBlock.gif)
DataSet ds = db.ExecuteDataSet(spName, parameters);
![](/Images/OutliningIndicators/InBlock.gif)
Assert.AreEqual(ds.Tables[0].Rows.Count,1);
}
Email:pwei013@163.com
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
然后我们在写一个给参数数组赋值的方法,如下:
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
然后我们就可以在程序中按照如下方法调用存储过程了:
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
Email:pwei013@163.com
posted on 2006-06-12 18:28 Daniel Pang 阅读(6374) 评论(3) 编辑 收藏 举报