I have a MySql database and I want to assign sql query result to a variable. How can I do this?
My query:
sql = "SELECT rol FROM users WHERE user = '" + txtUser.Text + "'"
I have a MySql database and I want to assign sql query result to a variable. How can I do this?
My query:
sql = "SELECT rol FROM users WHERE user = '" + txtUser.Text + "'"
void AssignValue()
{
using(MySqlConnection con =new MySqlConnection("/*connection string here*/"))
using(MySqlCommand command = new MySqlCommand("SELECT rol FROM users WHERE
user = @user",con))
{
con.Open();
command.Parameters.AddWithValue("@user",txtUser.Text);
TextBox2.Text = commad.ExecuteScalar().ToString();
}
}
using statement. This ensures that it will be closed, even if an exception occurs.