/// <summary> /// Insert Async. /// </summary> /// <param name="entity" /> /// <param name="connectionString" /> public static async Task <ReferenceItemDao> InsertAsync(this ReferenceItemDao entity, string connectionString) { // Sql string sql = @"INSERT INTO [Utility].[ReferenceItem] ([Id], [ReferenceId], [Code], [Description], [Archived], [CreatedOn], [ChangedOn], [Udf1], [Udf2], [Udf3]) OUTPUT [INSERTED].Id VALUES(@Id, @ReferenceId, @Code, @Description, @Archived, @CreatedOn, @ChangedOn, @Udf1, @Udf2, @Udf3)" ; // Parameters DynamicParameters para = new DynamicParameters(); para.Add("@Id", entity.Id); para.Add("@ReferenceId", entity.ReferenceId); para.Add("@Code", entity.Code); para.Add("@Description", entity.Description); para.Add("@Archived", entity.Archived); para.Add("@CreatedOn", entity.CreatedOn); para.Add("@ChangedOn", entity.ChangedOn); para.Add("@Udf1", entity.Udf1); para.Add("@Udf2", entity.Udf2); para.Add("@Udf3", entity.Udf3); // Db Operation using (var con = new SqlConnection(connectionString)) { await con.OpenAsync(); // Transaction using (IDbTransaction tn = con.BeginTransaction()) { try { var item = await con.QuerySingleAsync <Guid>(sql, para, tn); tn.Commit(); entity.Id = item; return(entity); } catch (Exception e) { tn.Rollback(); if (con.State != ConnectionState.Closed) { con.Close(); } return(null); } } } }
/// <summary> /// Update Async. /// </summary> /// <param name="entity" /> /// <param name="connectionString" /> public static async Task <ReferenceItemDao> UpdateAsync(this ReferenceItemDao entity, string connectionString) { // Sql string sql = @"UPDATE [Utility].[ReferenceItem] SET ReferenceId=@ReferenceId,Code=@Code,Description=@Description,Archived=@Archived,CreatedOn=@CreatedOn,ChangedOn=@ChangedOn,Udf1=@Udf1,Udf2=@Udf2,Udf3=@Udf3 WHERE Id=@Id;" ; // Parameters DynamicParameters para = new DynamicParameters(); para.Add("@Id", entity.Id); para.Add("@ReferenceId", entity.ReferenceId); para.Add("@Code", entity.Code); para.Add("@Description", entity.Description); para.Add("@Archived", entity.Archived); para.Add("@CreatedOn", entity.CreatedOn); para.Add("@ChangedOn", entity.ChangedOn); para.Add("@Udf1", entity.Udf1); para.Add("@Udf2", entity.Udf2); para.Add("@Udf3", entity.Udf3); // Db Operation using (var con = new SqlConnection(connectionString)) { await con.OpenAsync(); // Transaction using (IDbTransaction tn = con.BeginTransaction()) { try { var item = await con.ExecuteAsync(sql, para, tn); tn.Commit(); return(entity); } catch (Exception e) { tn.Rollback(); if (con.State != ConnectionState.Closed) { con.Close(); } return(null); } } } }