Alex
Alex

Reputation: 49

Assign Sql Query Result To Variable

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

Answers (1)

Sudhakar Tillapudi
Sudhakar Tillapudi

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

Related Questions