You are on page 1of 10

VEZBA 7

Komunikacija sa bazom. Select, insert update i delete.

Izgled forme:

Klasa konekcija
using
using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Data.SqlClient;
System.Data;

namespace Vezba6Novo
{
public class Konekcija
{
SqlConnection con;
public Konekcija()
{
con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|
DataDirectory|\MojaBaza.mdf;Integrated Security=True;User Instance=True");
}

private string _prezime. public int ClanID { get { return _clanID. System. private string _adresa. } set { _clanID = value.State == ConnectionState.Open(). } public void CloseConnection() { if (con.public SqlConnection ConnectionString() { return con.State == ConnectionState.Open) con. namespace Vezba6Novo { public class Clan { private int _clanID.Linq. System. private bool _pol.Closed) con. System. private string _telefon.Text.Collections. } public void OpenConnection() { if (con. private string _jmbg. } } .Generic.Close(). private string _ime. } } } Klasa Clan using using using using System.

} } public string Telefon { get { return _telefon. } set { _prezime = value. } } public string Adresa { get { return _adresa. } } public string Jmbg { get { return _jmbg. } } public string Prezime { get { return _prezime. } set { _ime = value. } set { _jmbg = value. } set { _adresa = value. .public string Ime { get { return _ime.

namespace Vezba6Novo { public class ClanProvider { Konekcija kon = new Konekcija(). da.CommandText = "select clanID.} set { _telefon = value.Data. SqlCommand sqlCom = new SqlCommand().SelectCommand = sqlCom. } } public bool Pol { get { return _pol. . sqlCom. System.pol from Clan". System. } set { _pol = value. sqlCom.Generic.Fill(dt). System.Linq. da.Connection = kon.Text.Collections. SqlDataAdapter da = new SqlDataAdapter(). System.ime + ' ' +prezime as imeprezime.adresa. Konekcija kon = new Konekcija().Data. public DataTable IscitajClanove() { try { DataTable dt = new DataTable(). System.ConnectionString().telefon.jmbg. } } } } Klasa ClanProvider using using using using using using System.SqlClient.

return dt.prezime.Add("@ime". da. SqlCommand sqlCom = new SqlCommand(). sqlCom. SqlDbType. if (RedClan["Telefon"] != null) cl. sqlCom. } catch { throw.Add("@jmbg".Pol = Convert.Value = cl.CommandText = "insert into clan (ime.Ime = RedClan["Ime"].@prezime.Fill(dt).NVarChar). } } public List<Clan> IscitajListuClanova() { try { List<Clan> Clanovi = new List<Clan>(). cl.pol) values(@ime. DataTable dt = new DataTable().Prezime = RedClan["Prezime"].ToString()). SqlDataAdapter da = new SqlDataAdapter(). cl. sqlCom.Parse(RedClan["ClanID"]. Konekcija kon = new Konekcija().Rows) { Clan cl = new Clan(). } return Clanovi.jmbg.Ime.NVarChar). sqlCom.Value = cl.ConnectionString(). sqlCom.Parameters. da.ToString().ToString().CommandText = "select * from Clan". } } public void UbaciClana(Clan cl) { try { Konekcija kon = new Konekcija(). sqlCom.SelectCommand = sqlCom.@jmbg. foreach (DataRow RedClan in dt. if (RedClan["Pol"] != null) cl.Jmbg. sqlCom. Clanovi. } catch { throw. SqlCommand sqlCom = new SqlCommand().Add("@prezime". cl.Parameters.ToBoolean(RedClan["Pol"]).telefon.ToString().ToString(). sqlCom.NVarChar). SqlDbType.ConnectionString().@telefon.@pol)".Value = cl. sqlCom.Parameters["@ime"].Telefon = RedClan["Telefon"].Adresa = RedClan["Adresa"].Parameters["@jmbg"].Connection = kon.Prezime.Parameters["@prezime"]. SqlDbType.Parameters. sqlCom. cl.@adresa. .Add(cl).ToString().ClanID = int.adresa.Jmbg = RedClan["Jmbg"].Connection = kon. cl.

sqlCom.Parameters.Value = cl.Parameters["@adresa"].Add("@ime". sqlCom. sqlCom.Parameters["@adresa"].prezime=@prezime. sqlCom. } catch { } finally { kon.Value = cl. sqlCom. sqlCom.NVarChar).ExecuteNonQuery().Add("@prezime".Add("@adresa".pol=@pol where clanid=@clanid".Value = cl.Parameters["@pol"]. SqlDbType.Parameters["@prezime"].Add("@adresa".Parameters["@pol"].NVarChar).NVarChar).Pol. } } public void IzmeniClana(Clan cl) { try { Konekcija kon = new Konekcija().Value = cl. sqlCom.Value = cl.sqlCom.Add("@jmbg". sqlCom.NVarChar). SqlDbType.ClanID.NVarChar).CloseConnection(). SqlDbType. sqlCom.Prezime.ExecuteNonQuery(). SqlDbType.Value = cl.Adresa. sqlCom. sqlCom.Parameters["@telefon"].Parameters.Ime.NVarChar).OpenConnection().NVarChar).Add("@pol".Value = cl.Value = cl. sqlCom. SqlDbType.Adresa. } } . sqlCom.ConnectionString(). kon. sqlCom.Value = cl.Parameters. sqlCom.Add("@telefon". sqlCom.Parameters.CloseConnection().Telefon.Parameters["@jmbg"].telefon=@telefon.Jmbg.Parameters. sqlCom.Add("@clanid". sqlCom. sqlCom. SqlDbType.Parameters. SqlDbType. kon.Telefon.Parameters.CommandText = "update clan set ime=@ime.NVarChar).NVarChar).Add("@telefon".Parameters.Parameters["@clanid"]. sqlCom.Value = cl.Add("@pol". } catch { } finally { kon. SqlDbType. SqlDbType.Parameters.Parameters["@telefon"].Parameters.adresa=@adresa. SqlDbType.Connection = kon. SqlCommand sqlCom = new SqlCommand(). sqlCom.NVarChar).OpenConnection().Parameters["@ime"]. sqlCom. sqlCom.jmbg=@jmbg.Telefon.

sqlCom. sqlCom.ExecuteNonQuery().CommandText = "delete from clan where clanid = @clanid".Value = ClanID. sqlCom. System.Add("@clanID". List<Clan> lc = new List<Clan>().Text. } catch { throw.Forms. sqlCom. SqlDbType. System.ConnectionString().Drawing. bool noviclan = false.public void IzbrisiClana(int ClanID) { Konekcija kon = new Konekcija(). System.Parameters. SqlCommand sqlCom = new SqlCommand().Linq.Generic. } ClanProvider cp = new ClanProvider(). System. private void Form1_Load(object sender.cs using using using using using using using using System.Data. System. EventArgs e) .Parameters["@clanID"]. } } } } Kod forme – form1.CloseConnection(). System. } finally { kon.OpenConnection(). System.Connection = kon.NVarChar).ComponentModel. namespace Vezba6Novo { public partial class Form1 : Form { public Form1() { InitializeComponent().Collections. try { kon.Windows. sqlCom.

ValueMember = "ClanID". } private void PonisiDetalje() { txtIme. txtAdresa.Enabled = !dozvola.Text = c.Jmbg. txtAdresa.Text = "". if (lbClanovi. if (c. return null. lbClanovi.SelectedIndex > -1) { int ClanID = Convert.Ime.IscitajListuClanova(). } private Clan NadjiClanaUListi(int ClanID) { foreach (Clan c in lc) if (c.Enabled = !dozvola. txtTelefon.{ IscitajPodatkeIzBaze(). DozvoliIzmenu(false).Text = "".Enabled = !dozvola. lbClanovi. txtTelefon.IscitajClanove().Checked = true.Text = "".Text = "". } private void IscitajPodatkeIzBaze() { lc = cp. lbClanovi.ClanID == ClanID) return c. txtJmbg. EventArgs e) { DozvoliIzmenu(true). btnObrisi.Prezime. . lbClanovi.Adresa. btnIzmeni. txtPrezime.Enabled = dozvola.Pol == true) rbMusko.DisplayMember = "ImePrezime". txtIme.Enabled = !dozvola. } } private void DozvoliIzmenu(bool dozvola) { gbDetalji.Text = c.Text = c. rbMusko. } private void btnDodaj_Click(object sender. EventArgs e) { PonisiDetalje().Text = "".Checked = true. txtJmbg.Adresa.Text = c. Clan c = NadjiClanaUListi(ClanID).ToInt32(lbClanovi.SelectedValue). } private void lbClanovi_SelectedIndexChanged(object sender.DataSource = cp. else rbZensko.Text = c. txtPrezime.Checked = true. btnDodaj.

Information).Checked) c. c. PonisiDetalje(). } } private void btnPotvrdi_Click(object sender.Pol = true. "Niste uneli ime!").Text. MessageBox.IzmeniClana(c).Trim() == "") { errorProvider1.Trim() == "") { errorProvider1. EventArgs e) { errorProvider1. } if (txtJmbg. "Niste uneli jmbg!").Prezime = txtPrezime. if (txtIme. try { if (noviclan) { cp. "Potvrda".Adresa = txtAdresa. if (rbMusko.SelectedIndex > -1) { DozvoliIzmenu(true). noviclan = false.noviclan = true. } IscitajPodatkeIzBaze(). return.ToInt32(lbClanovi. c. c. DozvoliIzmenu(false). } else { c.Text. return. MessageBoxButtons.SetError(txtIme. MessageBoxIcon. EventArgs e) { if (lbClanovi.SelectedValue).Clear(). return.Text.Text.UbaciClana(c).Trim() == "") { errorProvider1.Telefon = txtTelefon.Text.SetError(txtPrezime.SetError(txtJmbg.Jmbg = txtJmbg.ClanID = Convert. c.Pol = false. "Niste uneli prezime!").Show("Uspesno ste azurirali clana!". } catch .Ime = txtIme. } private void btnIzmeni_Click(object sender. } Clan c = new Clan(). cp.OK.Text. c. } if (txtPrezime. else c.Text.Text.

MessageBoxIcon.Show("Uspesno ste obrisali clana!". MessageBoxButtons. MessageBoxButtons.Information). FormClosingEventArgs e) { if (MessageBox.Show("Doslo je do greske!". EventArgs e) { DozvoliIzmenu(false).OK.Yes) { try { int ClanID = Convert. EventArgs e) { if (lbClanovi.YesNo.Question) == DialogResult.Warning). MessageBoxIcon. "Potvrda".Cancel = true.SelectedIndex > -1) { if (MessageBox. MessageBoxIcon.No) e. } } } . "Brisanje".YesNo. MessageBoxButtons.Question) == DialogResult. "Izlaz". EventArgs e) { IscitajPodatkeIzBaze(). "Greska". "Greska". MessageBoxButtons. MessageBox. MessageBoxIcon. cp.SelectedValue).Show("Doslo je do greske!".OK.Show("Da li ste sigurni da zelite da izadjete?".IzbrisiClana(ClanID).{ MessageBox. IscitajPodatkeIzBaze(). } private void btnOsveziPodatke_Click(object sender. } } } } private void Form1_FormClosing(object sender. } catch { MessageBox. MessageBoxButtons.OK.Show("Da li ste sigurni?". } private void btnObrisi_Click(object sender. } } private void btnOdustani_Click(object sender. MessageBoxIcon.ToInt32(lbClanovi.Warning).