Dodawanie wielu obiektów do Obiektowej Bazy Danych

0

Witam serdecznie.

Piszę aplikację, która ma dodawać studentów do Obiektowej Bazy Danych. ( db4objects .NET )

Mam 3 tabele ( 3 klasy )
Student / Adres / Telefon
Chciałbym aby student mógł posiadać wiele telefonów a numer indeksu był zawsze unikalny.

user image

Do tej pory udało mi się wydłubać coś takiego:

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Db4objects;
using Db4objects.Db4o;

namespace Program_1
{
    public partial class Form1 : Form
    {
        public List<Student> ListaStudentow = new List<Student>();
        public List<Adres> ListaAdresow = new List<Adres>();
        public List<Telefon> ListaTelefonow = new List<Telefon>();
        
        public Form1()
        {
            InitializeComponent();
        }

        private void btnDodajStudenta_Click(object sender, EventArgs e)
        {
            using (IObjectContainer db = Db4oEmbedded.OpenFile("BazaStudentow.yap"))
            {
                Student nowy = new Student();
                Telefon tel = new Telefon();
                Adres adr = new Adres();

                nowy.Imie = tbxImie.Text;
                nowy.Nazwisko = tbxNazwisko.Text;
                nowy.Numer = tbxNumerIndeksu.Text;

                adr.Miasto = tbxMiasto.Text;
                adr.Ulica = tbxUlica.Text;
                adr.KodPocztowy = tbxKodPocztowy.Text;

                tel.Numer = tbxTelefon.Text;
                tel.Operator = tbxOperator.Text;

                db.Store(nowy);
                db.Store(adr);
                db.Store(tel);

                db.Commit();

                IObjectSet result1 = db.QueryByExample(nowy);
                IObjectSet result2 = db.QueryByExample(adr);
                IObjectSet result3 = db.QueryByExample(tel);

                ListResult(result1,result2,result3);

            }
        }

        public void ListResult(IObjectSet result1, IObjectSet result2, IObjectSet result3)
        {
            int totalResult = result1.Count + result2.Count + result3.Count;
            tbxIloscStudentow.Text = "Aktualnie "+totalResult.ToString()+" studentów";
            foreach (Student item in result1)
            {
                ListaStudentow.Add(item);
            }
            foreach (Adres item in result2)
            {
                ListaAdresow.Add(item);
            }
            foreach (Telefon item in result3)
            {
                ListaTelefonow.Add(item);
            }


            DGV.DataSource = ListaStudentow.ToList() + " " + ListaAdresow.ToList() + " " + ListaTelefonow.ToList();
        }
    }

    public class Student
    {
        string _imie;
        string _nazwisko;
        string _numer;
        string _adres;
        List<Student> _telefony;

        public Student()
        {

        }
        public Student(string imie, string nazwisko, string numer, string adres, List<Student> telefony)
        {
            this._imie = imie;
            this._nazwisko = nazwisko;
            this._numer = numer;
            this._adres = adres;
            this._telefony = telefony;
        }

        public string Imie
        {
            get
            {
                return _imie;
            }
            set
            {
                _imie = value;
            }
        }

        public string Nazwisko
        {
            get
            {
                return _nazwisko;
            }
            set
            {
                _nazwisko = value;
            }
        }

        public string Numer
        {
            get
            {
                return _numer;
            }
            set
            {
                _numer = value;
            }
        }

        public string Adres
        {
            get
            {
                return _adres;
            }
            set
            {
                _adres = value;
            }
        }

        public List<Student> Telefony
        {
            get
            {
                return _telefony;
            }
            set
            {
                _telefony = value;
            }
        }

        public override string ToString()
        {
            return string.Format("{0}/{1}/{2}/{3}/{4}", _imie, _nazwisko, _numer, _adres, _telefony);
        }
    }

    public class Adres
    {
        string _ulica;
        string _kodPocztowy;
        string _miasto;

        public Adres()
        {

        }
        public Adres(string ulica, string kodPocztowy, string miasto)
        {
            this._ulica = ulica;
            this._kodPocztowy = kodPocztowy;
            this._miasto = miasto;
        }

