一道机考题,和大家分享

数据库有三张表,表一:学生ID,学生姓名。表二:课程ID,课程名称。表三:学生ID,课程ID,分数。

要求输出为如下:

姓名 语文 数学 英语 物理 化学 。。。总分

张三 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
 }
}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值