Advertisement
StevanovicMilan

Vežba 15

Feb 23rd, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.82 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 Vezba_15_2018
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         private BindingSource bs;
  16.  
  17.         public Form1()
  18.         {
  19.             InitializeComponent();
  20.  
  21.             bs = new BindingSource();
  22.         }
  23.  
  24.         private void UcitajSveGradove()
  25.         {
  26.             bs.DataSource = Database.ExecuteQuery("SELECT * FROM Grad ORDER BY GradID");
  27.         }
  28.  
  29.         private void PrikaziGrad()
  30.         {
  31.             DataRowView grad = (DataRowView)bs.Current;
  32.             if (grad != null)
  33.             {
  34.                 sifraTextBox.Text = grad["GradID"].ToString();
  35.                 gradTextBox.Text = grad["Grad"].ToString();
  36.                 pozBrojTextBox.Text = grad["PozivniBroj"].ToString();
  37.                 postBrojTextBox.Text = grad["PostanskiBroj"].ToString();
  38.                 stanovniciTextBox.Text = grad["BrojStanovnika"].ToString();
  39.             }
  40.         }
  41.  
  42.         private void Form1_Load(object sender, EventArgs e)
  43.         {
  44.             UcitajSveGradove();
  45.  
  46.             bs.MoveFirst();
  47.             PrikaziGrad();
  48.         }
  49.  
  50.         private void sledeciButton_Click(object sender, EventArgs e)
  51.         {
  52.             if (string.IsNullOrEmpty(sifraTextBox.Text))
  53.                 bs.MoveLast();
  54.             else
  55.             {
  56.                 if (bs.Position == bs.Count - 1)
  57.                     bs.MoveFirst();
  58.                 else
  59.                     bs.MoveNext();
  60.             }
  61.             PrikaziGrad();
  62.         }
  63.  
  64.         private void prethodniButton_Click(object sender, EventArgs e)
  65.         {
  66.             if(string.IsNullOrEmpty(sifraTextBox.Text))
  67.             {
  68.                 bs.MoveFirst();
  69.             }
  70.             else
  71.             {
  72.                 if (bs.Position == 0)
  73.                     bs.MoveLast();
  74.                 else
  75.                     bs.MovePrevious();
  76.             }
  77.             PrikaziGrad();
  78.         }
  79.  
  80.         private void noviButton_Click(object sender, EventArgs e)
  81.         {
  82.             sifraTextBox.Clear();
  83.             gradTextBox.Clear();
  84.             pozBrojTextBox.Clear();
  85.             postBrojTextBox.Clear();
  86.             stanovniciTextBox.Clear();
  87.             sifraTextBox.Focus();
  88.         }
  89.  
  90.         private void izadjiButton_Click(object sender, EventArgs e)
  91.         {
  92.             Close();
  93.         }
  94.  
  95.         private void upisiButton_Click(object sender, EventArgs e)
  96.         {
  97.             int sifra = int.Parse(sifraTextBox.Text);
  98.             string grad = gradTextBox.Text;
  99.             int pozivniBroj = int.Parse(pozBrojTextBox.Text);
  100.             int postanskiBroj = int.Parse(postBrojTextBox.Text);
  101.             int brojStanovnika = int.Parse(stanovniciTextBox.Text);
  102.  
  103.             string query = string.Format("INSERT INTO Grad (GradID, Grad, PozivniBroj, PostanskiBroj, BrojStanovnika) VALUES({0}, '{1}', {2}, {3}, {4});", sifra, grad, pozivniBroj, postanskiBroj, brojStanovnika);
  104.             if (Database.ExecuteNonQuery(query) > 0)
  105.             {
  106.                 MessageBox.Show("Uspesno ste dodali grad u bazu!");
  107.             }
  108.             else
  109.             {
  110.                 MessageBox.Show("Greska!");
  111.             }
  112.  
  113.             UcitajSveGradove();
  114.         }
  115.     }
  116. }
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123. using System;
  124. using System.Collections.Generic;
  125. using System.Linq;
  126. using System.Text;
  127. using System.Threading.Tasks;
  128. using System.Data.OleDb;
  129. using System.Data;
  130.  
  131. namespace Vezba_15_2018
  132. {
  133.     class Database
  134.     {
  135.         public static DataTable ExecuteQuery(string query)
  136.         {
  137.             DataTable dataTable = new DataTable();
  138.             using (OleDbConnection connection =
  139.                 new OleDbConnection(Properties.Settings.Default.B5FudbKluboviConnectionString))
  140.             {
  141.                 connection.Open();
  142.                 using (OleDbCommand command = new OleDbCommand(query, connection))
  143.                 {
  144.                     using (OleDbDataReader reader = command.ExecuteReader())
  145.                     {
  146.                         dataTable.Load(reader);
  147.                     }
  148.                 }
  149.             }
  150.             return dataTable;
  151.         }
  152.  
  153.         public static int ExecuteNonQuery(string query)
  154.         {
  155.             int rowsAffected = 0;
  156.             using (OleDbConnection connection =
  157.                 new OleDbConnection(Properties.Settings.Default.B5FudbKluboviConnectionString))
  158.             {
  159.                 connection.Open();
  160.                 using (OleDbCommand command = new OleDbCommand(query, connection))
  161.                 {
  162.                     rowsAffected = command.ExecuteNonQuery();
  163.                 }
  164.             }
  165.             return rowsAffected;
  166.         }
  167.     }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement