public Zgrada getZgradaID(int _id) { connection(); Zgrada zgrada = new Zgrada(); con.ConnectionString = str; using (MySqlCommand cmd = new MySqlCommand()) { cmd.Connection = con; cmd.CommandText = "SELECT id_zgrada, naziv, ulica, grad, broj_stanara FROM Zgrada WHERE id=+@id"; cmd.CommandType = CommandType.Text; cmd.Parameters.AddWithValue("@id", _id); con.Open(); using (MySqlDataReader sdr = cmd.ExecuteReader()) { if (sdr.HasRows) { while (sdr.Read()) { zgrada = new Zgrada() { id_zgrada = Convert.ToInt32(sdr["id_zgrada"]), naziv = sdr["naziv"].ToString(), ulica = sdr["ulica"].ToString(), grad = sdr["grad"].ToString(), broj_stanara = Convert.ToInt32(sdr["broj_stanara"]) }; } } } con.Close(); } return(zgrada); }
public string updateZgrada(Zgrada data) { try { connection(); using (MySqlCommand cmd = new MySqlCommand()) { cmd.Connection = con; cmd.CommandText = "UPDATE zgrada SET naziv = @naziv, ulica = @ulica, grad = @grad, broj_stanara = @broj_stanara WHERE id = @id"; cmd.CommandType = CommandType.Text; cmd.Parameters.AddWithValue("@id", data.id); cmd.Parameters.AddWithValue("@naziv", data.naziv); cmd.Parameters.AddWithValue("@ulica", data.ulica); cmd.Parameters.AddWithValue("@grad", data.grad); cmd.Parameters.AddWithValue("@broj_stanara", data.broj_stanara); con.Open(); cmd.ExecuteNonQuery(); con.Close(); return("OK"); } } catch (Exception err) { con.Close(); return("Error"); } }
public string addZgrada(Zgrada data) { try { connection(); using (MySqlCommand cmd = new MySqlCommand()) { cmd.Connection = con; cmd.CommandText = "INSERT INTO zgrada(id_zgrada,naziv,ulica,grad,broj_stanara) " + " VALUES(@id_zgrada,@naziv,@ulica,@grad,@broj_stanara)"; cmd.CommandType = CommandType.Text; cmd.Parameters.AddWithValue("@id_zgrada", data.id_zgrada); cmd.Parameters.AddWithValue("@naziv", data.naziv); cmd.Parameters.AddWithValue("@ulica", data.ulica); cmd.Parameters.AddWithValue("@grad", data.grad); cmd.Parameters.AddWithValue("@broj_stanara", data.broj_stanara); con.Open(); cmd.ExecuteNonQuery(); con.Close(); return("OK"); } } catch (Exception err) { con.Close(); return("Error"); } }
public List <Zgrada> GetZgrada_2(string searchData) { connection(); List <Zgrada> zgrade = new List <Zgrada>(); con.ConnectionString = str; using (MySqlCommand cmd = new MySqlCommand()) { cmd.Connection = con; cmd.CommandText = "SELECT id_zgrada, naziv, ulica, grad, broj_stanara FROM Zgrada WHERE " + " naziv like '%" + searchData + "%' " + " ORDER BY id ASC"; con.Open(); using (MySqlDataReader sdr = cmd.ExecuteReader()) { if (sdr.HasRows) { while (sdr.Read()) { Zgrada emp = new Zgrada() { id_zgrada = Convert.ToInt32(sdr["id_zgrada"]), naziv = sdr["naziv"].ToString(), ulica = sdr["ulica"].ToString(), grad = sdr["grad"].ToString(), broj_stanara = Convert.ToInt32(sdr["broj_stanara"]) }; if (emp.naziv.Length > 0 && emp.ulica.Length > 0 && emp.grad.Length > 0 && emp.broj_stanara.Length > 0) { gradovi.Add(emp); } } } } con.Close(); } return(zgrade); }