protected void Page_Load(object sender, EventArgs e) { Zakaznik c = (Zakaznik)Session["login"]; if (!Page.IsPostBack) { Collection <Zbran> zbrane = Zbran.SelectAll(); Collection <Prostor> prostory = Prostor.SelectAll(); DropDownList1.DataSource = zbrane; DropDownList1.DataTextField = "Nazev"; DropDownList1.DataValueField = "Id"; DropDownList2.DataSource = prostory; DropDownList2.DataTextField = "Vzdalenost"; DropDownList2.DataValueField = "Id"; DropDownList1.DataBind(); DropDownList2.DataBind(); } if (IsPostBack) { Bul0056.Aggregates.Rezervace rez = new Bul0056.Aggregates.Rezervace(); rez.Start = DateTime.Parse(TextBox1.Text); rez.Vytvoreni = DateTime.Now; rez.Zakaznik = c; rez.Zbran = Zbran.getById(Int32.Parse(DropDownList1.SelectedValue)); rez.Prostor = Prostor.getById(Int32.Parse(DropDownList2.SelectedValue)); Bul0056.Aggregates.Rezervace.insert(rez); } }
public static Rezervace getById(int id) { Rezervace rez = null; try { using (SqlConnection connection = new SqlConnection(conStr)) { connection.Open(); string sql = "SELECT * FROM Rezervace WHERE idRez=@id;"; using (SqlCommand command = new SqlCommand(sql, connection)) { command.Parameters.AddWithValue("@id", id); using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { rez = new Rezervace(reader.GetInt32(0), reader.GetDateTime(1), reader.GetDateTime(2), Zbran.getById(reader.GetInt32(3)), Zakaznik.getById(reader.GetInt32(4)), Prostor.getById(reader.GetInt32(5))); } } } } } catch (SqlException ex) { Console.WriteLine(ex.ToString()); } return(rez); }
public static int delete(Rezervace Rezervace) { int ret = 0; try { using (SqlConnection connection = new SqlConnection(conStr)) { connection.Open(); string sql = "DELETE FROM Rezervace Where idRez = @idRez"; using (SqlCommand command = new SqlCommand(sql, connection)) { command.Parameters.AddWithValue("@idRez", Rezervace.Id); ret = command.ExecuteNonQuery(); } } } catch (SqlException ex) { Console.WriteLine(ex.ToString()); } return(ret); }
private static void PrepareCommand(SqlCommand command, Rezervace rezervace) { command.Parameters.AddWithValue("@idRez", rezervace.Id); command.Parameters.AddWithValue("@datumVytvoreni", rezervace.Vytvoreni); command.Parameters.AddWithValue("@datumStrelby", rezervace.Start); command.Parameters.AddWithValue("@Zakaznik_idZak", rezervace.Zakaznik.Id); command.Parameters.AddWithValue("@Prostor_idSpr", rezervace.Prostor.Id); command.Parameters.AddWithValue("@Zbran_idZbr", rezervace.Zbran.Id); }
public static int update(Rezervace Rezervace) { int ret = 0; try { using (SqlConnection connection = new SqlConnection(conStr)) { connection.Open(); string sql = "UPDATE Rezervace SET datumVytvoreni=@datumVytvoreni, datumStrelby=@datumStrelby, Zakaznik_idZak=@Zakaznik_idZak, Prostor_idSpr=@Prostor_idSpr, Zbran_idZbr=@Zbran_idZbr WHERE idRez=@idRez"; using (SqlCommand command = new SqlCommand(sql, connection)) { PrepareCommand(command, Rezervace); ret = command.ExecuteNonQuery(); } } } catch (SqlException ex) { Console.WriteLine(ex.ToString()); } return(ret); }
public static int insert(Rezervace Rezervace) { int ret = 0; try { using (SqlConnection connection = new SqlConnection(conStr)) { connection.Open(); string sql = "INSERT INTO Rezervace (datumVytvoreni, datumStrelby, Zakaznik_idZak, Prostor_idSpr, Zbran_idZbr) VALUES (@datumVytvoreni, @datumStrelby, @Zakaznik_idZak, @Prostor_idSpr, @Zbran_idZbr)"; using (SqlCommand command = new SqlCommand(sql, connection)) { PrepareCommand(command, Rezervace); ret = command.ExecuteNonQuery(); } } } catch (SqlException ex) { Console.WriteLine(ex.ToString()); } return(ret); }