我有一个MySql数据库,我想将sql查询结果分配给变量。我怎么能这样做?
我的查询:
sql = "SELECT rol FROM users WHERE user = '" + txtUser.Text + "'"
答案 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();
}
}