2015-04-01 63 views
0

我完成了我的學生安排班級把它在,我的教授,回到我的工作宣稱「計劃是一個list類,不只是studentid和CRN」列出上課時間學生

他是什麼意思呢?我如何解決它?

using System; 
using System.Collections.Generic; 
using System.Text; 

namespace Schedule 
{ 
    class Schedule 
    { 
     private int studentID; 
    private int cRN; 



    public Schedule() { 
     this.studentID = 0; 
     this.cRN = 0; 
    } 

     //++++++++++++++++ DATABASE Data Elements +++++++++++++++++ 
     public System.Data.OleDb.OleDbDataAdapter OleDbDataAdapter; 
     public System.Data.OleDb.OleDbCommand OleDbSelectCommand; 
     public System.Data.OleDb.OleDbCommand OleDbInsertCommand; 
     public System.Data.OleDb.OleDbCommand OleDbUpdateCommand; 
     public System.Data.OleDb.OleDbCommand OleDbDeleteCommand; 
     public System.Data.OleDb.OleDbConnection OleDbConnection; 
     public string cmd; 

     public void DBSetup(){ 
     // +++++++++++++++++++++++++++ DBSetup function +++++++++++++++++++++++++++ 
     // This DBSetup() method instantiates all the DB objects needed to access a DB, 
     // including OleDbDataAdapter, which contains 4 other objects(OlsDbSelectCommand, 
     // oleDbInsertCommand, oleDbUpdateCommand, oleDbDeleteCommand.) And each 
     // Command object contains a Connection object and an SQL string object. 
      OleDbDataAdapter = new System.Data.OleDb.OleDbDataAdapter(); 
      OleDbSelectCommand = new System.Data.OleDb.OleDbCommand(); 
      OleDbInsertCommand = new System.Data.OleDb.OleDbCommand(); 
      OleDbUpdateCommand = new System.Data.OleDb.OleDbCommand(); 
      OleDbDeleteCommand = new System.Data.OleDb.OleDbCommand(); 
      OleDbConnection = new System.Data.OleDb.OleDbConnection(); 


      OleDbDataAdapter.DeleteCommand = OleDbDeleteCommand; 
      OleDbDataAdapter.InsertCommand = OleDbInsertCommand; 
      OleDbDataAdapter.SelectCommand = OleDbSelectCommand; 
      OleDbDataAdapter.UpdateCommand = OleDbUpdateCommand; 


OleDbConnection.ConnectionString = "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Reg"+ 
"istry Path=;Jet OLEDB:Database L" + 
"ocking Mode=1;Data Source=C:\Users\Tina\Desktop\RegistrationMDB.accdb;J" + 
"et OLEDB:Engine Type=5;Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:System datab" + 
"ase=;Jet OLEDB:SFP=False;persist security info=False;Extended Properties=;Mode=S" + 
"hare Deny None;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Create System Database=False;Jet " + 
"OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repai" + 
"r=False;User ID=Admin;Jet OLEDB:Global Bulk Transactions=1"; 

     } 



     public void SelectDB(int studentID) 
     { //++++++++++++++++++++++++++ SELECT +++++++++++++++++++++++++ 
      DBSetup(); 
      cmd = "Select * from Courses where StudentID = " + studentID; 
      OleDbDataAdapter.SelectCommand.CommandText = cmd; 
      OleDbDataAdapter.SelectCommand.Connection = OleDbConnection; 
      Console.WriteLine(cmd); 
      try { 
        OleDbConnection.Open(); 
        System.Data.OleDb.OleDbDataReader dr; 
        dr = OleDbDataAdapter.SelectCommand.ExecuteReader(); 

        dr.Read(); 


       setStudentID(Int32.Parse(dr.GetValue(1)+"")); 
        setCRN(Int32.Parse(dr.GetValue(1)+"")); 

      } 
      catch (Exception ex) 
      { 
       Console.WriteLine(ex); 
      } 
      finally 
      { 
       OleDbConnection.Close(); 
      }      
     } 

