Создание веб-приложения, сочетающего в себе проектирование и создание баз данных, работу со сторонними форматами данных, программирование в NET и веб-разработку, страница 63

                    string sql;

                    string[] all_roles = null;

                    MySqlDataAdapter adapter;

                    DataSet data = new DataSet();

                    //ДОСТАЕМ ВСЕ РОЛИ ИЗ БАЗЫ

                    sql = "SELECT name FROM roles ;";

                    adapter = new MySqlDataAdapter(sql, conn);

                    adapter.Fill(data);

                    if (data.Tables[0].Rows.Count == 0)

                    {

                        conn.Close();

                        return null;

                    }

                    else

                    {

                        all_roles = new string[data.Tables[0].Rows.Count];

                        for (int i = 0; i < data.Tables[0].Rows.Count; i++)

                        {

                            all_roles[i] = data.Tables[0].Rows[i]["name"].ToString();

                        }

                        conn.Close();

                        return all_roles;

                    }

            }

            catch (MySqlException exp)

            {

                conn.Close();

                return null;

            }

            catch (ProviderException exp)

            {

                conn.Close();

                return null;

            }

        }

        //возвращает массив с названием роли юзера (массив содержит 1 элемент, т.к. у каждого юзера может быть только 1 роль)

        public override string[] GetRolesForUser(string username)

        {

            MySqlConnection conn = null;

            try

            {

                if (username == null || username == "") throw new ProviderException();

                else

                {

                    //ОБЪЯВЛЕНИЕ

                    conn = new MySqlConnection(connectionString);

                    conn.Open();

                    string sql;

                    string[] roles = new string[1];

                    int id_role = 0;

                    MySqlCommand comm;

                    MySqlDataReader reader = null;

                    //ДОСТАЕМ ВСЕ РОЛИ ИЗ БАЗЫ

                    sql = "SELECT role FROM auth_user WHERE name = '" + username + "';";

                    comm = new MySqlCommand(sql, conn);

                    reader = comm.ExecuteReader();

                    while (reader.Read())

                    {

                        id_role = Convert.ToInt32(reader[0].ToString());

                    }

                    reader.Close();

                    if (id_role == 0) throw new ProviderException();

                    else

                    {

                        sql = "SELECT name FROM roles WHERE id=" + id_role + ";";

                        comm = new MySqlCommand(sql, conn);

                        reader = comm.ExecuteReader();

                        while (reader.Read())

                        {

                            roles[0] = reader[0].ToString();

                        }

                        reader.Close();

                        conn.Close();

                        return roles;

                    }

                }

            }

            catch (MySqlException exp)

            {

                conn.Close();

                return null;

            }

            catch (ProviderException exp)

            {

                conn.Close();

                return null;

            }

        }

        //возвращает массив с именами юезров заданой роли

        public override string[] GetUsersInRole(string roleName)

        {

            MySqlConnection conn = null;

            try

            {

                if (roleName == null || roleName == "") throw new ProviderException();

                else

                {

                    //ОБЪЯВЛЕНИЕ

                    conn = new MySqlConnection(connectionString);

                    conn.Open();