//InsertobGetId public int _InsertObGetId(Ob_POS_KhuyenMaiDoiTuong ob, SqlConnection conn) { int num = 0; if (conn.ConnectionString != "") { conn.Open(); SqlCommand command = new SqlCommand { Connection = conn, CommandType = CommandType.Text, CommandText = @"Insert Into POS_KhuyenMaiDoiTuong (ID_KhuyenMai, ID_DoiTuong) Values(@ID_KhuyenMai, @ID_DoiTuong)Select @@IDENTITY" }; command.Parameters.Add("ID_KhuyenMai", SqlDbType.Int).Value = ob.ID_KHUYENMAI; command.Parameters.Add("ID_DoiTuong", SqlDbType.Int).Value = ob.ID_DOITUONG; try { num = Convert.ToInt32(command.ExecuteScalar()); } catch (Exception exception) { MessageBox.Show("Không thể thêm dữ liệu. \r\n" + exception.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error); command.Dispose(); command = null; conn.Close(); return(num); } command.Dispose(); command = null; conn.Close(); } return(num); }
//DeleteOB public bool _DeleteOb(Ob_POS_KhuyenMaiDoiTuong ob, SqlConnection conn) { if (conn.ConnectionString == "") { return(false); } conn.Open(); SqlCommand command = new SqlCommand { Connection = conn, CommandType = CommandType.Text, CommandText = @"Delete From POS_KhuyenMaiDoiTuong Where ID_KhuyenMaiDoiTuong=@ID_KhuyenMaiDoiTuong" }; command.Parameters.Add(new SqlParameter("ID_KhuyenMaiDoiTuong", ob.ID_KHUYENMAIDOITUONG)); try { command.ExecuteNonQuery(); } catch (Exception exception) { MessageBox.Show("Không thể xóa dữ liệu. \r\n" + exception.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error); command.Dispose(); command = null; conn.Close(); return(false); } command.Dispose(); command = null; conn.Close(); return(true); }
//Insert ob public bool _InsertOb(Ob_POS_KhuyenMaiDoiTuong ob, SqlConnection conn) { if (conn.ConnectionString == "") { return(false); } conn.Open(); SqlCommand command = new SqlCommand { Connection = conn, CommandType = CommandType.Text, CommandText = @"Insert Into POS_KhuyenMaiDoiTuong (ID_KhuyenMai, ID_DoiTuong) Values(@ID_KhuyenMai, @ID_DoiTuong)" }; command.Parameters.Add("ID_KhuyenMai", SqlDbType.Int).Value = ob.ID_KHUYENMAI; command.Parameters.Add("ID_DoiTuong", SqlDbType.Int).Value = ob.ID_DOITUONG; try { command.ExecuteNonQuery(); } catch (Exception exception) { MessageBox.Show("Không thể thêm dữ liệu. \r\n" + exception.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error); command.Dispose(); command = null; conn.Close(); return(false); } command.Dispose(); command = null; conn.Close(); return(true); }
//GetObWhere public Ob_POS_KhuyenMaiDoiTuong _GetObWhere(string keyword, SqlConnection conn) { if (conn.ConnectionString == "") { return(null); } conn.Open(); Ob_POS_KhuyenMaiDoiTuong ob = new Ob_POS_KhuyenMaiDoiTuong(); SqlCommand command = new SqlCommand { Connection = conn, CommandType = CommandType.Text, CommandText = @"Select ID_KhuyenMaiDoiTuong, ID_KhuyenMai, ID_DoiTuong From POS_KhuyenMaiDoiTuong Where " + keyword + "" }; SqlDataReader reader = command.ExecuteReader(); int num = 0; while (reader.Read()) { num++; if (reader[0] != DBNull.Value) { ob.ID_KHUYENMAIDOITUONG = Convert.ToInt64(reader[0]); } if (reader[1] != DBNull.Value) { ob.ID_KHUYENMAI = Convert.ToInt32(reader[1]); } if (reader[2] != DBNull.Value) { ob.ID_DOITUONG = Convert.ToInt32(reader[2]); } } if (num == 0) { command.Dispose(); command = null; reader.Dispose(); reader = null; conn.Close(); return(null); } command.Dispose(); command = null; reader.Dispose(); reader = null; conn.Close(); return(ob); }
//ListWhere_sp public List <Ob_POS_KhuyenMaiDoiTuong> _ListWhere_sp(string StoredName, SqlParameter[] ArrayParameter, SqlConnection conn) { if (conn.ConnectionString == "") { return(null); } conn.Open(); List <Ob_POS_KhuyenMaiDoiTuong> list = new List <Ob_POS_KhuyenMaiDoiTuong>(); SqlCommand command = new SqlCommand { Connection = conn, CommandType = CommandType.Text, CommandText = StoredName }; command.Parameters.AddRange(ArrayParameter); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { Ob_POS_KhuyenMaiDoiTuong ob = new Ob_POS_KhuyenMaiDoiTuong(); if (reader[0] != DBNull.Value) { ob.ID_KHUYENMAIDOITUONG = Convert.ToInt64(reader[0]); } if (reader[1] != DBNull.Value) { ob.ID_KHUYENMAI = Convert.ToInt32(reader[1]); } if (reader[2] != DBNull.Value) { ob.ID_DOITUONG = Convert.ToInt32(reader[2]); } list.Add(ob); } command.Dispose(); command = null; reader.Dispose(); reader = null; conn.Close(); return(list); }
//UpdateOb ob public bool _UpdateOb(Ob_POS_KhuyenMaiDoiTuong ob, SqlConnection conn) { if (conn.ConnectionString == "") { return(false); } conn.Open(); SqlCommand command = new SqlCommand { Connection = conn, CommandType = CommandType.Text, CommandText = @"Update POS_KhuyenMaiDoiTuong Set ID_KhuyenMai=@ID_KhuyenMai, ID_DoiTuong=@ID_DoiTuong Where ID_KhuyenMaiDoiTuong=@ID_KhuyenMaiDoiTuong" }; command.Parameters.Add("ID_KhuyenMaiDoiTuong", SqlDbType.BigInt).Value = ob.ID_KHUYENMAIDOITUONG; command.Parameters.Add("ID_KhuyenMai", SqlDbType.Int).Value = ob.ID_KHUYENMAI; command.Parameters.Add("ID_DoiTuong", SqlDbType.Int).Value = ob.ID_DOITUONG; try { command.ExecuteNonQuery(); } catch (Exception exception) { MessageBox.Show("Không thể cập nhật dữ liệu. \r\n" + exception.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error); command.Dispose(); command = null; conn.Close(); return(false); } if (command.ExecuteNonQuery() == 0) { MessageBox.Show("Không thể cập nhật dữ liệu. \r\n", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error); command.Dispose(); command = null; conn.Close(); return(false); } command.Dispose(); command = null; conn.Close(); return(true); }
//ListAll public List <Ob_POS_KhuyenMaiDoiTuong> _ListAll(SqlConnection conn) { if (conn.ConnectionString == "") { return(null); } conn.Open(); List <Ob_POS_KhuyenMaiDoiTuong> list = new List <Ob_POS_KhuyenMaiDoiTuong>(); SqlCommand command = new SqlCommand { Connection = conn, CommandType = CommandType.Text, CommandText = @"Select ID_KhuyenMaiDoiTuong, ID_KhuyenMai, ID_DoiTuong From POS_KhuyenMaiDoiTuong" }; SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { Ob_POS_KhuyenMaiDoiTuong ob = new Ob_POS_KhuyenMaiDoiTuong(); if (reader[0] != DBNull.Value) { ob.ID_KHUYENMAIDOITUONG = Convert.ToInt64(reader[0]); } if (reader[1] != DBNull.Value) { ob.ID_KHUYENMAI = Convert.ToInt32(reader[1]); } if (reader[2] != DBNull.Value) { ob.ID_DOITUONG = Convert.ToInt32(reader[2]); } list.Add(ob); } command.Dispose(); command = null; reader.Dispose(); reader = null; conn.Close(); return(list); }
public Ob_POS_KhuyenMaiDoiTuong(Ob_POS_KhuyenMaiDoiTuong newOb) { id_khuyenmaidoituong = newOb.ID_KHUYENMAIDOITUONG; id_khuyenmai = newOb.ID_KHUYENMAI; id_doituong = newOb.ID_DOITUONG; }