SQL求和并返回结果赋值给控件~~~~求解~~
SELECT SUM(OrderPrice) AS OrderTotal FROM table
C#中~~~~想把返回的结果赋值给label2控件~~~~但~~在C#找不到返回的结果~~所以~~不知道怎么写
------解决方案--------------------
//属性:数据库链接对象
private static SqlConnection conn;
public static SqlConnection Conn
{
get
{
try
{
string connstr = "server=.;database=Driver;User ID=sa;Password=123456";
if (conn == null)
conn = new SqlConnection(connstr);
if (conn.State == ConnectionState.Closed)
conn.Open();
if (conn.State == ConnectionState.Broken)
{
conn.Close();
conn.Open();
}
return conn;
}
catch (Exception ex)
{
throw;
}
}
}
//返回首行首列
public static object GetScalar(string SqlStr)
{
SqlCommand cmd = new SqlCommand(SqlStr, Conn);
object obj = cmd.ExecuteScalar();
conn.Close();
return obj;
}
string sql="SELECT SUM(OrderPrice) AS OrderTotal FROM table";
lable2.Text=GetScalar(sql).tostring();