将Sql查询结果分配给变量

时间:2014-03-15 19:43:53

标签: c# mysql sql variables

我有一个MySql数据库,我想将sql查询结果分配给变量。我怎么能这样做?

我的查询:

sql = "SELECT rol FROM users WHERE user = '" + txtUser.Text + "'"

1 个答案:

答案 0 :(得分:2)

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();        
    }
}