//DeleteOB
        public bool _DeleteOb(Ob_DH_LoaiDH ob, SqlConnection conn)
        {
            if (conn.ConnectionString == "")
            {
                return(false);
            }
            conn.Open();
            SqlCommand command = new SqlCommand
            {
                Connection  = conn,
                CommandType = CommandType.Text,
                CommandText = @"Delete From DH_LoaiDH Where ID_LoaiDH=@ID_LoaiDH"
            };

            command.Parameters.Add(new SqlParameter("ID_LoaiDH", ob.ID_LOAIDH));
            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);
        }
        //InsertobGetId
        public int _InsertObGetId(Ob_DH_LoaiDH ob, SqlConnection conn)
        {
            int num = 0;

            if (conn.ConnectionString != "")
            {
                conn.Open();
                SqlCommand command = new SqlCommand
                {
                    Connection  = conn,
                    CommandType = CommandType.Text,
                    CommandText = @"Insert Into DH_LoaiDH (MaLoaiDH, TenLoaiDH, HopDong, TenMua, ID_BangGia, Active) Values(@MaLoaiDH, @TenLoaiDH, @HopDong, @TenMua, @ID_BangGia, @Active)Select @@IDENTITY"
                };
                if (ob.MALOAIDH.Trim() == "")
                {
                    command.Parameters.Add("MaLoaiDH", SqlDbType.VarChar).Value = DBNull.Value;
                }
                else
                {
                    command.Parameters.Add("MaLoaiDH", SqlDbType.VarChar).Value = ob.MALOAIDH;
                }
                if (ob.TENLOAIDH.Trim() == "")
                {
                    command.Parameters.Add("TenLoaiDH", SqlDbType.NVarChar).Value = DBNull.Value;
                }
                else
                {
                    command.Parameters.Add("TenLoaiDH", SqlDbType.NVarChar).Value = ob.TENLOAIDH;
                }
                command.Parameters.Add("HopDong", SqlDbType.Bit).Value = ob.HOPDONG;
                if (ob.TENMUA.Trim() == "")
                {
                    command.Parameters.Add("TenMua", SqlDbType.NVarChar).Value = DBNull.Value;
                }
                else
                {
                    command.Parameters.Add("TenMua", SqlDbType.NVarChar).Value = ob.TENMUA;
                }
                command.Parameters.Add("ID_BangGia", SqlDbType.Int).Value = ob.ID_BANGGIA;
                command.Parameters.Add("Active", SqlDbType.Bit).Value     = ob.ACTIVE;
                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);
        }
        //ListWhere_sp
        public List <Ob_DH_LoaiDH> _ListWhere_sp(string StoredName, SqlParameter[] ArrayParameter, SqlConnection conn)
        {
            if (conn.ConnectionString == "")
            {
                return(null);
            }
            conn.Open();
            List <Ob_DH_LoaiDH> list    = new List <Ob_DH_LoaiDH>();
            SqlCommand          command = new SqlCommand
            {
                Connection  = conn,
                CommandType = CommandType.Text,
                CommandText = StoredName
            };

            command.Parameters.AddRange(ArrayParameter);
            SqlDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
                Ob_DH_LoaiDH ob = new Ob_DH_LoaiDH();
                if (reader[0] != DBNull.Value)
                {
                    ob.ID_LOAIDH = Convert.ToInt32(reader[0]);
                }
                if (reader[1] != DBNull.Value)
                {
                    ob.MALOAIDH = Convert.ToString(reader[1]);
                }
                if (reader[2] != DBNull.Value)
                {
                    ob.TENLOAIDH = Convert.ToString(reader[2]);
                }
                if (reader[3] != DBNull.Value)
                {
                    ob.HOPDONG = Convert.ToBoolean(reader[3]);
                }
                if (reader[4] != DBNull.Value)
                {
                    ob.TENMUA = Convert.ToString(reader[4]);
                }
                if (reader[5] != DBNull.Value)
                {
                    ob.ID_BANGGIA = Convert.ToInt32(reader[5]);
                }
                if (reader[6] != DBNull.Value)
                {
                    ob.ACTIVE = Convert.ToBoolean(reader[6]);
                }
                list.Add(ob);
            }
            command.Dispose();
            command = null;
            reader.Dispose();
            reader = null;
            conn.Close();
            return(list);
        }
        //ListAll
        public List <Ob_DH_LoaiDH> _ListAll(SqlConnection conn)
        {
            if (conn.ConnectionString == "")
            {
                return(null);
            }
            conn.Open();
            List <Ob_DH_LoaiDH> list    = new List <Ob_DH_LoaiDH>();
            SqlCommand          command = new SqlCommand
            {
                Connection  = conn,
                CommandType = CommandType.Text,
                CommandText = @"Select ID_LoaiDH, MaLoaiDH, TenLoaiDH, HopDong, TenMua, ID_BangGia, Active From DH_LoaiDH"
            };
            SqlDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
                Ob_DH_LoaiDH ob = new Ob_DH_LoaiDH();
                if (reader[0] != DBNull.Value)
                {
                    ob.ID_LOAIDH = Convert.ToInt32(reader[0]);
                }
                if (reader[1] != DBNull.Value)
                {
                    ob.MALOAIDH = Convert.ToString(reader[1]);
                }
                if (reader[2] != DBNull.Value)
                {
                    ob.TENLOAIDH = Convert.ToString(reader[2]);
                }
                if (reader[3] != DBNull.Value)
                {
                    ob.HOPDONG = Convert.ToBoolean(reader[3]);
                }
                if (reader[4] != DBNull.Value)
                {
                    ob.TENMUA = Convert.ToString(reader[4]);
                }
                if (reader[5] != DBNull.Value)
                {
                    ob.ID_BANGGIA = Convert.ToInt32(reader[5]);
                }
                if (reader[6] != DBNull.Value)
                {
                    ob.ACTIVE = Convert.ToBoolean(reader[6]);
                }
                list.Add(ob);
            }
            command.Dispose();
            command = null;
            reader.Dispose();
            reader = null;
            conn.Close();
            return(list);
        }
