public List <LichchamsocDTO> selectByKeyWord(string sKeyword) { string query = string.Empty; query += " SELECT [machamsoc],[thoigian],[mavattu],[madonvitinh],[soluong],[ghichu],[macay],[ngaylaplich]"; query += " FROM [LICHCHAMSOC]"; query += " WHERE ([macay] LIKE CONCAT('%',@sKeyword,'%'))"; List <LichchamsocDTO> lsLich = new List <LichchamsocDTO>(); using (SqlConnection con = new SqlConnection(ConnectionString)) { using (SqlCommand cmd = new SqlCommand()) { cmd.Connection = con; cmd.CommandType = System.Data.CommandType.Text; cmd.CommandText = query; cmd.Parameters.AddWithValue("@sKeyword", sKeyword); try { con.Open(); SqlDataReader reader = null; reader = cmd.ExecuteReader(); if (reader.HasRows == true) { while (reader.Read()) { LichchamsocDTO lich = new LichchamsocDTO(); lich.MaChamSocPT = reader["machamsoc"].ToString(); lich.MaVatTuPT = int.Parse(reader["mavattu"].ToString()); lich.MaDonViTinhPT = int.Parse(reader["madonvitinh"].ToString()); lich.SoLuongPT = int.Parse(reader["soluong"].ToString()); lich.ThoiGianPT = long.Parse(reader["thoigian"].ToString()); lich.GhiChuPT = reader["ghichu"].ToString(); lich.MaCayPT = reader["macay"].ToString(); lich.NgayLapLichPT = DateTime.Parse(reader["ngaylaplich"].ToString()); lsLich.Add(lich); } } con.Close(); con.Dispose(); } catch (Exception ex) { con.Close(); return(null); } } } return(lsLich); }
private void LenLichBT_Click(object sender, EventArgs e) { if (gioTB.Text == "" || PhutTB.Text == "" || maChamSocTB.Text == "" || maCayTB.Text == "" || ghiChuTB.Text == "" || soLuongTB.Text == "") { MessageBox.Show("Vui Lòng nhập đây đủ thông tin trước khi thực hiện"); return; } //1. Map data from GUI DateTime ngaylap = new DateTime(); ngaylap = DateTime.Now; TimeSpan time = new TimeSpan(int.Parse(gioTB.Text.ToString()), int.Parse(PhutTB.Text.ToString()), 0); long timeticks = time.Ticks; LichchamsocDTO lichDTO = new LichchamsocDTO(); lichDTO.MaChamSocPT = maChamSocTB.Text; lichDTO.MaCayPT = maCayTB.Text; lichDTO.GhiChuPT = ghiChuTB.Text; lichDTO.MaVatTuPT = int.Parse(comboBoxMaVatTu.SelectedValue.ToString()); lichDTO.SoLuongPT = int.Parse(soLuongTB.Text.ToString()); lichDTO.NgayLapLichPT = ngaylap; lichDTO.MaDonViTinhPT = listDonVi[selectedLoaiVatTu - 1].MaDonViTinhPT; lichDTO.ThoiGianPT = timeticks; //lichDTO.DonViTinhPT = listDonVi[selectedLoaiVatTu - 1].TenDonViTinhPT; //lichDTO.TenVatTuPT = listVatTu[selectedLoaiVatTu - 1].TenVatTuPT; //2. Kiểm tra data hợp lệ or not //3. Thêm vào DB bool result = lichBus.themlich(lichDTO); if (result == false) { MessageBox.Show("Thêm Lịch thất bại. Vui lòng kiểm tra lại dữ liệu"); } else { MessageBox.Show("Thêm Lịch thành công"); } }
public bool themLich(LichchamsocDTO lich) { string query = string.Empty; query += "INSERT INTO [LICHCHAMSOC] ([machamsoc],[thoigian],[mavattu],[madonvitinh],[soluong],[ghichu],[macay],[ngaylaplich])"; query += "VALUES (@machamsoc,@thoigian,@mavattu,@madonvitinh,@soluong,@ghichu,@macay,@ngaylaplich)"; using (SqlConnection con = new SqlConnection(ConnectionString)) { using (SqlCommand cmd = new SqlCommand()) { cmd.Connection = con; cmd.CommandType = System.Data.CommandType.Text; cmd.CommandText = query; cmd.Parameters.AddWithValue("@machamsoc", lich.MaChamSocPT); cmd.Parameters.AddWithValue("@thoigian", lich.ThoiGianPT); cmd.Parameters.AddWithValue("@mavattu", lich.MaVatTuPT); cmd.Parameters.AddWithValue("@madonvitinh", lich.MaDonViTinhPT); cmd.Parameters.AddWithValue("@soluong", lich.SoLuongPT); cmd.Parameters.AddWithValue("@ghichu", lich.GhiChuPT); cmd.Parameters.AddWithValue("@macay", lich.MaCayPT); cmd.Parameters.AddWithValue("@ngaylaplich", lich.NgayLapLichPT); try { con.Open(); cmd.ExecuteNonQuery(); con.Close(); con.Dispose(); } catch (Exception ex) { con.Close(); return(false); } } } return(true); }
public bool themlich(LichchamsocDTO lich) { bool result = lichDAL.themLich(lich); return(result); }