我使用c#asp.net編寫了一個簡單的應用程序。我得到了列的平均值。如何從結果中獲取單個值並將其顯示在新標籤中(label1
,label2
,label3
....)?在asp.net中顯示來自sql結果的單個列c#
我試過ExecuteScalar().ToString();
但它只返回第一列。
下面是我的代碼:
SqlConnection con;
con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\STATDB.MDF;Integrated Security=True;User Instance=True");
SqlCommand com = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
string result = "SELECT AVG(p_tan) AS p_tang, AVG(e_tan) AS e_tang, AVG(p_rel) AS p_reli FROM statistics";
SqlCommand showresult = new SqlCommand(result, con);
con.Open();
Label1.Text = showresult.ExecuteScalar().ToString();
//Label2.Text = p_tang
//Label3.Text = e_tang
//Label4.Text = p_reli
con.Close();
任何幫助將不勝感激。
請閱讀[SqlCommand.ExecuteReader](http://msdn.microsoft.com/en-us/library/9kcbe65k.aspx) –