我使用EnterpriseLibrary使用异步方法访问数据库。但这是错误的:"操作无效。连接已关闭。"
我的代码:
public Database CreateDatabase()
{
if (null == this._createDatabase)
{
this._createDatabase = new DatabaseProviderFactory().Create("ConnectionString");
}
return this._createDatabase;
}
public async Task<bool> IsExistCode(string code)
{
Database db = base.CreateDatabase();
using (DbCommand dbCommand = db.GetSqlStringCommand("select top 1 1 from Ads WITH(NOLOCK) where Code=@Code"))
{
db.AddInParameter(dbCommand, "Code", DbType.AnsiString, code);
var result = await dbCommand.ExecuteScalarAsync();//error occurrence
return null != result;
}
}
ps:sync方法(dbCommand.ExecuteScalar())没问题。
注意:我的使用方式有误,请参阅:https://msdn.microsoft.com/zh-cn/library/hh211418.aspx