Student Management System---three-tier architecture

Source: Internet
Author: User


Let's take a look at the form frame (reference order)

Now let's look at the model layer

Let's take the example of who wrote the code in the model inside the Studeng.cs class (the data is written according to the data in the library table).

public class Student    {
Number private int studentno; public int Studentno { get {return studentno;} set {Studentno = value;} } Password private string loginpwd; public string loginpwd { get {return loginpwd;} set {loginpwd = value;} } ...}

Then let's look at the DAL layer (studentdal)


public class Gradedal    {       //01. Retrieves all the grade name collections, returning a generic collection list<grade> public        list<grade> Getallgradelist ()        {            String sql = "SELECT * from grade";            1.1 Strong SQL turns into a table in memory            DataTable dt=sqlhelper.executedatatable (SQL);            Mytool tool=new Mytool ();            DT to list            list<grade> list = tool. datatabletolist<grade> (DT);            return list;        }    }

MyTool.cs (Custom tool Class)

 public class Mytool {//<summary>//datasettolist//</summary>/<t Ypeparam name= "T" > Conversion type </typeparam>//<param name= "DataSet" > Data source </param>///<param Name= "Tableindex" > Indexes to convert tables </param>//<returns></returns> public list<t> Datatab            Letolist<t> (DataTable DT) {//Confirm parameter valid if (dt = = NULL) return null;            list<t> list = new list<t> (); for (int i = 0; i < dt. Rows.Count;                i++) {//Create generic object T _t = activator.createinstance<t> (); Gets all properties of the object propertyinfo[] PropertyInfo = _t.gettype ().                GetProperties (); for (int j = 0; j < dt. Columns.count;                        J + +) {foreach (PropertyInfo info in PropertyInfo) {              Property name and column name are both assigned values          if (dt. COLUMNS[J]. Columnname.toupper (). Equals (info. Name.toupper ())) {if (dt). ROWS[I][J]! = DBNull.Value) {info. SetValue (_t, dt.                            ROWS[I][J], NULL); } else {info.                            SetValue (_t, NULL, NULL);                        } break; }}} list.            ADD (_t);        } return list; }    }


public class Resultdal {public list<resultext> Getallresult () {String sql = "Select St Udentname,subjectname,studentresult,examdate from Student,subject,result where Student.studentno=result.studentno            and Result.subjectid=subject.subjectid ";            DataTable dt=sqlhelper.executedatatable (SQL);            Mytool tool=new Mytool (); list<resultext> list = tool.            datatabletolist<resultext> (DT);        return list; } public list<resultext> getresultbyparameter (int subjectid,string stuname) {String sql = "Select Studentname,subjectname,studentresult,examdate from Student,subject,result where student.studentno=            Result.studentno and Result.subjectid=subject.subjectid ";            if (subjectid!=-1) {SQL + = "and [email protected]"; } if (!string. IsNullOrEmpty (stuname))//null "" {SQL + = "and studentname like @namE "; } sqlparameter[] para = {new SqlParameter ("@subid", Subjectid), new S            Qlparameter ("@name", '% ' +stuname+ '% ')};            DataTable dt = sqlhelper.executedatatable (Sql,para);            Mytool tool = new Mytool (); list<resultext> list = tool.            datatabletolist<resultext> (DT);        return list; }    }


public static class SQLHelper {public static string constr = configurationmanager.connectionstrings["Constr"].      ConnectionString;      public static int id;      <summary>///execute Nonquery command///</summary>//<param name= "Cmdtxt" ></param> <param name= "Parames" ></param>///<returns></returns> public static int Executeno Nquery (String cmdtxt, params sqlparameter[] parames) {return ExecuteNonQuery (cmdtxt, CommandType.Text, para      MES); }//You can use the stored procedure's ExecuteNonQuery public static int ExecuteNonQuery (string cmdtxt, CommandType cmdtype, params sqlpar Ameter[] parames) {//Determines if the script is empty and returns 0 if (string) directly.          IsNullOrEmpty (Cmdtxt)) {return 0; } using (SqlConnection con = new SqlConnection (constr)) {using (SqlCommand cmd = new Sqlco Mmand (Cmdtxt, con)) {if (parames! = null) {cmd.commandtype = Cmdtype; Cmd.                  Parameters.addrange (Parames); } con.                  Open (); return CMD.              ExecuteNonQuery ();      }}} public static SqlDataReader Executedatareader (string cmdtxt, params sqlparameter[] parames)      {return Executedatareader (Cmdtxt, CommandType.Text, parames);  }//SqlDataReader stored procedure method public static SqlDataReader Executedatareader (String cmdtxt, CommandType cmdtype, params Sqlparameter[] parames) {if (string.          IsNullOrEmpty (Cmdtxt)) {return null;          } SqlConnection con = new SqlConnection (CONSTR);              using (SqlCommand cmd = new SqlCommand (Cmdtxt, con)) {cmd.commandtype = Cmdtype; if (parames! = null) {cmd.              Parameters.addrange (Parames); } con. Open (); Add the reader's behavior. When reader releases the resource, Con is also closed with return CMD.          ExecuteReader (System.Data.CommandBehavior.CloseConnection);      }}//params mutable parameter public static DataTable executedatatable (String sql, params sqlparameter[] parames)      {return executedatatable (SQL, CommandType.Text, parames); }//Call the class of the stored procedure, about (executedatatable) public static DataTable executedatatable (String sql, CommandType Cmdtype, para Ms Sqlparameter[] parames) {if (string.          IsNullOrEmpty (SQL)) {return null;          } datatable dt = new DataTable (); using (SqlDataAdapter da = new SqlDataAdapter (SQL, constr)) {da.              Selectcommand.commandtype = Cmdtype; if (parames! = null) {da.              SelectCommand.Parameters.AddRange (Parames); } da.              Fill (DT);          return DT; }}///<summary>//ExeCutescalar//</summary>//<param name= "Cmdtxt" > first parameter, SQL Server statement </param>///<para M name= "Parames" > second parameter, pass 0 or more parameters </param>//<returns></returns> public static object Exec Utescalar (String cmdtxt, params sqlparameter[] parames) {return ExecuteScalar (cmdtxt, CommandType.Text, par      Ames); }//can use stored procedure executescalar public static object ExecuteScalar (String cmdtxt, CommandType cmdtype, params Sqlparam Eter[] parames) {if (string.          IsNullOrEmpty (Cmdtxt)) {return null; } using (SqlConnection con = new SqlConnection (constr)) {using (SqlCommand cmd = new Sqlco                  Mmand (Cmdtxt, con)) {cmd.commandtype = Cmdtype; if (parames! = null) {cmd.                  Parameters.addrange (Parames); } con.                Open (); return cmd.              ExecuteScalar (); }}}//Call the DBHelper class of the stored procedure (about Execeutscalar, contains transactions, can only handle int type, return error number) public static object Executescala R (String cmdtxt, CommandType cmdtype,sqltransaction Sqltran, params sqlparameter[] parames) {if (string.          IsNullOrEmpty (Cmdtxt)) {return 0;              } using (SqlConnection con = new SqlConnection (constr)) {int sum = 0; using (SqlCommand cmd = new SqlCommand (Cmdtxt, con)) {cmd.                  Commandtype=cmdtype; if (parames! = null) {cmd.                  Parameters.addrange (Parames); } con.                  Open (); Sqltran = con.                  BeginTransaction (); try {cmd.                      Transaction = Sqltran; Sum=convert.toint32 (cmd.                      ExecuteScalar ()); Sqltran.                  Commit ();                }  catch (SqlException ex) {Sqltran.                  Rollback ();              } return sum; }          }      }    }


