合并语句
请问如何合并以下的sql语句为一条语句以提高执行效率?
private string Count_Str = "select count(*) from Test ";
private string Max_Str = " Select max(test) from Test";
private string Avg_Str = " Select avg(test) from Test";
SqlCommand Count_Cmd = new SqlCommand(Count_Str, Conn);
SqlCommand Max_Cmd = new SqlCommand();
Count = (int)Count_Cmd.ExecuteScalar();//count是数据表中总的记录条数
SqlCommand MAX_Cmd = new SqlCommand(Max_Str, Conn);
Max =Convert.ToDouble( MAX_Cmd.ExecuteScalar());
SqlCommand Avg_Cmd = new SqlCommand(Avg_Str, Conn);
Avg =Convert.ToDouble(Avg_Cmd.ExecuteScalar());
------解决方案--------------------聚合函数可以一起用的
private string Count_Str = "select count(*) as a, max(test) as b,avg(test) as c from Test ";
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)
http://feiyun0112.cnblogs.com/
------解决方案--------------------select count(*),max(test),avg(test) from Test
连上就行
------解决方案--------------------select count(*)as count_str,max(text)as max_str,avg(test)as avg_str fro test group by test
试一下,我也没验证
------解决方案--------------------也可以用dataset
C# code
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "";//数据库连接语句;
conn.Open();
string sql = "select count(*),max(test),avg(test) from test";
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds, "mytable");
int count = Convert.ToInt32(ds.Tables["mytable"].Rows[0][0]);
int max = Convert.ToInt32(ds.Tables["mytable"].Rows[0][1]);
int avg = Convert.ToInt32(ds.Tables["mytable"].Rows[0][2]);
conn.Close();//用datareader自己写吧;