Exemplo n.º 1
0
        /// <summary>
        /// Load a list of users from the database.
        /// </summary>
        /// <param name="connectionString">The connection string to the database.</param>
        /// <param name="filter">The filter used to select users.</param>
        /// <returns></returns>
        public static List <User> LoadCollection(UserFilter filter)
        {
            List <User>   users  = new List <User>();
            SqlCommand    cmd    = new SqlCommand();
            StringBuilder select = new StringBuilder(1000);

            if (filter.Guid != Guid.Empty)
            {
                select.Append("UM.user_master_guid=@user_master_guid");
                cmd.Parameters.Add(new SqlParameter("@user_master_guid", SqlDbType.UniqueIdentifier)
                {
                    Value = filter.Guid
                });
            }

            if (!String.IsNullOrEmpty(filter.UserName))
            {
                if (select.Length > 0)
                {
                    select.Append(" AND ");
                }
                select.Append("UM.user_name=@user_name");
                cmd.Parameters.Add(new SqlParameter("@user_name", SqlDbType.NVarChar)
                {
                    Value = filter.UserName
                });
            }

            if (!String.IsNullOrEmpty(filter.UserPassword))
            {
                if (select.Length > 0)
                {
                    select.Append(" AND ");
                }
                select.Append("UM.user_password=@user_password");
                cmd.Parameters.Add(new SqlParameter("@user_password", SqlDbType.NVarChar)
                {
                    Value = filter.UserPassword
                });
            }

            if (!String.IsNullOrEmpty(filter.UserSalt))
            {
                if (select.Length > 0)
                {
                    select.Append(" AND ");
                }
                select.Append("UM.user_salt=@user_salt");
                cmd.Parameters.Add(new SqlParameter("@user_salt", SqlDbType.NVarChar)
                {
                    Value = filter.UserSalt
                });
            }

            select.Insert(0, String.Format("SELECT * FROM User_Master UM {0} ", cmd.Parameters.Count > 0 ? "WHERE" : String.Empty));
            select.Append(" ORDER BY UM.user_name");
            cmd.CommandText = select.ToString();

            using (SqlConnection cn = new SqlConnection(HConfig.DBConnectionString))
            {
                using (DataTable dt = HDatabase.FillDataTable(cn, cmd))
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        users.Add(new User(dr));
                    }
                }
            }

            return(users);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Load a single user.
        /// </summary>
        /// <param name="connectionString">The connection string to the database.</param>
        /// <param name="filter">The filter used to select users.</param>
        /// <returns></returns>
        public static User Load(UserFilter filter)
        {
            List <User> users = User.LoadCollection(filter);

            return(users.Count > 0 ? users[0] : null);
        }