Reputation: 49
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 + "'"
Upvotes: 0
Views: 2903
Reputation: 26209
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();
}
}
Upvotes: 2