从SQL Server查询中显示标签中的值
本文关键字:标签 显示 Server 查询 SQL | 更新日期: 2023-09-27 18:14:37
我正试图从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();
试试下面的代码…
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();
试试这样:
SqlCommand cmd1 = new SqlCommand(query, sc);
using (SqlDataReader sdr = cmd1.ExecuteReader())
{
while (sdr.Read())
{
for (int i = 0; i < sdr.FieldCount; i++)
{
label23.text += sdr[i].ToString();
}
}
}
sc.Close();