EnterpriseLibrary dbCommand.ExecuteScalarAsync is error :Invalid operation. The connection is closed

408 views Asked by At

I use EnterpriseLibrary to access database with async method.but it is error this:"Invalid operation. The connection is closed."

my code:

        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 method(dbCommand.ExecuteScalar()) is ok.

Note:my way to use is wrong,see:https://msdn.microsoft.com/zh-cn/library/hh211418.aspx

0

There are 0 answers