要求输出为如下:
姓名 语文 数学 英语 物理 化学 。。。总分
张三 60 60 60 60 60 。。。。SUM
李四
小明
。
。
。
我的代码:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace WebChat
{
/// <summary>
/// Score2 的摘要说明。
/// </summary>
public class Score2 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Table Table1;
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
TableCell td;
TableRow tr;
SqlConnection con=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["con"]);
con.Open();
SqlDataAdapter da=new SqlDataAdapter("select * from tbSubject",con);
DataSet ds1=new DataSet();
da.Fill(ds1,"subject");
tr=new TableRow();
td = new TableCell();
td.Controls.Add(new LiteralControl("姓名"));
tr.Cells.Add(td);
for(int k=0;k<ds1.Tables["subject"].Rows.Count;k++)
{
td=new TableCell();
td.Controls.Add(new LiteralControl(ds1.Tables["subject"].Rows[k]["Name"].ToString()));
tr.Cells.Add(td);
}
td = new TableCell();
td.Controls.Add(new LiteralControl("总分"));
tr.Cells.Add(td);
this.Table1.Rows.Add(tr);
SqlCommand sda=new SqlCommand("select count(*) from tbstudent",con);
int count=Convert.ToInt32(sda.ExecuteScalar());
for(int i=0;i< count;i++)
{
SqlDataAdapter sda1=new SqlDataAdapter("select stuName from tbstudent",con);
sda1.Fill(ds1,"name");
string strName=ds1.Tables["name"].Rows[i][0].ToString();
tr = new TableRow();
td = new TableCell();
td.Controls.Add(new LiteralControl(strName));
tr.Cells.Add(td);
int all=0;
for(int n=0;n<ds1.Tables["subject"].Rows.Count;n++)
{
int subj1=Convert.ToInt32(ds1.Tables["subject"].Rows[n]["ID"].ToString());
sda1=new SqlDataAdapter("select Score from tbScore where subID='"+subj1+"'",con);
string subj="sub"+n.ToString();
sda1.Fill(ds1,subj);
string subj2=ds1.Tables[subj].Rows[i][0].ToString();
all+=Convert.ToInt32(subj2);
td = new TableCell();
td.Controls.Add(new LiteralControl(subj2));
tr.Cells.Add(td);
}
string All=Convert.ToString(all);
td = new TableCell();
td.Controls.Add(new LiteralControl(All));
tr.Cells.Add(td);
this.Table1.Rows.Add(tr);
}
con.Close();
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}