Ejemplo n.º 1
0
        /// <summary>
        ///     Insert Async.
        /// </summary>
        /// <param name="entity" />
        /// <param name="connectionString" />
        public static async Task <UserProfileDao> InsertAsync(this UserProfileDao entity, string connectionString)
        {
            // Sql
            string sql = @"INSERT INTO [Administration].[UserProfile] 
				 ([UserId], [FirstName], [LastName], [UserTypeId], [Title], [Suffix], [Prefix], [PrefferedName], [Dob], [GenderId], [CountryId], [Organization], [Department], [PictureUrl], [Udf1], [Udf2], [Udf3]) 
				 OUTPUT [INSERTED].UserId 
				 VALUES(@UserId, @FirstName, @LastName, @UserTypeId, @Title, @Suffix, @Prefix, @PrefferedName, @Dob, @GenderId, @CountryId, @Organization, @Department, @PictureUrl, @Udf1, @Udf2, @Udf3)"                ;

            // Parameters
            DynamicParameters para = new DynamicParameters();

            para.Add("@UserId", entity.UserId);
            para.Add("@FirstName", entity.FirstName);
            para.Add("@LastName", entity.LastName);
            para.Add("@UserTypeId", entity.UserTypeId);
            para.Add("@Title", entity.Title);
            para.Add("@Suffix", entity.Suffix);
            para.Add("@Prefix", entity.Prefix);
            para.Add("@PrefferedName", entity.PrefferedName);
            para.Add("@Dob", entity.Dob);
            para.Add("@GenderId", entity.GenderId);
            para.Add("@CountryId", entity.CountryId);
            para.Add("@Organization", entity.Organization);
            para.Add("@Department", entity.Department);
            para.Add("@PictureUrl", entity.PictureUrl);
            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.UserId = item;
                        return(entity);
                    }
                    catch (Exception e)
                    {
                        tn.Rollback();
                        if (con.State != ConnectionState.Closed)
                        {
                            con.Close();
                        }
                        return(null);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Update Async.
        /// </summary>
        /// <param name="entity" />
        /// <param name="connectionString" />
        public static async Task <UserProfileDao> UpdateAsync(this UserProfileDao entity, string connectionString)
        {
            // Sql
            string sql = @"UPDATE [Administration].[UserProfile] 
				 SET FirstName=@FirstName,LastName=@LastName,UserTypeId=@UserTypeId,Title=@Title,Suffix=@Suffix,Prefix=@Prefix,PrefferedName=@PrefferedName,Dob=@Dob,GenderId=@GenderId,CountryId=@CountryId,Organization=@Organization,Department=@Department,PictureUrl=@PictureUrl,Udf1=@Udf1,Udf2=@Udf2,Udf3=@Udf3 
				 WHERE UserId=@UserId;"                ;

            // Parameters
            DynamicParameters para = new DynamicParameters();

            para.Add("@UserId", entity.UserId);
            para.Add("@FirstName", entity.FirstName);
            para.Add("@LastName", entity.LastName);
            para.Add("@UserTypeId", entity.UserTypeId);
            para.Add("@Title", entity.Title);
            para.Add("@Suffix", entity.Suffix);
            para.Add("@Prefix", entity.Prefix);
            para.Add("@PrefferedName", entity.PrefferedName);
            para.Add("@Dob", entity.Dob);
            para.Add("@GenderId", entity.GenderId);
            para.Add("@CountryId", entity.CountryId);
            para.Add("@Organization", entity.Organization);
            para.Add("@Department", entity.Department);
            para.Add("@PictureUrl", entity.PictureUrl);
            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);
                    }
                }
            }
        }