我正在使用 Codesmith 进行数据库连接,它不时抛出以下异常。这个类也有单例
我假设是 codesmith 自动处理打开和关闭与数据库的连接。
*中的错误。例如:System.InvalidOperationException:ExecuteReader 需要一个打开且可用的连接。连接的当前状态为关闭。在 System.Data.SqlClient.SqlCommand.ValidateCommand(String 方法,布尔异步) 在 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,布尔 returnStream,String 方法,TaskCompletionSource 1 完成,Int32 超时,任务和任务, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) at System.Data.Linq.SqlClient.SqlProvider.Execute(表达式查询,
代码:
/// <summary>
/// Returns DelimiterOfAnalyser
/// </summary>
/// <returns></returns>
public Dictionary<string, string> GetDelimiterOfAnalyser()
{
Dictionary<string, string> delimiters = new Dictionary<string, string>();
try
{
foreach (var delimiter in dataContext.DataSource.Where(a => a.TypeId == 1).ToList())
{
if (!delimiters.ContainsKey(delimiter.Name))
{
delimiters.Add(delimiter.Name, delimiter.Delimiter.Value);
}
}
}
catch (Exception ex)
{
logger.Error("Error in GetDelimiterOfAnalyser. Ex: " + ex);
}
return delimiters;
}
public static DBManager GetInstance()
{
try
{
if (dbMgr == null)
{
dbMgr = new DBManager();
}
dataContext = new DataContext();
}
catch (Exception ex)
{
logger.Error(ex);
}
return dbMgr;
}