日期:2014-05-18  浏览次数:20779 次

要统计20个字段值为1的行数,怎么写简单?
要20个字段,字段名分别为:1、2、3 ... 20,字段类型为 是/否 类型
我要统计表里,每个字段值为1(是)的行数,应该怎么写。不会我下面的过程需要做20次吧。。。 有没有什么简单的方法求值?

C# code

string sql_no = "select count(id) from red where 1 = 1";//头一个一位字段名,
string strpath = System.Windows.Forms.Application.StartupPath + @"..\记录.mdb";
string ole_conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strpath + ";User Id=admin;Password=;";
OleDbConnection conn = new OleDbConnection(ole_conn);
OleDbCommand red_cmd = new OleDbCommand(sql_red,conn);
conn.Open();
r_l1.Text =  red_cmd.ExecuteScalar().ToString();



------解决方案--------------------

这一句for(label1="1",lable1<=20,label1++)呵呵,打错了,
应写成
for(int label1=1,lable1<=20,label1++)
{?
? string sql_no = "select count(id) from red where "+label1.TOstring()+" = 1";
? r_l1.Text = red_cmd.ExecuteScalar().ToString();
}