StevanovicMilan

Vežba 16 Opet

Mar 9th, 2018
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.39 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace Vežba16Opet_2018
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.  
  20.         private void sifraTextBox_Leave(object sender, EventArgs e)
  21.         {
  22.             errorProvider1.Clear();
  23.  
  24.             int sifra;
  25.             if(int.TryParse(sifraTextBox.Text, out sifra))
  26.             {
  27.                 string query = string.Format("SELECT * FROM Glumac WHERE GlumacID = {0}", sifra);
  28.                 DataTable dt = Database.ExecuteQuery(query);
  29.                 if (dt.Rows.Count > 0)
  30.                 {
  31.                     DataRow dr = dt.Rows[0];
  32.                     imeTextBox.Text = dr["Ime"].ToString();
  33.                     prezimeTextBox.Text = dr["Prezime"].ToString();
  34.                     datumTextBox.Text = dr["DatumRodjenja"].ToString();
  35.                     mestoTextBox.Text = dr["MestoRodjenja"].ToString();
  36.                 }
  37.                 else
  38.                 {
  39.                     imeTextBox.Clear();
  40.                     prezimeTextBox.Clear();
  41.                     datumTextBox.Clear();
  42.                     mestoTextBox.Clear();
  43.                 }
  44.             }
  45.             else
  46.             {
  47.                 errorProvider1.SetError(sifraTextBox, "Niste uneli ispravnu vrednost za sifru!");
  48.             }
  49.         }
  50.  
  51.         private void obrisiButton_Click(object sender, EventArgs e)
  52.         {
  53.             errorProvider1.Clear();
  54.  
  55.             int sifra;
  56.             if(int.TryParse(sifraTextBox.Text, out sifra))
  57.             {
  58.                 string query = string.Format("DELETE FROM Glumac WHERE GlumacID = {0}", sifra);
  59.                 if(Database.ExecuteNonQuery(query) > 0)
  60.                 {
  61.                     MessageBox.Show("Uspesno ste obrisali glumca");
  62.                    PrikaziSveGlumce();
  63.                 }
  64.                 else
  65.                 {
  66.                     MessageBox.Show("Ne postoji glumac sa zadatom sifrom!");
  67.                 }
  68.             }
  69.             else
  70.             {
  71.                 errorProvider1.SetError(sifraTextBox, "Niste uneli ispravnu vrednost za sifru");
  72.             }
  73.         }
  74.  
  75.         private void PrikaziSveGlumce()
  76.         {
  77.             string query = string.Format("SELECT * FROM Glumac");
  78.             dataGridView1.DataSource = Database.ExecuteQuery(query);
  79.         }
  80.  
  81.         private void Form1_Load(object sender, EventArgs e)
  82.         {
  83.             PrikaziSveGlumce();
  84.         }
  85.  
  86.         private void izmeniButton_Click(object sender, EventArgs e)
  87.         {
  88.             errorProvider1.Clear();
  89.  
  90.             int sifra;
  91.             if (int.TryParse(sifraTextBox.Text, out sifra))
  92.             {
  93.                 string query = string.Format("UPDATE Glumac SET Ime = '{0}', Prezime = '{1}', MestoRodjenja = '{2}' WHERE GlumacID = {3}", imeTextBox.Text, prezimeTextBox.Text, mestoTextBox.Text, sifra);
  94.                 if (Database.ExecuteNonQuery(query) > 0)
  95.                 {
  96.                     MessageBox.Show("Uspesno ste izmenili glumca");
  97.                     PrikaziSveGlumce();
  98.                 }
  99.                 else
  100.                 {
  101.                     MessageBox.Show("Ne postoji glumac sa zadatom sifrom!");
  102.                 }
  103.             }
  104.             else
  105.             {
  106.                 errorProvider1.SetError(sifraTextBox, "Niste uneli ispravnu vrednost za sifru");
  107.             }
  108.         }
  109.  
  110.         private void upisiButton_Click(object sender, EventArgs e)
  111.         {
  112.             errorProvider1.Clear();
  113.  
  114.             int sifra;
  115.             if (int.TryParse(sifraTextBox.Text, out sifra))
  116.             {
  117.                 DateTime datumRodjenja;
  118.                 if(DateTime.TryParse(datumTextBox.Text, out datumRodjenja))
  119.                 {
  120.                     string query = string.Format("INSERT INTO Glumac(GlumacID, Ime, Prezime, DatumRodjenja, MestoRodjenja) VALUES ({0}, '{1}', '{2}', '{3}', '{4}');", sifra, imeTextBox.Text, prezimeTextBox.Text, datumRodjenja.ToShortDateString(), mestoTextBox.Text);
  121.                     if (Database.ExecuteNonQuery(query) > 0)
  122.                     {
  123.                         MessageBox.Show("Uspesno ste dodali glumca");
  124.                         PrikaziSveGlumce();
  125.                     }
  126.                     else
  127.                     {
  128.                         MessageBox.Show("Greska u dodavanju glumca!");
  129.                     }
  130.                 }
  131.                 else
  132.                 {
  133.                     MessageBox.Show("Pogresan unos datuma!");
  134.                 }
  135.                
  136.             }
  137.             else
  138.             {
  139.                 errorProvider1.SetError(sifraTextBox, "Niste uneli ispravnu vrednost za sifru");
  140.             }
  141.         }
  142.  
  143.         private void izadjiButton_Click(object sender, EventArgs e)
  144.         {
  145.             Close();
  146.         }
  147.     }
  148. }
  149.  
  150.  
  151.  
  152.  
  153.  
  154. using System;
  155. using System.Collections.Generic;
  156. using System.Linq;
  157. using System.Text;
  158. using System.Threading.Tasks;
  159. using System.Data.OleDb;
  160. using System.Data;
  161.  
  162. namespace Vežba16Opet_2018
  163. {
  164.     class Database
  165.     {
  166.         public static DataTable ExecuteQuery(string query)
  167.         {
  168.             DataTable dt = new DataTable();
  169.  
  170.             using (OleDbConnection connection = new OleDbConnection(Properties.Settings.Default.B3DVDkolekcijaConnectionString))
  171.             {
  172.                 using (OleDbCommand command = new OleDbCommand(query, connection))
  173.                 {
  174.                     connection.Open();
  175.                     using (OleDbDataReader reader = command.ExecuteReader())
  176.                     {
  177.                         dt.Load(reader);
  178.                     }
  179.                 }
  180.             }
  181.             return dt;
  182.         }
  183.  
  184.         public static int ExecuteNonQuery(string query)
  185.         {
  186.             int rowsAffected = 0;
  187.  
  188.             using (OleDbConnection connection = new OleDbConnection(Properties.Settings.Default.B3DVDkolekcijaConnectionString))
  189.             {
  190.                 using (OleDbCommand command = new OleDbCommand(query, connection))
  191.                 {
  192.                     connection.Open();
  193.                     rowsAffected = command.ExecuteNonQuery();
  194.                 }
  195.             }
  196.             return rowsAffected;
  197.         }
  198.     }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment