用户操作
[留言]  [发消息]  [加为好友] 
订阅我的博客
XML聚合    FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
zengxie的公告
文章分类
    存档

    原创  一道机考题,和大家分享 收藏

    数据库有三张表,表一:学生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
     }
    }

     

    发表于 @ 2007年05月29日 01:46:00 | 评论( loading... ) | 编辑| 举报| 收藏

    旧一篇:一个古老的面试题给我们的思考 | 新一篇:猫叫,老鼠逃,人醒

    • 发表评论
    • 评论内容:
    •  
    Copyright © zengxie
    Powered by CSDN Blog