 public class Studentdal {public list<student> getallstudentlist () {String sql =            "SELECT * from student";             DataTable dt = sqlhelper.executedatatable (SQL);            Mytool tool=new Mytool (); list<student> list = tool.                        datatabletolist<student> (DT);                   return list;            } public DataTable Getallstudent () {String sql = ' SELECT * from student ';            DataTable dt = sqlhelper.executedatatable (SQL);        return DT;            } public bool IsLogin (Student stu) {bool flag = false; String sql = "SELECT COUNT (1) from student where studentno= '" +stu. Sname+ "' and loginpwd= '" +stu.            Loginpwd+ "'";            int Count=convert.toint32 (sqlhelper.executescalar (SQL));            if (count>0) {flag = true;        } return flag; }    }


public class Subjectdal    {       //01. Write a method that retrieves the collection of all subjects under that grade according to the grade number public       list<subject> Getallsubjectbygradeid (int id)       {           String sql = "SELECT * from subject where [email protected]";           SqlParameter para = new SqlParameter ("@id", id);           DataTable dt=sqlhelper.executedatatable (SQL, para);           Mytool tool=new Mytool ();           list<subject> list = tool. datatabletolist<subject> (DT);           return list;       }    }

We're looking at the BLL layer (STUDENTBLL) The BLL layer is used to connect the UI layer and the DAL layer

Here we simply take a look at the code inside the StudentBLL.cs class

public class STUDENTBLL    {public       list<student> getallstudent ()       {           list<student> list=dal . Getallstudentlist ();           DataTable dt= dal. Getallstudent ();           return list;       }       Studentdal dal=new Studentdal ();             public bool IsLogin (Student stu)       {          bool result= dal. IsLogin (Stu);           return result;       }           }

Finally, the complete code is filled into the UI layer.

Login Form


