通过动态创建表来实现成绩存放

本文探讨了成绩存储的方法,选择了动态创建表的方式,以适应不同专业的成绩管理。作者放弃了查询现有表名来检查专业成绩是否存在,而是决定建立一个记录专业成绩表的新表,即使这需要重写部分代码。

经过一番思量,感觉成绩存放采用动态创建表更合理。本来想着通过查询DB中现有表名来确定某专业成绩是否存在,后来决定重新建一个只有一个字段(专业名)的表来记录已经有哪些专业的成绩表存在。

using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for DbOperation
/// </summary>

public class DbOperation
{
    
string Department;
    
double StuID;
    
string Course;
    
double Score;

    
public static string connString = ConfigurationManager.AppSettings["provider"].ToString()
            
+ HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["dbPath"])
            
+ ";";

public DbOperation()
{
}


    
public DbOperation(string department,double stuID,string course,double score)
    
{
        Department 
= department;
        StuID 
= stuID;
        Course 
= course;
        Score 
= score;
    }


  
    
public void InsertGradeOfDepartment()
    
{
        OleDbConnection Conn 
= new OleDbConnection(connString);
        
string sql = "select * from GradeAmount where Department='"+Department+"'";
        Conn.Open();
        OleDbCommand Cmd 
= new OleDbCommand(sql, Conn);
        OleDbDataReader dr 
= Cmd.ExecuteReader();
        
if (dr.Read())
        
{
            Conn.Close();
            InsertGrade();

        }

        
else
        
{
            Conn.Close();
            CreatGradeTable();
            InsertStu2GradeTable();
            SignDepartment();
            InsertGrade();
        }

    }


    
private void CreatGradeTable()
    
{
        
string CreatSql = "Create table " + Department
                        
+ " (StuID Number, StuName Text)";
        
string GetCourseSql = "select CourseName from Course where Department='" 
                        
+ Department + "'";

        OleDbConnection CreatConn 
= new OleDbConnection(connString);

        
try
        
{
            CreatConn.Open();
            OleDbCommand CreatCmd 
= new OleDbCommand(CreatSql, CreatConn);
            CreatCmd.ExecuteNonQuery();
            OleDbDataAdapter da 
= new OleDbDataAdapter(GetCourseSql, CreatConn);
            DataSet ds 
= new DataSet();
            da.Fill(ds, 
"Course");

            
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            
{
                
string temp = ds.Tables[0].Rows[i][0].ToString();
                temp 
= temp.Replace('.''_');
                
string AlterSql = "alter table " + Department + " add [" + temp + "] Number;";
                OleDbCommand AlterCmd 
= new OleDbCommand(AlterSql, CreatConn);
                AlterCmd.ExecuteNonQuery();
            }

        }

        
catch
        
{
        }

        
finally
        
{
            CreatConn.Close();
        }

    }


       
private void InsertStu2GradeTable()
    
{
        OleDbConnection Conn 
= new OleDbConnection(connString);
        Conn.Open();
        
string GetStuSql = "select StuID,StuName from StuInfo where Department='"+Department+"'";
        OleDbDataAdapter da 
= new OleDbDataAdapter(GetStuSql, Conn);
        DataSet ds 
= new DataSet();
        da.Fill(ds, 
"Stu");
        
for (int i=0; i < ds.Tables[0].Rows.Count; i++)
        
{
            
double stuId =(double) ds.Tables[0].Rows[i][0];
            
string stuName = ds.Tables[0].Rows[i][1].ToString();
            
string InsertSql = "Insert Into " + Department + " (StuID,StuName) Values("+stuId+",'"+stuName+"')";
            OleDbCommand InsCmd 
= new OleDbCommand(InsertSql, Conn);
            InsCmd.ExecuteNonQuery();
        }

        Conn.Close();
    }


    
private void SignDepartment()
    
{
        OleDbConnection Conn 
= new OleDbConnection(connString);
        Conn.Open();
        
string sql = "Insert into GradeAmount (Department) Values('"+Department+"')";
        OleDbCommand Cmd 
= new OleDbCommand(sql, Conn);
        Cmd.ExecuteNonQuery();
        Conn.Close();
    }


 
private void InsertGrade()
    
{
        
string InsertSql = "update "+Department+" set "+Course+"="+Score+" where StuID="+StuID;
        OleDbConnection Conn 
= new OleDbConnection(connString);
        
try
        
{
            Conn.Open();
            OleDbCommand InsertCmd 
= new OleDbCommand(InsertSql, Conn);
            InsertCmd.ExecuteNonQuery();
        }

        
catch
        
{
        }

        
finally
        
{
            Conn.Close();
        }

    }


}

//虽然这将导致很多代码重写,但是感觉无论是为了学习还是仅仅为了毕业设计都是值得的。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值