Question

This is my class Record which can be master/detail/details Records of teachers and their students

class Record
{

    public string RecorNo { get; set; }

    private List<Teacher> _Student = new List<Teacher>();

    public List<Teacher> Teacher
    {

        get { return _Teacher; }
        set { _Teacher=value; }

    }
    }


class Teacher
{
    private string _TeacherName;

    public Teacher(string teachername)
    {
        _TeacherName = teachername;
    }

    public string TeacherName
    {
        get { return _TeacherName; }
        set { _TeacherName = value; }
    }

    private List<Students> _students = new List<Students>();

    public List<Students> Students
    {
        get { return _students;}
        set { _students = value;}

    }


}


class Details
{


    private string _studentname;

    public Details(string studentsname)
    {

        _studentsname = studentsname;

    }

I have bind the Teacher/Student in 2 datagridview like so:

TeacherBindingSource.DataSource = record.Teacher;
StudentBindingSourcce.DataSource = TeacherBindingSource;
StudentBindingSourcce.DataMember = "Student";

dataGridView1.DataSource =TeacherBindingSource;
dataGridView2.DataSource =  StudentBindingSourcce;

Now Im lost how to save this record.
How can I save this to the database? I want to have a.

           record.Save(); method that will save the whole object.

I can iterate through the list and insert it one by one but the record may be existing already in the database so i had to know which record is added, updated or deleted?

Also how to make a method that will fill the teacher/student list like:

           records.LoadTeachers();
           records.Teacher("Smith").LoadStudents();
Était-ce utile?

La solution

you might want to have a look at ORM (object-relational mapping) libraries like Microsofts Entity Framework or (my favorite) NHibernate. Their sole reason to exist is to make persisting objects to a relational database easier. They can create new rows, can track object updates and relations between objects. You could create the logic needed to persist your objects on your own, but the established ORM libraries are matured, widely used and probably much better designed than anything a single developer could come up with in a reasonable amount of time.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top