有一个MVC Web应用程序,它有一个用ADO.NET实现的模块。 控制器代码如下:
private List<CustomTabModel> GenerateReports(ReportModel oViewModel)
{
List<CentralDbCase> Cases = AppController.Instance.Prov.Cases(a, b, c, InitiativeTypes(submissionYear), includedDataSource, namePrefix, out message);
//other code
return DoWork(Cases);
}
private List<CustomTabModel> DoWork(List<CentralDbCase> Cases)
{
//other code
}
当尝试连接到SQL数据库时,当两个或更多人同时连接到应用程序时,我收到以下错误:
ExecuteReader需要一个开放且可用的连接。连接的当前状态已打开。
以下是Business Classes,我正在使用此模块: 1个Provider.cs(在BusinessLayer文件夹中)
public List<CentralDbCase> Cases(string a, int b, List<KeyValuePair<string, string>> c, List<string> SelectedTypes, string includedDataSource, string NamePrefix, out string message)
{
List<CentralDbCase> theList = new List<CentralDbCase>();
string sql = (AppController.Instance.DbConnection is System.Data.SqlClient.SqlConnection) ? ListCaseSqlEx(a, b, c) : ListCaseSql(a, b, c);
IDbCommand cmd = DbHelper.NewCommandForConnection(AppController.Instance.DbConnection, sql);
IDbDataAdapter da = DbHelper.NewDataAdapterForConnection(AppController.Instance.DbConnection);
da.SelectCommand = cmd;
DataSet ds = new DataSet();
ds.CaseSensitive = true;
da.Fill(ds); //--getting error here--
//other line of code
return theList;
}
2 ICentralDbProvider.cs(在BusinessLayer文件夹中)
namespace PJ.Business
{
public interface ICentralDbProvider
{
List<CentralDbCase> Cases(string a, int b, List<KeyValuePair<string,string>> c,
List<string> SelectedTypes, string includedDataSource, string NamePrefix, out string message);
Dictionary<string, string> ListNames(int submissionYear);
VariableInfo[] VariableTypes { get; }
DataTable GetDataTable(int submissionYear, string scenario, TableName tableName, string tablePrefix, string whereClause, string includedDataSource);
}
}
3 AppController.cs(在BusinessLayer文件夹中)
namespace PJ.Business
{
public class AppController
{
IDbConnection dbConnection;
ICentralDbProvider cdbProv;
public static AppController Instance
{
get
{
if (_appController == null)
{
_appController = new AppController();
}
return _appController;
}
}
public void InitConnection()
{
string connString = ConfigurationManager.ConnectionStrings["ABC_Connection"].ConnectionString;
string provider = ConfigurationManager.ConnectionStrings["ABC_Connection"].ProviderName;
if (provider == EnumHelper.GetDescription(Provider.SQL))
{
dbConnection = new System.Data.SqlClient.SqlConnection(connString);
}
else if (provider == EnumHelper.GetDescription(Provider.Oracle))
{
dbConnection = new System.Data.OracleClient.OracleConnection(connString);
}
else
{
throw new Exception("Invalid Connection");
}
}
public IDbConnection DbConnection
{
get { return dbConnection; }
set { dbConnection = value; }
}
public ICentralDbProvider Prov
{
get { return cdbProv; }
set { cdbProv = value; }
}
}
}
4 DbHelper.cs(在BusinessLayer文件夹内)
namespace PJ.Business
{
internal static class DbHelper
{
public static IDbCommand NewCommandForConnection(IDbConnection conn, string commandText)
{
IDbCommand cmd = NewCommandForConnection(conn);
cmd.CommandText = commandText;
return cmd;
}
public static IDbCommand NewCommandForConnection(IDbConnection conn)
{
IDbCommand cmd;
if (conn is OracleConnection)
{
cmd = new OracleCommand();
}
else if (conn is SqlConnection)
{
cmd = new SqlCommand();
}
else
{
throw new Exception("Incorrect Connection class currently in use.");
}
cmd.Connection = conn;
return cmd;
}
public static IDbDataAdapter NewDataAdapterForConnection(IDbConnection conn)
{
IDbDataAdapter da;
if (conn is OracleConnection)
{
da = new OracleDataAdapter();
}
else if (conn is SqlConnection)
{
da = new SqlDataAdapter();
}
else
{
throw new Exception("Incorrect Connection class currently in use.");
}
return da;
}
}
}
注意:此模块使用Singelton Pattern。请告诉我代码中的错误。 提前致谢
编辑:堆栈跟踪
System.Data.SqlClient.SqlConnection.GetOpenConnection(String method) System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(字符串 方法,SqlCommand命令) System.Data.SqlClient.SqlCommand.ValidateCommand(String方法, 布尔异步) System.Data.SqlClient.SqlCommand.RunExecuteReader(的CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String 方法,TaskCompletionSource`1完成,Int32超时,任务&amp;任务, 布尔asyncWrite) System.Data.SqlClient.SqlCommand.RunExecuteReader(的CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String 方法)System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior 行为,字符串方法) System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(的CommandBehavior 行为) System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(的CommandBehavior 行为)System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset,DataTable [] datatables,Int32 startRecord,Int32 maxRecords, String srcTable,IDbCommand命令,CommandBehavior行为) System.Data.Common.DbDataAdapter.Fill(DataSet dataSet,Int32 startRecord,Int32 maxRecords,String srcTable,IDbCommand命令, CommandBehavior行为) System.Data.Common.DbDataAdapter.Fill(DataSet dataSet) PJ.Business.Provider.Cases(字符串a,int b,List&gt; c,List SelectedTypes,string includedDataSource, in string namePrefix,out string message)in c:\ Temp \ PJ.Business \ Provider.cs:第952行 PJ.WebClient.Areas.Tool.Controllers.ReportController.GenerateReports(ReportModel oViewModel)in C:\ TEMP \ PJ.WebClient \区\工具\ \控制器ReportController.cs:行 213 at PJ.WebClient.Areas.Tool.Controllers.ReportController.Index(ReportModel m)in C:\ TEMP \ PJ.WebClient \区\工具\ \控制器ReportController.cs:行 154