        public string Ulica
        {
            get
            {
                return _ulica;
            }
            set
            {
                _ulica = value;
            }
        }

        public string KodPocztowy
        {
            get
            {
                return _kodPocztowy;
            }
            set
            {
                _kodPocztowy = value;
            }
        }

        public string Miasto
        {
            get
            {
                return _miasto;
            }
            set
            {
                _miasto = value;
            }
        }

        public override string ToString()
        {
            return string.Format("{0}/{1}/{2}", _ulica, _kodPocztowy, _miasto);
        }
    }

    public class Telefon
    {
        string _numer;
        string _operator;

        public Telefon()
        {

        }
        public Telefon(string numer, string oper)
        {
            this._numer = numer;
            this._operator = oper;
        }
        public string Numer
        {
            get
            {
                return _numer;
            }
            set
            {
                _numer = value;
            }

        }

        public string Operator
        {
            get
            {
                return _operator;
            }
            set
            {
                _operator = value;
            }
        }

        public override string ToString()
        {
            return string.Format("{0}/{1}", _numer, _operator);
        }


    }
}

Przepraszam za sieczkę ale usiłuje aby wartości wprowadzone w textboxy na formie wpisały się w bazę BazaStudentów.yap i później ładnie wyświetliły mi się na DataGridzie.

Problem w tym, że muszę działać tak jakby na 3 odmiennych obiektach a chciałbym wyświetlić to wszystko w jednym wierszu na datagridzie.

Jak to jakoś fajnie oprogramować? Z góry dziękuję.

0

Poprawiłem co nieco ale nadal mam problem

 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Db4objects;
using Db4objects.Db4o;

namespace Program_1
{
    public partial class Form1 : Form
    {
        public List<Student> StudentList = new List<Student>();
        public List<Adres> AdresList = new List<Adres>();
        public List<Telephone> TelephonesList = new List<Telephone>();
       
        public Form1()
        {
            InitializeComponent();
            DGV.DataSource = StudentList.ToList();
        }

        public void btnDodajStudenta_Click(object sender, EventArgs e)
        {
            using (IObjectContainer db = Db4oEmbedded.OpenFile("BazaStudentow.yap"))
            {

                Student std = new Student();
                std.Name = tbxName.Text;
                std.Surname = tbxSurname.Text;
                std.NumberOfStudent = tbxNumberOfIndex.Text;
                std.Adres = new Adres(tbxStreet.Text, tbxPostalCode.Text, tbxCity.Text);
                std.Telephone = new Telephone(tbxPhoneNumber.Text,tbxOperator.Text);

                IObjectSet result = db.QueryByExample(std);

                ListResult(result);
                db.Close();
            }
        }

        public void ListResult(IObjectSet result)
        {
            tbxCountOfStudentsInDatabase.Text = "Aktualnie "+result.Count.ToString()+" studentów";
            foreach (Student item in result)
            {
                StudentList.Add(item);
            }
            DGV.DataSource = StudentList.ToList();
        }

       
    }

    public class Student : Adres
    {
        private string _name;
        private string _surname;
        private string _numberofStudent;
        private List<Adres> _adres;
        private int _telephone;


        public Student()
        {

        }
        public Student(string name, string surname, string numberofStudent, List<Adres> adres, int telephone)
        {
            this._name = name;
            this._surname = surname;
            this._numberofStudent = numberofStudent;
            this._adres = adres;
            this._telephone = telephone;
        }

        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }

        public string Surname
        {
            get
            {
                return _surname;
            }
            set
            {
                _surname = value;
            }
        }

        public string NumberOfStudent
        {
            get
            {
                return _numberofStudent;
            }
            set
            {
                _numberofStudent = value;
            }
        }

        public List<Adres> Adres
        {
            get
            {
                return _adres;
            }
            set
            {
                _adres = value;
            }
        }

        public int Telephone
        {
            get
            {
                return _telephone;
            }
            set
            {
                _telephone = value;
            }
        }

