F
F
FFFFFFFFFFFFFF2020-05-19 19:53:52
MySQL
FFFFFFFFFFFFFF, 2020-05-19 19:53:52

Authorization c# mysql., separation of users!?

C# WinForms Mysql, made authorization via login/password (pre-written in the database, without registration). How to make it so that, for example, the user who entered the login / password Admin / Admin opened one window, and the user who entered user / user opened another window.
I don't really understand how this can be done...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikita Verdinsky, 2020-05-19
@Nie_yar

Imagine that you have a user
table . This table has the following columns :

  • ID
  • Name
  • Password
  • User_Role

Then you hang a click event on the login button and write the following code in it:
string connStr = "server=localhost; port=3306; username=root; password= root; database=bd;";
string sql = "SELECT * FROM `user` WHERE `Name` = @un and  `Password`= @up";

MySqlConnection conn = new MySqlConnection(connStr);
conn.Open();

DataTable table = new DataTable();
MySqlDataAdapter adapter = new MySqlDataAdapter();

MySqlCommand command = new MySqlCommand(sql, conn);
command.Parameters.Add("@un", MySqlDbType.VarChar, 25);
command.Parameters.Add("@up", MySqlDbType.VarChar, 25);

command.Parameters["@un"].Value = TextBox1.Text;
command.Parameters["@up"].Value = TextBox2.Text

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

if (table.Rows.Count > 0) 
{ 
      userRole(); // метод, который будет открывать разные формы в зависимости от пользователя
}

conn.Close();

Next we create a new method which we call userRole
string UserName = TextBox1.Text;;

string connStr = "server=localhost; port=3306; username=root; password= root; database=bd;";
string sql = "SELECT User_Role FROM `user` WHERE `Name` = @un";

MySqlConnection conn = new MySqlConnection(connStr);
conn.Open();

MySqlParameter nameParam = new MySqlParameter("@un", UserName);

 MySqlCommand command = new MySqlCommand(sql, conn);
 command.Parameters.Add(nameParam);

string Form_Role = command.ExecuteScalar().ToString();

Switch(Form_Role)
{
case "Администратор": Form.ActiveForm.Close(); Form1 f1 = new Form1(); f1.Show(); break;
default:  Form.ActiveForm.Close(); Form2 f2 = new Form2(); f2.Show();
}
conn.Close();

Please note that the username must be individual .
I haven't tested the code, but it should work. As a last resort, you must understand in which direction to move.
If more experienced colleagues have found some kind of flaw, then correct me.
P.S. I hope that helped.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question