Advertisement
Domy131097

[LV7] Baze podataka

May 17th, 2018
166
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 3.28 KB | None
  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. USING System.DATA.SqlClient;
  11.  
  12. namespace Baze_LV7_predlozak
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.  
  21.         private void btnSve_Click(object sender, EventArgs e)
  22.         {
  23.             // OVDJE SLIJEDI ZADATAK IZ LV7 a):
  24.             SqlConnection conn = NEW SqlConnection("Data Source=192.168.23.35;Initial Catalog=student;User ID=student;Password=student");
  25.             conn.OPEN();
  26.             string statement = "SELECT * FROM osobe ORDER BY prezime ASC";
  27.             SqlDataAdapter dataAdapter = NEW SqlDataAdapter(statement, conn);
  28.             DataTable dt = NEW DataTable();
  29.             dataAdapter.Fill(dt);
  30.             dgvPodaci.DataSource = dt;
  31.             conn.Close();
  32.         }
  33.  
  34.         private void btnSpremi_Click(object sender, EventArgs e)
  35.         {
  36.             // OVDJE SLIJEDI ZADATAK IZ LV7 b) (i BONUS zadatak):
  37.             string ime = txtIme.Text, prezime = txtPrezime.Text, oib = txtOIB.Text, datum = txtDatum.VALUE.ToString("yyyy/MM/dd"), spol = "M";
  38.             SqlConnection conn = NEW SqlConnection("Data Source=192.168.23.35;Initial Catalog=student;User ID=student;Password=student");
  39.             conn.OPEN();
  40.             string statement = "";
  41.             IF (rbM.Checked) spol = "M";
  42.             ELSE IF (rbZ.Checked) spol = "Z";
  43.             IF (txtOIB.ReadOnly == FALSE)
  44.             {
  45.                 IF (ime != "" && prezime != "" && oib != "" && datum != "")
  46.                 {
  47.                     statement = "INSERT INTO osobe VALUES('" + oib + "', '" + ime + "', '" + prezime + "', '" + spol + "', '" + datum + "')";
  48.                 }
  49.             }
  50.             ELSE
  51.             {
  52.                 statement = "UPDATE osobe SET ime='" + ime + "', prezime ='" + prezime + "', spol ='" + spol + "', datum_rodjenja='" + datum + "' WHERE OIB='" + oib + "'";
  53.             }
  54.             SqlCommand cmd = NEW SqlCommand(statement, conn);
  55.             cmd.ExecuteNonQuery();
  56.             conn.Close();
  57.         }
  58.  
  59.         public void obrisiSve()
  60.         {
  61.             txtOIB.Text = "";
  62.             txtIme.Text = "";
  63.             txtPrezime.Text = "";
  64.             txtDatum.Text = "";
  65.             dgvPodaci.ClearSelection();
  66.             txtOIB.ReadOnly = FALSE;
  67.         }
  68.  
  69.         private void btnObrisi_Click(object sender, EventArgs e)
  70.         {
  71.             obrisiSve();
  72.         }
  73.  
  74.         private void dgvPodaci_CellClick(object sender, DataGridViewCellEventArgs e)
  75.         {
  76.             //OVDJE JE DODATAK POTREBAN ZA BONUS ZADATAK
  77.             txtOIB.Text = dgvPodaci.SelectedRows[0].Cells[0].VALUE.ToString();
  78.             txtIme.Text = dgvPodaci.SelectedRows[0].Cells[1].VALUE.ToString();
  79.             txtPrezime.Text = dgvPodaci.SelectedRows[0].Cells[2].VALUE.ToString();
  80.             IF (dgvPodaci.SelectedRows[0].Cells[3].VALUE.ToString() == "M") rbM.Checked = TRUE;
  81.             ELSE rbZ.Checked = TRUE;
  82.             txtOIB.ReadOnly = TRUE;
  83.             txtDatum.Text = dgvPodaci.SelectedRows[0].Cells[4].VALUE.ToString();
  84.         }
  85.  
  86.  
  87.  
  88.  
  89.  
  90.     }
  91. }
Advertisement
RAW Paste Data Copied
Advertisement