【发布时间】:2014-03-28 10:58:52
【问题描述】:
我有一个问题。 WinForm如何在标签中显示数据库中的数据?
private void button1_Click(object sender, EventArgs e)
{
string connectionString = @"Data Source=.\SQLEXPRESS; AttachDbFilename=C:\Users\John\Documents\Visual Studio 2010\Projects\Shop\Shop\shop.mdf; Integrated Security=True;Connect Timeout=30;User Instance=True";
SqlConnection myDbconnection = new SqlConnection(connectionString);
myDbconnection.Open();
sqlQRY = "Select prix from fleurs where nom='"+flori.SelectedValue+"'";
SqlCommand cmd = new SqlCommand(sqlQRY, myDbconnection);
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
if (reader.HasRows)
{
label6.Text = 'prix'.ToString();
}
else
{
MessageBox.Show("Error");
}
}
我需要在label6.Text 中显示“prix”。
【问题讨论】:
-
你有什么错误吗?
-
它显示的正是你告诉它的内容。
-
他的意思是他想要数据库中'prix'列的值。如果一行中只有一个值,@Menfis ExecuteScalar 是正确的方法 - 请参阅我的答案