       STUDENTBLL BLL = new Studentbll ();       OK button        private void Btnlogin_click (object sender, EventArgs e)        {            //1.1 a Student object, and assign a value            Student Stu=new Student ();            Stu. SName = txtName.Text;            Stu. Loginpwd = Txtpwd.text;            1.2 Call the corresponding login method of the BLL layer, and pass in the student object            BOOL flag = BLL. IsLogin (Stu);            if (flag)            {                MessageBox.Show ("Success");            }        }

Login type

Query form

                GRADEBLL gradebll=new GRADEBLL ();        SUBJECTBLL subbll=new SUBJECTBLL ();        RESULTBLL resultbll=new RESULTBLL ();        private bool Flag; Load Event private void Frmselectresultm1_load (object sender, EventArgs e) {dgvresult.autogenerate            Columns = false;            Loadgrade ();            list<resultext> list = Resultbll.getallresult ();        Dgvresult.datasource = list;            } public void Loadgrade () {cbosubject.dropdownheight = 106;            01. To the Grade drop-down box binding data list<grade> List = Gradebll.getallgradelist ();            Two lines of code () Cbograde.valuemember = "Gradeid";            Cbograde.displaymember = "Gradename";            Cbograde.datasource = list;        Flag = true;                            }//Grade drop-down box the event that the selected item changed private void Cbograde_selectedindexchanged (object sender, EventArgs e) { 1.1 First gets the grade number corresponding to the selected grade name//How to get the hidden value of the drop-down box  () int selectid = Convert.ToInt32 (Cbograde.selectedvalue); Code a little, debug a little//1.2 involves the BLL, invokes the method, passes in the ID, gets list<subject> list<subjec                t> list = Subbll.getallsubjectbygradeid (Selectid);                Subject sub=new Subject (); Sub.                Subjectid =-1; Sub.                Subjectname = "Please select"; List.                Insert (0,sub);                Cbosubject.datasource = list;                Two lines of code () Cbosubject.valuemember = "Subjectid";        Cbosubject.displaymember = "Subjectname"; }//query private void btnSearch_Click (object sender, EventArgs e) {//drop-down box hidden value int s            Ubid = Convert.ToInt32 (Cbosubject.selectedvalue);            The value of the text box string name = txtName.Text;            list<resultext> list = Resultbll.getresultbyparameter (subid, name);        Dgvresult.datasource = list; }

Show All Information Form

STUDENTBLL bll=new STUDENTBLL ();        private void Frmstulist_load (object sender, EventArgs e)        {         list<student> list=   BLL. Getallstudent ();            Dgvlist.datasource = list;        }

Student Management System---three-tier architecture

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.