int no1=0,no2=0,no3=0;
SqlConnection conn=new SqlConnection(ConnDB.ConStr);
string sql="SELECT f_id,f_name,f_state,f_time FROM c_tststate";
SqlCommand myCommand=new SqlCommand(sql,conn);
conn.Open();
SqlDataReader sdr;
sdr=myCommand.ExecuteReader();
int cols=sdr.FieldCount;
listView1.BeginUpdate();
for(int i=0;i<cols;i++)
{
listView1.Columns.Add(sdr.GetName(i),130,HorizontalAlignment.Left);
}
string[] lvitem=new string[cols];
object[] values=new object[cols];
while(sdr.Read())
{
sdr.GetValues(values);
for(int i=0;i<values.Length;i++)
{
int state=Convert.ToInt32(sdr.GetValue(2));
if(state==0)
{
no1++;
lvitem[2]="未登录";
}
else if(state==1)
{
no2++;
lvitem[2]="答题中";
}
else if(state==2)
{
no3++;
lvitem[2]="已交卷";
}
textBox1.Text=Convert.ToString(no1/4);
textBox2.Text=Convert.ToString(no2/4);
textBox3.Text=Convert.ToString(no3/4);
lvitem[i]=values[i].ToString();
}
ListViewItem lvi=new ListViewItem(lvitem);
listView1.Items.Add(lvi);
}
listView1.EndUpdate();
sdr.Close();
conn.Close();