     public void InsertDB() { 
     // +++++++++++++++++++++++++++ INSERT +++++++++++++++++++++++++++++++ 

      DBSetup(); 
      cmd = "INSERT into StudentSchedule values(" + getStudentID() + "," + 
          "'" + getCRN() + ")"; 

      OleDbDataAdapter.InsertCommand.CommandText = cmd; 
      OleDbDataAdapter.InsertCommand.Connection = OleDbConnection; 
      Console.WriteLine(cmd); 
      try 
      { 
       OleDbConnection.Open(); 
       int n = OleDbDataAdapter.InsertCommand.ExecuteNonQuery(); 
       if (n==1) 
        Console.WriteLine("Data Inserted"); 
       else 
        Console.WriteLine("ERROR: Inserting Data"); 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine(ex); 
      } 
      finally 
      { 
       OleDbConnection.Close(); 
      }     
     } 

     public void updateDB() 
     { 
      //++++++++++++++++++++++++++ UPDATE +++++++++++++++++++++++++ 

      cmd = "Update StudentSchedule set StudentID = '" + getStudentID() + "'," + 
         "CRN = '" + getCRN(); 

      OleDbDataAdapter.UpdateCommand.CommandText = cmd; 
      OleDbDataAdapter.UpdateCommand.Connection = OleDbConnection; 
      Console.WriteLine(cmd); 
      try 
      { 
       OleDbConnection.Open(); 
       int n = OleDbDataAdapter.UpdateCommand.ExecuteNonQuery(); 
       if (n==1) 
        Console.WriteLine("Data Updated"); 
       else 
        Console.WriteLine("ERROR: Updating Data"); 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine(ex); 
      } 
      finally 
      { 
       OleDbConnection.Close(); 
      }      
     } 
     public void deleteDB() 
     { 
      //++++++++++++++++++++++++++ DELETE +++++++++++++++++++++++++ 

      cmd = "Delete from StudentSchedule where StudentID = " + getStudentID(); 
      OleDbDataAdapter.DeleteCommand.CommandText = cmd; 
      OleDbDataAdapter.DeleteCommand.Connection = OleDbConnection; 
      Console.WriteLine(cmd); 
      try 
      { 
       OleDbConnection.Open(); 
       int n = OleDbDataAdapter.DeleteCommand.ExecuteNonQuery(); 
       if (n==1) 
        Console.WriteLine("Data Deleted"); 
       else 
        Console.WriteLine("ERROR: Deleting Data"); 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine(ex); 
      } 
      finally 
      { 
       OleDbConnection.Close(); 
      }      
     } 

     public void setStudentID(int studentID) { 
     this.studentID = studentID; 
    } 
     public void setCRN(int cRN) { 
     this.cRN = cRN; 
    } 
     public int getStudentID() { 
     return studentID; 
    } 
     public int getCRN() { 
     return cRN; 
    } 

     public void display(){ 
     System.Console.WriteLine("Student ID = "+ getStudentID()); 
     System.Console.WriteLine("CRN = "+ getCRN()); 



    } 
    } 
} 

http://puu.sh/gYbR3/182e754705.png
這裏是學生日程

enter image description here
這是段。

回答

0

你應該問問你的教授,但我會說你應該有一個Student類,Schedule將有一個屬性是List<Student>。你也不應該/不應該公開所有這些OleDb*

0

您應該先考慮一下時間表 - 事件列表。如果你看看你的SelectDB,你只能存儲一個學生和一門課程。但是,如果您查看您的select語句Select * from Courses WHERE StudentId = whatever,則可能會從查詢中返回多行。

你應該有什麼是你的日程安排類定義一個List<Courses>和你需要的任何屬性。

public class Schedule 
{ 
    private int studentID; 

    private List<Course> courseList; 
} 

和課程目標可能類似於:

public class Course 
{ 
    public int courseId; 

    public int studentId; 

    public int name; 
} 

這應該讓你在正確的方向前進。