我正在使用Microsoft.Practices.EnterpriseLibrary v2.0.50727。 当我使用方法" GetStoredProcCommand"通过过程名称(例如A)获取命令对象,但它很少有机会执行另一个过程,例如B. 这是我的数据库类。
public class DatabaseBase
{
private string _connString = "ConnectionString";
private DbCommand _commandObject;
protected string ConnectionString
{
get { return _connString; }
set { _connString = value; }
}
protected Database DatabaseObject
{
get { return DatabaseFactory.CreateDatabase(_connString); }
}
protected DbCommand CommandObject
{
get { return _commandObject; }
set { _commandObject = value; }
}
public DatabaseBase()
{
}
protected void SetCommandObjectProcedure(string procedure)
{
CommandObject = DatabaseObject.GetStoredProcCommand(procedure);
CommandObject.Parameters.Clear();
}
public void MethodA(int a)
{
SetCommandObjectProcedure("A");
DatabaseObject.AddInParameter(CommandObject, "a1", DbType.Int32, a);
DatabaseObject.ExecuteReader(CommandObject);
}
public IDataReader MethodB(int b1,int b2)
{
SetCommandObjectProcedure("B");
DatabaseObject.AddInParameter(CommandObject, "b1", DbType.Int32, b1);
DatabaseObject.AddInParameter(CommandObject, "b2", DbType.Int32, b2);
return DatabaseObject.ExecuteReader(CommandObject);
}
}
当我在循环中调用MethodA数千次
时 foreach (inta in listA)
{
Database.MethodA(int a);
}
我将有一个很小的机会得到这样的例外:
Procedure or function B has too many arguments specified.
at Database.MethodA(int a)
.......奇迹! 没有多线程。 程序B如何处理方法A? 有人可以帮我指出发生了什么吗?
修改方法A,如下所示:
public void MethodA(int a)
{
using(var command = DatabaseObject.GetStoredProcCommand("A"))
{
DatabaseObject.AddInParameter(command , "a1", DbType.Int32, a);
DatabaseObject.ExecuteReader(command );
}
}
现在该命令被处理掉了。但问题仍然存在。