0% found this document useful (0 votes)
174 views10 pages

Baza Podataka

The document discusses database communication and CRUD operations using C# and SQL Server. It includes classes for database connection, member/user, and a member data provider. The member provider class contains methods for retrieving, inserting, updating, and deleting member data from the database. A Windows form is also included that allows viewing, adding, editing, and removing members using the member provider class.

Uploaded by

Halimovic Almir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
174 views10 pages

Baza Podataka

The document discusses database communication and CRUD operations using C# and SQL Server. It includes classes for database connection, member/user, and a member data provider. The member provider class contains methods for retrieving, inserting, updating, and deleting member data from the database. A Windows form is also included that allows viewing, adding, editing, and removing members using the member provider class.

Uploaded by

Halimovic Almir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 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");
}

public SqlConnection ConnectionString()


{
return con;
}
public void OpenConnection()
{
if (con.State == ConnectionState.Closed)
con.Open();
}
public void CloseConnection()
{
if (con.State == ConnectionState.Open)
con.Close();
}
}
}

Klasa Clan

using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;

namespace Vezba6Novo
{
public class Clan
{
private int _clanID;
private string _ime;
private string _prezime;
private string _jmbg;
private string _adresa;
private string _telefon;
private bool _pol;
public int ClanID
{
get
{
return _clanID;
}
set
{
_clanID = value;
}
}

public string Ime


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

}
set
{
_telefon = value;
}
}
public bool Pol
{
get
{
return _pol;
}
set
{
_pol = value;
}
}
}

Klasa ClanProvider

using
using
using
using
using
using

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

namespace Vezba6Novo
{
public class ClanProvider
{
Konekcija kon = new Konekcija();
public DataTable IscitajClanove()
{
try
{
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter();
Konekcija kon = new Konekcija();
SqlCommand sqlCom = new SqlCommand();
sqlCom.Connection = kon.ConnectionString();
sqlCom.CommandText = "select clanID,ime + ' ' +prezime as
imeprezime,jmbg,adresa,telefon,pol from Clan";
da.SelectCommand = sqlCom;
da.Fill(dt);

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

sqlCom.Parameters.Add("@adresa", SqlDbType.NVarChar);
sqlCom.Parameters["@adresa"].Value = cl.Adresa;
sqlCom.Parameters.Add("@telefon", SqlDbType.NVarChar);
sqlCom.Parameters["@telefon"].Value = cl.Telefon;
sqlCom.Parameters.Add("@pol", SqlDbType.NVarChar);
sqlCom.Parameters["@pol"].Value = cl.Pol;
kon.OpenConnection();
sqlCom.ExecuteNonQuery();

}
catch
{

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

}
catch
{

}
finally
{
kon.CloseConnection();
}
}

public void IzbrisiClana(int ClanID)


{
Konekcija kon = new Konekcija();
SqlCommand sqlCom = new SqlCommand();
sqlCom.Connection = kon.ConnectionString();
sqlCom.CommandText = "delete from clan where clanid = @clanid";
sqlCom.Parameters.Add("@clanID", SqlDbType.NVarChar);
sqlCom.Parameters["@clanID"].Value = ClanID;
try
{
kon.OpenConnection();
sqlCom.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
kon.CloseConnection();
}
}
}
}

Kod forme form1.cs

using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;

namespace Vezba6Novo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ClanProvider cp = new ClanProvider();
List<Clan> lc = new List<Clan>();
bool noviclan = false;
private void Form1_Load(object sender, EventArgs e)

IscitajPodatkeIzBaze();
DozvoliIzmenu(false);

}
private void IscitajPodatkeIzBaze()
{
lc = cp.IscitajListuClanova();
lbClanovi.ValueMember = "ClanID";
lbClanovi.DisplayMember = "ImePrezime";
lbClanovi.DataSource = cp.IscitajClanove();

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

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

MessageBox.Show("Doslo je do greske!", "Greska", MessageBoxButtons.OK,


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

You might also like