        public override string ToString()
        {
            return string.Format("{0}/{1}/{2}/{3}/{4}", _name, _surname, _numberofStudent, _adres, _telephone);
        }
    }
    public class Adres
    {
        private string _street;
        private string _postalCode;
        private string _city;

        public Adres()
        {

        }
        public Adres(string street, string postalCode, string city)
        {
            this._street = street;
            this._postalCode = postalCode;
            this._city = city;
        }

        public string Street
        {
            get
            {
                return _street;
            }
            set
            {
                _street = value;
            }
        }

        public string PostalCode
        {
            get
            {
                return _postalCode;
            }
            set
            {
                _postalCode = value;
            }
        }

        public string City
        {
            get
            {
                return _city;
            }
            set
            {
                _city = value;
            }
        }

        public override string ToString()
        {
            return string.Format("{0}/{1}/{2}", _street, _postalCode, _city);
        }
    }

    public class Telephone
    {
        private int _phoneNumber;
        private string _operator;

        public Telephone()
        {

        }
        public Telephone(int phoneNumber, string oper)
        {
            this._phoneNumber = phoneNumber;
            this._operator = oper;
        }
        public int PhoneNumber
        {
            get
            {
                return _phoneNumber;
            }
            set
            {
                _phoneNumber = value;
            }

        }

        public string Operator
        {
            get
            {
                return _operator;
            }
            set
            {
                _operator = value;
            }
        }

        public override string ToString()
        {
            return string.Format("{0}/{1}", _phoneNumber, _operator);
        }


    }
}

Error 1 Cannot implicitly convert type 'Program_1.Adres' to 'System.Collections.Generic.List<Program_1.Adres>' C:\Users\Odyn\Desktop\OBD\Obiektowe Bazy Danych\Program_1\Form1.cs 36 29 Program_1

W tym miejscu:

public void btnDodajStudenta_Click(object sender, EventArgs e)
{
using (IObjectContainer db = Db4oEmbedded.OpenFile("BazaStudentow.yap"))
{

            Student std = new Student();
            std.Name = tbxName.Text;
            std.Surname = tbxSurname.Text;
            std.NumberOfStudent = tbxNumberOfIndex.Text;
            std.Adres = new Adres(tbxStreet.Text, tbxPostalCode.Text, tbxCity.Text); // tu
            std.Telephone = new Telephone(tbxPhoneNumber.Text,tbxOperator.Text); // i tu

            IObjectSet result = db.QueryByExample(std);

            ListResult(result);
            db.Close();
        }
    }
0

No to teraz mam tak:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Db4objects;
using Db4objects.Db4o;

namespace Program_1
{
    public partial class Form1 : Form
    {
        public List<Student> StudentList = new List<Student>();
        public List<Adres> AdresList = new List<Adres>();
        public List<Telephone> TelephonesList = new List<Telephone>();
       
        public Form1()
        {
            InitializeComponent();
            DGV.DataSource = StudentList.ToList();
        }

        public void btnDodajStudenta_Click(object sender, EventArgs e)
        {
            using (IObjectContainer db = Db4oEmbedded.OpenFile("BazaStudentow.yap"))
            {

                Student std = new Student();
                Adres adr = new Adres();
                Telephone tel = new Telephone();

                std.Name = tbxName.Text;
                std.Surname = tbxSurname.Text;
                std.NumberOfStudent = tbxNumberOfIndex.Text;
                adr.Street = tbxStreet.Text;
                adr.PostalCode = tbxPostalCode.Text;
                adr.City = tbxCity.Text;
                tel.PhoneNumber = int.Parse(tbxPhoneNumber.Text);
                tel.Operator = tbxOperator.Text;

                std.Adres = new Adres() 
                { 
                    Street = tbxStreet.Text, 
                    PostalCode = tbxPostalCode.Text,
                    City = tbxCity.Text 
                };

                std.TelephoneNumber = new Telephone() 
                { 
                    PhoneNumber = int.Parse(tbxPhoneNumber.Text), 
                    Operator = tbxOperator.Text 
                };

                //std.Adres = new Adres(tbxStreet.Text, tbxPostalCode.Text, tbxCity.Text);
                //std.Telephone = new Telephone(tbxPhoneNumber.Text,tbxOperator.Text);

               
                IObjectSet result = db.QueryByExample(std);

                ListResult(result);
                db.Close();
            }
        }

