Skip to content

Instantly share code, notes, and snippets.

@mzdv
Created April 11, 2016 21:52
Show Gist options
  • Save mzdv/376831cb2811fe8820ce629e65b6825b to your computer and use it in GitHub Desktop.
Save mzdv/376831cb2811fe8820ce629e65b6825b to your computer and use it in GitHub Desktop.
//Broker
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;
namespace Sesija
{
using Modeli;
public class Broker
{
OleDbConnection konekcija;
OleDbCommand komanda;
void konektujSe()
{
konekcija = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Rasa\Desktop\BazaDomaci2.accdb;Persist Security Info=False;");
komanda = konekcija.CreateCommand();
}
private static Broker broker;
public static Broker DajBrokera()
{
if (broker == null)
{
broker = new Broker();
}
return broker;
}
private Broker()
{
konektujSe();
}
public List<Student> VratiSveStudente()
{
List<Student> studenti = new List<Student>();
try
{
komanda.CommandText = "Select * from TStudent Order By BrIndID ASC";
komanda.CommandType = CommandType.Text;
konekcija.Open();
OleDbDataReader citac = komanda.ExecuteReader();
while (citac.Read())
{
Student student = new Student();
student.BrIndexa = citac.GetInt32(0);
student.Ime = citac.GetString(1);
student.Prezime = citac.GetString(2);
studenti.Add(student);
}
return studenti;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (konekcija != null)
konekcija.Close();
}
}
public List<Prijava> VratiPrijaveStudenta(Student s)
{
List<Prijava> prijave = new List<Prijava>();
try
{
komanda.CommandText = "Select TPrijava.BrPrijaveID,TRok.Naziv,TPredmet.Naziv,"+
"TPrijava.Datum,TPrijava.Ocena from TRok INNER JOIN (TPrijava INNER JOIN TPredmet ON "+
"TPrijava.PredmetId=TPredmet.PredmetId) ON TPrijava.RokId=TRok.RokId Where BrIndId=" + s.BrIndexa+
" order by TRok.RokId asc";
komanda.CommandType = CommandType.Text;
konekcija.Open();
OleDbDataReader citac = komanda.ExecuteReader();
while (citac.Read())
{
Prijava prijava = new Prijava();
Rok rok = new Rok();
Student student = new Student();
Predmet predmet = new Predmet();
prijava.BrojPrijave = citac.GetInt32(0);
rok.NazivRoka = citac.GetString(1);
predmet.NazivPred = citac.GetString(2);
prijava.Datum = citac.GetDateTime(3);
prijava.Ocena = citac.GetInt32(4);
prijava.Rok = rok;
prijava.Student = student;
prijava.Predmet = predmet;
prijave.Add(prijava);
}
return prijave;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (konekcija != null)
konekcija.Close();
}
}
public List<Predmet> VratiSvePredmete()
{
List<Predmet> predmeti = new List<Predmet>();
try
{
komanda.CommandText = "Select * from TPredmet";
komanda.CommandType = CommandType.Text;
konekcija.Open();
OleDbDataReader citac = komanda.ExecuteReader();
while (citac.Read())
{
Predmet predmet = new Predmet();
predmet.PredmetID = citac.GetInt32(0);
predmet.NazivPred = citac.GetString(1);
predmeti.Add(predmet);
}
return predmeti;
}
catch(Exception ex)
{
throw ex;
}
finally
{
if (konekcija != null)
konekcija.Close();
}
}
public List<Rok> VratiAktivanRok()
{
List<Rok> rokovi = new List<Rok>();
try
{
komanda.CommandText = "Select RokID,Naziv from TRok Where AktivanRok=true";
komanda.CommandType = CommandType.Text;
konekcija.Open();
OleDbDataReader citac = komanda.ExecuteReader();
while (citac.Read())
{
Rok rok = new Rok();
rok.RokID = citac.GetInt32(0);
rok.NazivRoka = citac.GetString(1);
rokovi.Add(rok);
}
return rokovi;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (konekcija != null)
konekcija.Close();
}
}
public int UnesiPrijavu(Prijava pr)
{
try
{
komanda.CommandText = "Insert into TPrijava Values ("+pr.BrojPrijave+","+pr.Rok.RokID+","+pr.Student.BrIndexa+","+pr.Predmet.PredmetID+",'"+pr.Datum+"',"+pr.Ocena+")";
komanda.CommandType = CommandType.Text;
konekcija.Open();
int rezultat = komanda.ExecuteNonQuery();
return rezultat;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (konekcija != null)
konekcija.Close();
}
}
public int ZameniPrijavu(Prijava pr)
{
try
{
komanda.CommandText = "Update TPrijava Set BrPrijaveID="+pr.BrojPrijave+",RokID="+pr.Rok.RokID+",BrIndID="+pr.Student.BrIndexa+",PredmetID="+pr.Predmet.PredmetID+",Datum='"+pr.Datum+"',Ocena="+pr.Ocena+" Where BrIndID="+pr.Student.BrIndexa;
komanda.CommandType = CommandType.Text;
konekcija.Open();
int rezultat = komanda.ExecuteNonQuery();
return rezultat;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (konekcija != null)
konekcija.Close();
}
}
public int VratiVrednostBrojaca()
{
int brojac;
try
{
komanda.CommandText = "Select Brojac from TSifarnik Where IDDokumenta='PR'";
komanda.CommandType = CommandType.Text;
konekcija.Open();
OleDbDataReader citac = komanda.ExecuteReader();
citac.Read();
brojac = citac.GetInt32(0);
return brojac;
}
catch(Exception ex)
{
throw ex;
}
finally
{
if(konekcija!=null)
konekcija.Close();
}
}
public int AzurirajBrojac()
{
try
{
komanda.CommandText = "Update TSifarnik Set Brojac=Brojac+1";
komanda.CommandType = CommandType.Text;
konekcija.Open();
int rez = komanda.ExecuteNonQuery();
return rez;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (konekcija != null)
konekcija.Close();
}
}
}
}
//Student
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Modeli
{
public class Student
{
int brIndexa;
public int BrIndexa
{
get { return brIndexa; }
set { brIndexa = value; }
}
string ime;
public string Ime
{
get { return ime; }
set { ime = value; }
}
string prezime;
public string Prezime
{
get { return prezime; }
set { prezime = value; }
}
public override string ToString()
{
return ime +" "+ prezime;
}
}
}
//Sifrarnik
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Modeli
{
public class Sifarnik
{
string nazivDok;
public string NazivDok
{
get { return nazivDok; }
set { nazivDok = value; }
}
int brojacDok;
public int BrojacDok
{
get { return brojacDok; }
set { brojacDok = value; }
}
}
}
//Rok
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Modeli
{
public class Rok
{
int rokID;
public int RokID
{
get { return rokID; }
set { rokID = value; }
}
string nazivRoka;
public string NazivRoka
{
get { return nazivRoka; }
set { nazivRoka = value; }
}
bool aktivanRok;
public bool AktivanRok
{
get { return aktivanRok; }
set { aktivanRok = value; }
}
public override string ToString()
{
return NazivRoka;
}
}
}
//Prijava
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
namespace Modeli
{
public class Prijava
{
int brojPrijave;
[DisplayName("Broj prijave")]
public int BrojPrijave
{
get { return brojPrijave; }
set { brojPrijave = value; }
}
Rok rok;
[DisplayName("Naziv roka")]
public Rok Rok
{
get { return rok; }
set { rok = value; }
}
Student student;
[Browsable(false)]
[DisplayName("Student")]
public Student Student
{
get { return student; }
set { student = value; }
}
Predmet predmet;
[DisplayName("Naziv premeta")]
public Predmet Predmet
{
get { return predmet; }
set { predmet = value; }
}
DateTime datum;
public DateTime Datum
{
get { return datum; }
set { datum = value; }
}
int ocena;
public int Ocena
{
get { return ocena; }
set { ocena = value; }
}
}
}
//Predmet
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Modeli
{
public class Predmet
{
int predmetID;
public int PredmetID
{
get { return predmetID; }
set { predmetID = value; }
}
string nazivPred;
public string NazivPred
{
get { return nazivPred; }
set { nazivPred = value; }
}
public override string ToString()
{
return NazivPred;
}
}
}
//Forma
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Forma
{
using Modeli;
using Sesija;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
cbxSviStudenti1.DataSource = Broker.DajBrokera().VratiSveStudente();
cbxSviStudenti1.DisplayMember = "BrIndexa";
cbxSviStudenti2.DataSource = Broker.DajBrokera().VratiSveStudente();
cbxSviPredmeti.DataSource = Broker.DajBrokera().VratiSvePredmete();
cbxAktivanRok.DataSource = Broker.DajBrokera().VratiAktivanRok();
txtSifraPrijave.Text = Broker.DajBrokera().VratiVrednostBrojaca().ToString();
}
private void cbxSviStudenti1_SelectedIndexChanged(object sender, EventArgs e)
{
Student s = new Student();
s = cbxSviStudenti1.SelectedItem as Student;
txtImePrezime.Text = s.Ime + " " + s.Prezime; //s.ToString();
dgvPrijave.DataSource = Broker.DajBrokera().VratiPrijaveStudenta(s);
}
public void AzurirajBrojac()
{
Broker.DajBrokera().AzurirajBrojac();
txtSifraPrijave.Text = Broker.DajBrokera().VratiVrednostBrojaca().ToString();
}
public int BrojPrijava(Student st)
{
List<Prijava> listaPrijava = new List<Prijava>();
listaPrijava = Broker.DajBrokera().VratiPrijaveStudenta(st);
return listaPrijava.Count;
}
public string NazivRoka(Student std)
{
List<Prijava> listaPrijava = new List<Prijava>();
listaPrijava = Broker.DajBrokera().VratiPrijaveStudenta(std);
Prijava prijava = listaPrijava[0];
return prijava.Rok.NazivRoka;
}
private void btnSacuvaj_Click(object sender, EventArgs e)
{
Prijava pr = new Prijava();
Student s = new Student();
Predmet p = new Predmet();
Rok r = new Rok();
s = cbxSviStudenti2.SelectedItem as Student;
p = cbxSviPredmeti.SelectedItem as Predmet;
r = cbxAktivanRok.SelectedItem as Rok;
int ocena = Int32.Parse(txtOcena.Text);
int brojPrijave = Int32.Parse(txtSifraPrijave.Text);
int a = 0;
if(int.TryParse(txtSifraPrijave.Text,out a)){
}
DateTime datum = DateTime.ParseExact(txtDatum.Text,"dd.MM.yyyy",null);
if (ocena >= 6 && ocena <= 10)
{
pr.BrojPrijave = brojPrijave;
pr.Datum = datum;
pr.Ocena = ocena;
pr.Predmet = p;
pr.Rok = r;
pr.Student = s;
if (BrojPrijava(s) > 0)
{
if (MessageBox.Show("Prijava za studenta sa brojem indexa: " + s.BrIndexa + " je uneta u roku: " + NazivRoka(s),
"Da li zelite da sacuvate novu prijavu?", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
Broker.DajBrokera().ZameniPrijavu(pr);
AzurirajBrojac();
MessageBox.Show("Prijava je uspesno uneta");
}
else
{
MessageBox.Show("Nista");
}
}
else
{
Broker.DajBrokera().UnesiPrijavu(pr);
AzurirajBrojac();
MessageBox.Show("Prijava je uspesno uneta");
}
}
else
{
MessageBox.Show("Ocena mora biti od 6 do 10");
}
}
}
}
//Druga forma
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Forma
{
public partial class GlavnaForma : Form
{
public GlavnaForma()
{
InitializeComponent();
}
private void domaciRadFormaToolStripMenuItem_Click(object sender, EventArgs e)
{
new Form1().ShowDialog();
}
private void forma2ToolStripMenuItem_Click(object sender, EventArgs e)
{
new PrvaForma().ShowDialog();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment