从SQL Server查询中显示标签中的值

时间:2015-09-27 09:33:10

标签: c# sql sql-server visual-studio

我正在尝试从SQL查询中将值加载到标签:

sc.Open();
string type = Vehicle_cost.SelectedText;
string query = ("select Type from Vehicle_Registor where Reg_No = '" + type + "';");

SqlCommand cmd1 = new SqlCommand(query, sc);
SqlDataReader sdr = cmd1.ExecuteReader();
label23.Text = sdr.ToString();
sc.Close();

2 个答案:

答案 0 :(得分:1)

尝试以下代码..

sc.Open();

string query = @"select Type from Vehicle_Registor where Reg_No = @Reg_No";

SqlCommand cmd1 = new SqlCommand(query, sc);
cmd1.Parameters.AddWithValue("@Reg_No", Vehicle_cost.SelectedText);

SqlDataReader sdr = cmd1.ExecuteReader();
sdr.Read();

label23.Text = sdr[0].ToString();

sc.Close();

答案 1 :(得分:0)

尝试这样的事情:

  do {
    // do your stuff here
  }while(error)