        public void ListResult(IObjectSet result)
        {
            tbxCountOfStudentsInDatabase.Text = "Aktualnie "+result.Count.ToString()+" studentów";
            foreach (Student item in result)
            {
                StudentList.Add(item);
            }
            DGV.DataSource = StudentList.ToList();
        }

       
    }

    public class Student
    {
        private string _name;
        private string _surname;
        private string _numberofStudent;
        private List<Adres> _adres;
        private int _telephoneNumber;

        public string Name { get; set; }

        public string Surname { get; set; }

        public string NumberOfStudent { get; set; }

        public List<Adres> Adres { get; set; }

        public int TelephoneNumber { get; set; }


        public Student()
        {

        }
        public Student(string name, string surname, string numberofStudent, List<Adres> adres, int telephoneNumber)
        {
            this._name = name;
            this._surname = surname;
            this._numberofStudent = numberofStudent;
            this._adres = adres;
            this._telephoneNumber = telephoneNumber;
        }

        public override string ToString()
        {
            return string.Format("{0}/{1}/{2}/{3}/{4}", _name, _surname, _numberofStudent, _adres, _telephoneNumber);
        }
    }
    public class Adres
    {
        private string _street;
        private string _postalCode;
        private string _city;

        public string Street { get; set; }

        public string PostalCode { get; set; }

        public string City { get; set; }

        public Adres()
        {

        }
        public Adres(string street, string postalCode, string city)
        {
            this._street = street;
            this._postalCode = postalCode;
            this._city = city;
        }


        public override string ToString()
        {
            return string.Format("{0}/{1}/{2}", _street, _postalCode, _city);
        }
    }

    public class Telephone
    {
        private int _phoneNumber;
        private string _operator;

        public int PhoneNumber { get; set; }
        public string Operator { get; set; }
        public Telephone()
        {

        }
        public Telephone(int phoneNumber, string oper)
        {
            this._phoneNumber = phoneNumber;
            this._operator = oper;
        }


        public override string ToString()
        {
            return string.Format("{0}/{1}", _phoneNumber, _operator);
        }


    }
}

 

Wywalilem te gettery i settery ale dalej jest problem z ta konwersją ;/

1
Odyn napisał(a):

Wywalilem te gettery i settery ale dalej jest problem z ta konwersją ;/

Za to teraz da się to czytać i nie zwariować.

W klasie Student masz:

public List<Adres> Adres { get; set; }

A chcesz przypisać:

std.Adres = new Adres() 
                { 
                    Street = tbxStreet.Text, 
                    PostalCode = tbxPostalCode.Text,
                    City = tbxCity.Text 
                };

Nie możesz przypisać pojedynczego obiektu to właściwości, która jest kolekcją.
Spróbuj tak:

std.Adres = new List<Adres>();
std.Address.Add(new Adres { Street = tbxStreet.Text, PostalCode = tbxPostalCode.Text, City = tbxCity.Text };
0

Ok 1 błędu się już pozbyłem

 

std.TelephoneNumber = Convert.ToInt16(new Telephone() 
                { 
                    PhoneNumber = int.Parse(tbxPhoneNumber.Text), 
                    Operator = tbxOperator.Text 
                });

Pozostał jeszcze ten gorszy

 std.Adres = new Adres() 
                { 
                    Street = tbxStreet.Text, 
                    PostalCode = tbxPostalCode.Text,
                    City = tbxCity.Text 
                };

Error 1 Cannot implicitly convert type 'Program_1.Adres' to 'System.Collections.Generic.List<Program_1.Adres>'

0

Ok poprawione:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Db4objects;
using Db4objects.Db4o;

namespace Program_1
{
    public partial class Form1 : Form
    {
        public List<Student> StudentList = new List<Student>();
        public List<Adres> AdresList = new List<Adres>();
        public List<Telephone> TelephonesList = new List<Telephone>();
       
        public Form1()
        {
            InitializeComponent();
            DGV.DataSource = StudentList.ToList();
        }

        public void btnDodajStudenta_Click(object sender, EventArgs e)
        {
            using (IObjectContainer db = Db4oEmbedded.OpenFile("BazaStudentow.yap"))
            {

                Student std = new Student();
                Adres adr = new Adres();
                Telephone tel = new Telephone();

                std.Name = tbxName.Text;
                std.Surname = tbxSurname.Text;
                std.NumberOfStudent = tbxNumberOfIndex.Text;
                adr.Street = tbxStreet.Text;
                adr.PostalCode = tbxPostalCode.Text;
                adr.City = tbxCity.Text;
                tel.PhoneNumber = int.Parse(tbxPhoneNumber.Text);
                tel.Operator = tbxOperator.Text;

                std.Adres = new List<Adres>();
                std.Adres.Add(new Adres { Street = tbxStreet.Text, PostalCode = tbxPostalCode.Text, City = tbxCity.Text });
                
                std.TelephoneNumber = Convert.ToInt16(new Telephone() 
                { 
                    PhoneNumber = int.Parse(tbxPhoneNumber.Text), 
                    Operator = tbxOperator.Text 
                });
               
                IObjectSet result = db.QueryByExample(std);

                ListResult(result);
                db.Close();
            }
        }

        public void ListResult(IObjectSet result)
        {
            tbxCountOfStudentsInDatabase.Text = "Aktualnie "+result.Count.ToString()+" studentów";
            foreach (Student item in result)
            {
                StudentList.Add(item);
            }
            DGV.DataSource = StudentList.ToList();
        }

       
    }

    public class Student
    {
        private string _name;
        private string _surname;
        private string _numberofStudent;
        private List<Adres> _adres;
        private int _telephoneNumber;

        public string Name { get; set; }

        public string Surname { get; set; }

        public string NumberOfStudent { get; set; }

        public List<Adres> Adres { get; set; }

        public int TelephoneNumber { get; set; }


        public Student()
        {

        }
        public Student(string name, string surname, string numberofStudent, List<Adres> adres, int telephoneNumber)
        {
            this._name = name;
            this._surname = surname;
            this._numberofStudent = numberofStudent;
            this._adres = adres;
            this._telephoneNumber = telephoneNumber;
        }

        public override string ToString()
        {
            return string.Format("{0}/{1}/{2}/{3}/{4}", _name, _surname, _numberofStudent, _adres, _telephoneNumber);
        }
    }
    public class Adres
    {
        private string _street;
        private string _postalCode;
        private string _city;

        public string Street { get; set; }

        public string PostalCode { get; set; }

        public string City { get; set; }

        public Adres()
        {

        }
        public Adres(string street, string postalCode, string city)
        {
            this._street = street;
            this._postalCode = postalCode;
            this._city = city;
        }


        public override string ToString()
        {
            return string.Format("{0}/{1}/{2}", _street, _postalCode, _city);
        }
    }

    public class Telephone
    {
        private int _phoneNumber;
        private string _operator;

        public int PhoneNumber { get; set; }
        public string Operator { get; set; }
        public Telephone()
        {

        }
        public Telephone(int phoneNumber, string oper)
        {
            this._phoneNumber = phoneNumber;
            this._operator = oper;
        }


        public override string ToString()
        {
            return string.Format("{0}/{1}", _phoneNumber, _operator);
        }


    }
}


 

Teraz mam taki exception jak chcę dodać jakiegoś studenta:

An unhandled exception of type 'System.InvalidCastException' occurred in mscorlib.dll

Additional information: Nie można rzutować obiektu typu 'Program_1.Telephone' na typ 'System.IConvertible'.

1 użytkowników online, w tym zalogowanych: 0, gości: 1