コード例 #1
0
        private void registracia_Click(object sender, EventArgs e)
        {
            if (last_name.Text == "" || name.Text == "" || dad_name.Text == "" || login.Text == "" || pass.Text == "" || email.Text == "" || phone.Text == "")
            {
                MessageBox.Show("Вы заполнили не все поля, нажмите ОК, чтобы продолжить", "Уведомление", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                con.Open();
                DataTable tb = new DataTable();
                SqlDataAdapter ad = new SqlDataAdapter();
                command = new SqlCommand("select id from users where login='******'",con);
                ad.SelectCommand = command;
                ad.Fill(tb);
                if (tb.Rows.Count>0)
                {
                    MessageBox.Show("Пользователь с таким логином уже существует,\nпожалуйста,придумайте другой логин","Уведомление", MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
                    login.Clear();
                    con.Close();
                }
                else
                {
                    SHA256_pwd toAes = new SHA256_pwd();
                    string ecnrypted_password = toAes.SHA256HexHashString(pass.Text);
                   
                    command.Dispose();
                    command = new SqlCommand("users_insert", con);
                    command.CommandType = CommandType.StoredProcedure;
                    command.Parameters.Add(new SqlParameter("@last_name", SqlDbType.NChar));
                    command.Parameters["@last_name"].Value = last_name.Text;
                    command.Parameters.Add(new SqlParameter("@name", SqlDbType.NChar));
                    command.Parameters["@name"].Value = name.Text;
                    command.Parameters.Add(new SqlParameter("@dad_name", SqlDbType.NChar));
                    command.Parameters["@dad_name"].Value = dad_name.Text;
                    command.Parameters.Add(new SqlParameter("@login", SqlDbType.NChar));
                    command.Parameters["@login"].Value = login.Text;
                    command.Parameters.Add(new SqlParameter("@pass", SqlDbType.NVarChar));
                    command.Parameters["@pass"].Value = ecnrypted_password;
                    command.Parameters.Add(new SqlParameter("@email", SqlDbType.NChar));
                    command.Parameters["@email"].Value = email.Text;
                    command.Parameters.Add(new SqlParameter("@phone", SqlDbType.NChar));
                    command.Parameters["@phone"].Value = phone.Text;
                    command.Parameters.Add(new SqlParameter("@birth", SqlDbType.Date));
                    command.Parameters["@birth"].Value = birth.Text;

                    if (command.ExecuteNonQuery() ==1)
                    {
                        MessageBox.Show("Пользователь успешно добавлен!", "Уведомление", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("Ошибочка!");
                    }
                    con.Close();
                }
                
                
            }
        }
コード例 #2
0
ファイル: Form1.cs プロジェクト: ksya0307/WindowsFormsApp2
        private void autouser_Click(object sender, EventArgs e)
        {
            SHA256_pwd shifr = new SHA256_pwd();

            //если не заполнено одно из полей ввода
            if (textBox1.Text == "" || textBox2.Text == "")
            {
                MessageBox.Show("Вы заполнили не все поля,\n" +
                                "нажмите ОК, чтобы продолжить", "Уведомление", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                con.Open();//открытие подключения
                //установление выражения которое будет выполняться
                cmd = new SqlCommand("select login, password from dbo.users where login='******' and password='******';", con);
                //выполняет sql-выражение и возвращает строки из таблицы
                SqlDataReader reader = cmd.ExecuteReader();
                string        login  = "";
                if (reader.HasRows)       //если вернул строки
                {
                    while (reader.Read()) // построчно считываем данные
                    {
                        textBox1.Focus();
                        login = reader.GetString(0);//получаем в переменную login
                        login = login.Trim(' ');
                    }
                    reader.Close(); //закрытие
                    cmd.Dispose();  //сброс ресурсов
                }
                else
                {
                    MessageBox.Show("Неправильный логин или пароль,\n" +
                                    "пожалуйста,повторите попытку ",
                                    "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    textBox1.Clear();
                    textBox2.Clear();
                    textBox1_Enter(sender, EventArgs.Empty);
                    textBox2_Enter(sender, EventArgs.Empty);
                    textBox1_Leave(sender, EventArgs.Empty);
                    textBox2_Leave(sender, EventArgs.Empty);
                }
                con.Close();//закрытие подключения

                con.Open();
                //для получения роли
                cmd  = new SqlCommand("select role from dbo.users where login='******'", con);
                role = Convert.ToInt32(cmd.ExecuteScalar());
                con.Close();

                switch (role)
                {
                case 1:    //если роль =1 открытие формы для клиента
                    Form4 form4 = new Form4(login);
                    form4.Show();
                    this.Hide();
                    break;

                case 2:    //роль =2 открывается форма сотрудника
                    Form3 form3 = new Form3(login);
                    form3.Show();
                    this.Hide();
                    break;

                case 3:    //роль =3 открывается форма администратора
                    Form2 form2 = new Form2(login);
                    form2.Show();
                    this.Hide();
                    break;
                }
            }
        }