Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Lab5.Properties;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Data.SqlClient;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace Lab5 {
- public partial class MainForm : Form {
- public bool AdminRole { get; set; }
- public static int UserId { get; set; }
- public void showForm(Form formClass) {
- this.Hide();
- var newForm = formClass;
- newForm.Closed += (s, args) => this.Show();
- newForm.Show();
- }
- public MainForm() {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e) {
- string uLogin = textBox1.Text;
- string uPassword = textBox2.Text;
- using (SqlConnection sqlConnect = new SqlConnection(Settings.Default.LicenseKeysConnectionString)) { // настройка строки подключени
- try {
- sqlConnect.Open(); // открываю подключение
- SqlCommand getLogin = new SqlCommand("SELECT TOP 1 Login FROM User_tab WHERE Login = '" + uLogin + "'", sqlConnect);
- if ((string)getLogin.ExecuteScalar() == null) {
- MessageBox.Show("Нет пользователя с указанным логином.", "Ошибка авторизации");
- } else {
- SqlCommand checkPassword = new SqlCommand("SELECT TOP 1 Login FROM User_tab WHERE Login = '" + uLogin + "' AND Password = '" + uPassword + "'", sqlConnect);
- if ((string)checkPassword.ExecuteScalar() == null) {
- MessageBox.Show("Неверный пароль.", "Ошибка авторизации");
- } else {
- SqlCommand getRole = new SqlCommand("SELECT TOP 1 AdminRole FROM User_tab WHERE Login = '" + uLogin + "' AND Password = '" + uPassword + "'", sqlConnect);
- SqlCommand getUserId = new SqlCommand("SELECT TOP 1 UserId FROM User_tab WHERE Login = '" + uLogin + "' AND Password = '" + uPassword + "'", sqlConnect);
- AdminRole = (bool)getRole.ExecuteScalar();
- UserId = (Int32)getUserId.ExecuteScalar();
- if (AdminRole) {
- showForm(new ManagerForm());
- } else {
- showForm(new WorkerForm());
- }
- }
- }
- } catch (SqlException ex) {
- MessageBox.Show("Ошибка подключения к БД.", "Ошибка");
- }
- }
- }
- }
- public abstract class Users {
- protected int UserId;
- protected bool AdminRole;
- public string getLogin(int userId) {
- string Login = "";
- using (SqlConnection sqlConnect = new SqlConnection(Settings.Default.LicenseKeysConnectionString)) { // настройка строки подключени
- try {
- sqlConnect.Open(); // открываю подключение
- SqlCommand getLogin = new SqlCommand("SELECT TOP 1 Login FROM User_tab WHERE UserId = " + userId, sqlConnect);
- Login = (string)getLogin.ExecuteScalar();
- } catch (SqlException ex) {
- MessageBox.Show("Ошибка подключения к БД.", "Ошибка");
- }
- }
- return Login;
- }
- abstract protected string getKey();
- }
- public interface IUsers {
- string getLogin(int userId);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement