C#實體類的關聯運用


定義學生類(student):

class student
{
public string stu_id { get; set; }
public string stu_name { get; set; }
public int stu_age { get; set; }
public string stu_pwd { get; set; }
public classes cls { get; set; }
public stu_cj cj { get; set; }
}

定義班級類classes:

class classes
{
public string cls_id { get; set; }
public string cls_name { get; set; }
}

定義學生成績類stu_cj:

class stu_cj
{
public string stu_id { get; set; }
public int cj_yw { get; set; }
public int cj_sx { get; set; }
public int cj_yy { get; set; }
}

 

在保存數據時:

student stud = new student();

stud.stu_name = dr["stu_name"].ToString();//直接保存


classes cls = new classes();                    //需要將其實例化
cls.cls_name = dr["cls_name"].ToString();// 保存
stud.cls = cls;         //在將其保存在學生類里面


stu_cj cj = new stu_cj();
cj.cj_yw = int.Parse(dr["yuwen"].ToString());
cj.cj_sx = int.Parse(dr["shuxue"].ToString());
cj.cj_yy = int.Parse(dr["yingyu"].ToString());
stud.cj = cj;

 

讀取的時候則可以直接讀取:

label1.Text = stu.stu_name;
label2.Text = stu.cls.cls_name;
label3.Text = stu.cj.cj_yw.ToString();
label4.Text = stu.cj.cj_sx.ToString();
label5.Text = stu.cj.cj_sx.ToString();

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM