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

                    for (int i = 0; i < roleNames.Length; i++)

                    {

                        sql = "SELECT id FROM roles WHERE name = '" + roleNames[i] + "';";

                        comm = new MySqlCommand(sql, conn);

                        reader = comm.ExecuteReader();

                        while (reader.Read())

                        {

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

                        }

                        reader.Close();

                    }

                    //НАЗНАЧАЕМ РОЛИ

                    for (int i = 0; i < usernames.Length; i++)

                    {

                        if (usernames[i] == null || usernames[i] == "" || roleNames[i] == null || roleNames[i] == "") throw new ProviderException();

                        else

                        {

                            sql = "UPDATE auth_user SET role = "+id_role[i]+" WHERE id = "+usernames[i]+";";

                            comm = new MySqlCommand(sql, conn);

                            comm.ExecuteNonQuery();

                        }

                    }

                    conn.Close();

                }

            }

            catch (MySqlException exp)

            {

                conn.Close();

                return;

            }

            catch (ProviderException exp)

            {

                conn.Close();

                return;

            }

        }

        public override string ApplicationName

        {

            get

            {

                return applicationName;

            }

            set

            {

                applicationName = value;

            }

        }

        public override void CreateRole(string roleName)

        {

            MySqlConnection conn = null;

            try

            {

                if (roleName.Length > 25) throw new ProviderException();

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

                else

                {

                    //ОБЪЯВЛЯЕМ

                    MySqlCommand comm;

                    conn = new MySqlConnection(connectionString);

                    conn.Open();

                    string sql;

                    DataSet data = new DataSet();

                    MySqlDataAdapter adapter;

                    //ПРОВЕРЯЕМ, ЕСТЬ ЛИ УЖЕ ТАКАЯ РОЛЬ

                    sql = "SELECT * FROM roles WHERE name = '" + roleName + "';";

                    adapter = new MySqlDataAdapter(sql, conn);

                    adapter.Fill(data);

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

                    {

                        throw new ProviderException();

                    }

                    //ЕСЛИ НЕТ, СОЗДАЕМ ЕЁ

                    else

                    {

                        sql = "INSERT INTO roles(id,name) VALUE (default, '" + roleName + "');";

                        comm = new MySqlCommand(sql, conn);

                        comm.ExecuteNonQuery();

                        conn.Close();

                    }

                }

            }

            catch (MySqlException exp)

            {

                conn.Close();

                return;

            }

            catch (ProviderException exp)

            {

                conn.Close();

                return;

            }

        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)

        {

            MySqlConnection conn = null;

            try

            {

                if (roleName.Length > 25) throw new ProviderException();

                else if (throwOnPopulatedRole) throw new ProviderException();

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