Hello, I made the authorization of the user in the program through the database by login and password. I wanted to after the login. The data of the authorized user was output. Please help me figure out how to implement this. Here is the authorization code:

using (var con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\User\Desktop\база\Питание.mdf;Integrated Security=True;Connect Timeout=30")) { con.Open(); using (var cmd = new SqlCommand("Select Count (1) From Регистрация where Логин=@login and Пароль=@Pass", con)) { cmd.Parameters.AddWithValue("login", textBox1.Text); cmd.Parameters.AddWithValue("Pass", textBox2.Text); var accountsCount = (int)cmd.ExecuteScalar(); if (accountsCount==1) { Form5 a = new Form5(); a.label1.Text = accountsCount.ToString(); a.Show(); } else { MessageBox.Show("Неверные данные"); } } } 
  • textBox1.Text or some other information means? I also hope you understand that this “authorization” will not save you from a person who connects to the file directly (for example, through the management studio) and reads all logins and passwords. - default locale
  • I have a table in sql where there are columns with the name Name, Login, Password, Height, Weight and I need an authorized user to see his login after login. - Egor Pyshnov
  • Well, you wrote a request to the table Регистрация . What is the problem to write the same query to the table, say Пользователи ? Try to describe in more detail with what exactly the difficulties arose, so that respondents know how to concentrate their attention. - default locale
  • Well, from this table, the registration needs to display all the data of this registered user. For example, I have a user with login 123 and password 123 and he also has data such as gender and weight. successful login. On the following form, I can see the gender, height and weight of the user who entered under login 123 and password 123 - Egor Pyshnov

1 answer 1

As with the "authorization", with the help of SqlCommand perform a query to the table, putting down the list of the necessary columns:

 using (var cmd = new SqlCommand("Select пол, рост, вес From Регистрация where Логин=@login and Пароль=@Pass", con)) { 

To read the results, use the ExecuteReader method, which returns a SqlDataReader object. This object contains methods for reading a dataset.

The method documentation has an example of use:

 SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { Console.WriteLine(String.Format("{0}", reader[0])); } 

The columns can be accessed in order in the query ( reader[1] , reader[2] ) or by name ( reader["пол"], reader["рост"] , etc.). Experiment with querying and reading the results and choose a convenient way.

ZY It is worth noting that this “authorization” only makes sense for your application and does not protect against direct access to the database file. To protect data, install the server and configure the built-in authorization. And do not store passwords in clear text.

  • Thank you, everything turned out. All the best to you)) - Egor Pyshnov
  • @EgorPyshnov Please! If the problem is solved, you can mark the question as a solution: ru.stackoverflow.com/help/someone-answers - default locale