filip710

BAZE PODATAKA LV5 Z2

Dec 21st, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.46 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. 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.             /*Pritiskom na tipku Prikaži sve potrebno je spojiti se na bazu podataka student
  25.             (server: 161.53.201.59; username: student; password: student) i u
  26.             DataGridView dgvPodaci ispisati sve osobe iz tablice osobe poredane
  27.             abecedno po prezimenu.*/
  28.  
  29.             SqlConnection conn = new SqlConnection("Data Source=192.168.26.14;Initial Catalog=student;User ID=student;Password=student");
  30.             conn.Open();
  31.             string statement = "SELECT * FROM osobe_R3758 ORDER BY prezime;";
  32.             SqlDataAdapter dataAdapter = new SqlDataAdapter(statement, conn);
  33.             DataTable dt = new DataTable();
  34.             dataAdapter.Fill(dt);
  35.             dgvPodaci.DataSource = dt;
  36.             conn.Close();
  37.  
  38.         }
  39.  
  40.         private void btnSpremi_Click(object sender, EventArgs e)
  41.         {
  42.             // OVDJE SLIJEDI ZADATAK IZ LV7 b) (i BONUS zadatak):
  43.             /*Pritiskom na tipku Spremi potrebno je spremiti novu osobu s podacima (OIB,
  44.             ime, prezime, spol, datum rođenja) koje je korisnik upisao u sučelju aplikacije
  45.             (txtOIB, txtIme, txtPrezime, rbM, rbZ, txtDatum).*/
  46.  
  47.             char spol;
  48.             if (rbM.Checked)
  49.                 spol = 'M';
  50.             else
  51.                 spol = 'F';
  52.  
  53.             txtDatum.Format = DateTimePickerFormat.Custom;
  54.             txtDatum.CustomFormat = "yyyy-MM-dd";
  55.  
  56.             SqlConnection conn = new SqlConnection("Data Source=192.168.26.14;Initial Catalog=student;User ID=student;Password=student");
  57.             conn.Open();
  58.  
  59.             string statement = "INSERT INTO osobe_R3758 (oib, ime, prezime, datum, spol) VALUES ('" + txtOIB.Text + "', '" + txtIme.Text + "', '" + txtPrezime.Text + "', '" + txtDatum.Text + "', '" + spol + "');";
  60.             SqlCommand cmd = new SqlCommand(statement, conn);
  61.             cmd.ExecuteNonQuery();
  62.             conn.Close();
  63.         }
  64.  
  65.         public void obrisiSve()
  66.         {
  67.             txtOIB.Text = "";
  68.             txtIme.Text = "";
  69.             txtPrezime.Text = "";
  70.             txtDatum.Text = "";
  71.             dgvPodaci.ClearSelection();
  72.             txtOIB.ReadOnly = false;
  73.         }
  74.  
  75.         private void btnObrisi_Click(object sender, EventArgs e)
  76.         {
  77.             obrisiSve();
  78.         }
  79.  
  80.         private void dgvPodaci_CellClick(object sender, DataGridViewCellEventArgs e)
  81.         {
  82.             //OVDJE JE DODATAK POTREBAN ZA BONUS ZADATAK
  83.             txtOIB.Text = dgvPodaci.SelectedRows[0].Cells[0].Value.ToString();
  84.             txtIme.Text = dgvPodaci.SelectedRows[0].Cells[1].Value.ToString();
  85.             txtPrezime.Text = dgvPodaci.SelectedRows[0].Cells[2].Value.ToString();
  86.             if (dgvPodaci.SelectedRows[0].Cells[3].Value.ToString() == "M") rbM.Checked = true;
  87.             else rbZ.Checked = true;
  88.             txtOIB.ReadOnly = true;
  89.             txtDatum.Text = dgvPodaci.SelectedRows[0].Cells[4].Value.ToString();
  90.         }
  91.     }
  92. }
Add Comment
Please, Sign In to add comment