Example #5
0
 public Ob_DH_LoaiDH(Ob_DH_LoaiDH newOb)
 {
     id_loaidh  = newOb.ID_LOAIDH;
     maloaidh   = newOb.MALOAIDH;
     tenloaidh  = newOb.TENLOAIDH;
     hopdong    = newOb.HOPDONG;
     tenmua     = newOb.TENMUA;
     id_banggia = newOb.ID_BANGGIA;
     active     = newOb.ACTIVE;
 }
        //UpdateOb ob
        public bool _UpdateOb(Ob_DH_LoaiDH ob, SqlConnection conn)
        {
            if (conn.ConnectionString == "")
            {
                return(false);
            }
            conn.Open();
            SqlCommand command = new SqlCommand {
                Connection  = conn,
                CommandType = CommandType.Text,
                CommandText = @"Update DH_LoaiDH Set MaLoaiDH=@MaLoaiDH, TenLoaiDH=@TenLoaiDH, HopDong=@HopDong, TenMua=@TenMua, ID_BangGia=@ID_BangGia, Active=@Active Where ID_LoaiDH=@ID_LoaiDH"
            };

            command.Parameters.Add("ID_LoaiDH", SqlDbType.Int).Value = ob.ID_LOAIDH;
            if (ob.MALOAIDH.Trim() == "")
            {
                command.Parameters.Add("MaLoaiDH", SqlDbType.VarChar).Value = DBNull.Value;
            }
            else
            {
                command.Parameters.Add("MaLoaiDH", SqlDbType.VarChar).Value = ob.MALOAIDH;
            }
            if (ob.TENLOAIDH.Trim() == "")
            {
                command.Parameters.Add("TenLoaiDH", SqlDbType.NVarChar).Value = DBNull.Value;
            }
            else
            {
                command.Parameters.Add("TenLoaiDH", SqlDbType.NVarChar).Value = ob.TENLOAIDH;
            }
            command.Parameters.Add("HopDong", SqlDbType.Bit).Value = ob.HOPDONG;
            if (ob.TENMUA.Trim() == "")
            {
                command.Parameters.Add("TenMua", SqlDbType.NVarChar).Value = DBNull.Value;
            }
            else
            {
                command.Parameters.Add("TenMua", SqlDbType.NVarChar).Value = ob.TENMUA;
            }
            command.Parameters.Add("ID_BangGia", SqlDbType.Int).Value = ob.ID_BANGGIA;
            command.Parameters.Add("Active", SqlDbType.Bit).Value     = ob.ACTIVE;
            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);
        }
        //GetObWhere
        public Ob_DH_LoaiDH _GetObWhere(string keyword, SqlConnection conn)
        {
            if (conn.ConnectionString == "")
            {
                return(null);
            }
            conn.Open();
            Ob_DH_LoaiDH ob      = new Ob_DH_LoaiDH();
            SqlCommand   command = new SqlCommand
            {
                Connection  = conn,
                CommandType = CommandType.Text,
                CommandText = @"Select ID_LoaiDH, MaLoaiDH, TenLoaiDH, HopDong, TenMua, ID_BangGia, Active From DH_LoaiDH Where " + keyword + ""
            };
            SqlDataReader reader = command.ExecuteReader();
            int           num    = 0;

            while (reader.Read())
            {
                num++;
                if (reader[0] != DBNull.Value)
                {
                    ob.ID_LOAIDH = Convert.ToInt32(reader[0]);
                }
                if (reader[1] != DBNull.Value)
                {
                    ob.MALOAIDH = Convert.ToString(reader[1]);
                }
                if (reader[2] != DBNull.Value)
                {
                    ob.TENLOAIDH = Convert.ToString(reader[2]);
                }
                if (reader[3] != DBNull.Value)
                {
                    ob.HOPDONG = Convert.ToBoolean(reader[3]);
                }
                if (reader[4] != DBNull.Value)
                {
                    ob.TENMUA = Convert.ToString(reader[4]);
                }
                if (reader[5] != DBNull.Value)
                {
                    ob.ID_BANGGIA = Convert.ToInt32(reader[5]);
                }
                if (reader[6] != DBNull.Value)
                {
                    ob.ACTIVE = Convert.ToBoolean(reader[6]);
                }
            }
            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);
        }