• Как сделать авторизацию и регистрацию на C#?

    @Mitrofan_Aleksandrovich
    database.openConnection();
    var loginUser = LoginField.Text;
    var passUser=PasswordField.Text;
    SqlDataAdapter adapter= new SqlDataAdapter();
    DataTable table = new DataTable();

    string querystring = $"select Id_пользователя,Логин_пользователя,Пароль_пользователя,is_Admin from Авторизация where Логин_пользователя='{loginUser}' and Пароль_пользователя = '{passUser}' ";

    SqlCommand comand =new SqlCommand(querystring,database.getConnection());

    adapter.SelectCommand = comand;
    adapter.Fill(table);

    if (LoginField.TextLength <= 0)
    {
    MessageBox.Show("Не заполнено поле Логин");

    }

    if (PasswordField.TextLength <= 0)
    {
    MessageBox.Show("Не заполнено поле Пароль");

    }

    if (table.Rows.Count == 1)
    {
    SqlDataReader dr= comand.ExecuteReader();
    dr.Read();
    if (dr[3].ToString() == "Admin"){

    Database.type = "A";

    }

    else if (dr[3].ToString() == "User")
    {

    Database.type = "U";

    }

    MessageBox.Show("Вы успешно вошли!", "Успешно!", MessageBoxButtons.OK, MessageBoxIcon.Information);
    Authorization frm = new Authorization();

    frm.Show();

    this.Hide();

    }
    else {

    MessageBox.Show("Такого аккаунта не существует!!!", "Ошибка!!!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    return;

    РЕГИСТРАЦИЯ

    var login = LoginField.Text;
    var password = PasswordField.Text;
    int ID = Convert.ToInt32(ID_Field.Text);
    var admin = "User";
    SqlCommand cmd;
    SqlDataReader dr;
    SqlConnection cn;

    string querystring = $"insert into Авторизация (Id_пользователя,Логин_пользователя,Пароль_пользователя,is_Admin) values('{ID}','{login}','{password}','{admin}') ";

    cn = new SqlConnection(@"Data Source=MSI\MSISQLSERVER2023;Initial Catalog=soz_zahita;Integrated Security=True;MultipleActiveResultSets=True");
    cn.Open();


    database.openConnection();

    if (LoginField.Text == "Введите логин")
    {

    MessageBox.Show("Введите логин");
    return;

    }

    if (ID_Field.Text == "ID")
    {

    MessageBox.Show("Введите ID");
    return;

    }

    if (PasswordField.Text == "Введите пароль")
    {

    MessageBox.Show("Введите пароль");
    return;

    }

    if (LoginField.Text != string.Empty || PasswordField.Text != string.Empty )
    {

    if (PasswordField.Text == PasswordField.Text)
    {

    cmd = new SqlCommand("select * from Авторизация where Логин_пользователя='"+LoginField.Text+"'",cn);
    dr = cmd.ExecuteReader();
    if (dr.Read())
    {

    dr.Close();
    MessageBox.Show("Такой аккаунт уже существует!!!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);

    }
    else
    {
    dr.Close();

    cmd = new SqlCommand(querystring, database.getConnection());
    cmd.Parameters.AddWithValue("Id_пользователя", ID);
    cmd.Parameters.AddWithValue("Логин_пользователя", login);
    cmd.Parameters.AddWithValue("Пароль_пользователя", password);
    cmd.ExecuteNonQuery();

    MessageBox.Show("Аккаунт успешно создан!!!", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information);

    Form_Autorization frm = new Form_Autorization();

    this.Hide();
    frm.ShowDialog();

    }


    }
    else
    {
    MessageBox.Show("Этот аккаунт уже существует!!!!!! .", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    private void label6_Click(object sender, EventArgs e)
    {
    con.Open();
    adapter = new SqlDataAdapter("select * from Сотрудники", con);
    dt = new System.Data.DataTable();
    adapter.Fill(dt);
    dataGridView1.DataSource = dt;
    con.Close();
    }
    private void textBoxROOM_TextChanged(object sender, EventArgs e)
    {
    con.Open();
    adapter = new SqlDataAdapter("select * from Сотрудники where Кабинет like '" + textBoxROOM.Text + "%'", con);
    dt = new System.Data.DataTable();
    adapter.Fill(dt);
    dataGridView1.DataSource = dt;
    con.Close();
    }

    UPDATE Сотрудники SET [Фото] = (SELECT
    MyImage.* from Openrowset(Bulk
    'C:\Users\79204\Downloads\foto_documents_after.jpg', Single_Blob) MyImage)
    where id_сотрудника = 1
    добавление фото в sql
    Ответ написан
    Комментировать