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

            command.Parameters.Add(new SqlParameter("ID_KhachHang", ob.ID_KHACHHANG));
            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);
        }
Exemplo n.º 2
0
 public Ob_POS_DM_KhachHang(Ob_POS_DM_KhachHang newOb)
 {
     id_khachhang = newOb.ID_KHACHHANG;
     tenkhachhang = newOb.TENKHACHHANG;
     sodienthoai  = newOb.SODIENTHOAI;
     ngaysinh     = newOb.NGAYSINH;
     gioitinh     = newOb.GIOITINH;
     diachi       = newOb.DIACHI;
     createdtime  = newOb.CREATEDTIME;
     createdby    = newOb.CREATEDBY;
     modifiedtime = newOb.MODIFIEDTIME;
     modifiedby   = newOb.MODIFIEDBY;
 }
        //ListAll
        public List <Ob_POS_DM_KhachHang> _ListAll_sp(string StoredName, SqlConnection conn)
        {
            if (conn.ConnectionString == "")
            {
                return(null);
            }
            conn.Open();
            List <Ob_POS_DM_KhachHang> list = new List <Ob_POS_DM_KhachHang>();
            SqlCommand command = new SqlCommand
            {
                Connection  = conn,
                CommandType = CommandType.Text,
                CommandText = StoredName
            };
            SqlDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
                Ob_POS_DM_KhachHang ob = new Ob_POS_DM_KhachHang();
                if (reader[0] != DBNull.Value)
                {
                    ob.ID_KHACHHANG = Convert.ToString(reader[0]);
                }
                if (reader[1] != DBNull.Value)
                {
                    ob.TENKHACHHANG = Convert.ToString(reader[1]);
                }
                if (reader[2] != DBNull.Value)
                {
                    ob.SODIENTHOAI = Convert.ToString(reader[2]);
                }
                if (reader[3] != DBNull.Value)
                {
                    ob.NGAYSINH = Convert.ToDateTime(reader[3]);
                }
                if (reader[4] != DBNull.Value)
                {
                    ob.GIOITINH = Convert.ToInt32(reader[4]);
                }
                if (reader[5] != DBNull.Value)
                {
                    ob.DIACHI = Convert.ToString(reader[5]);
                }
                if (reader[6] != DBNull.Value)
                {
                    ob.CREATEDTIME = Convert.ToDateTime(reader[6]);
                }
                if (reader[7] != DBNull.Value)
                {
                    ob.CREATEDBY = Convert.ToString(reader[7]);
                }
                if (reader[8] != DBNull.Value)
                {
                    ob.MODIFIEDTIME = Convert.ToDateTime(reader[8]);
                }
                if (reader[9] != DBNull.Value)
                {
                    ob.MODIFIEDBY = Convert.ToString(reader[9]);
                }
                list.Add(ob);
            }
            command.Dispose();
            command = null;
            reader.Dispose();
            reader = null;
            conn.Close();
            return(list);
        }
        //UpdateOb ob
        public bool _UpdateOb(Ob_POS_DM_KhachHang ob, SqlConnection conn)
        {
            if (conn.ConnectionString == "")
            {
                return(false);
            }
            conn.Open();
            SqlCommand command = new SqlCommand {
                Connection  = conn,
                CommandType = CommandType.Text,
                CommandText = @"Update POS_DM_KhachHang Set TenKhachHang=@TenKhachHang, SoDienThoai=@SoDienThoai, NgaySinh=@NgaySinh, GioiTinh=@GioiTinh, DiaChi=@DiaChi, CreatedTime=@CreatedTime, CreatedBy=@CreatedBy, ModifiedTime=@ModifiedTime, ModifiedBy=@ModifiedBy Where ID_KhachHang=@ID_KhachHang"
            };

            command.Parameters.Add("ID_KhachHang", SqlDbType.UniqueIdentifier).Value = new SqlGuid(ob.ID_KHACHHANG);
            if (ob.TENKHACHHANG.Trim() == "")
            {
                command.Parameters.Add("TenKhachHang", SqlDbType.NVarChar).Value = DBNull.Value;
            }
            else
            {
                command.Parameters.Add("TenKhachHang", SqlDbType.NVarChar).Value = ob.TENKHACHHANG;
            }
            if (ob.SODIENTHOAI.Trim() == "")
            {
                command.Parameters.Add("SoDienThoai", SqlDbType.VarChar).Value = DBNull.Value;
            }
            else
            {
                command.Parameters.Add("SoDienThoai", SqlDbType.VarChar).Value = ob.SODIENTHOAI;
            }
            if (ob.NGAYSINH == DateTime.MinValue)
            {
                command.Parameters.Add("NgaySinh", SqlDbType.DateTime).Value = DBNull.Value;
            }
            else
            {
                command.Parameters.Add("NgaySinh", SqlDbType.DateTime).Value = ob.NGAYSINH;
            }
            command.Parameters.Add("GioiTinh", SqlDbType.Int).Value = ob.GIOITINH;
            if (ob.DIACHI.Trim() == "")
            {
                command.Parameters.Add("DiaChi", SqlDbType.NVarChar).Value = DBNull.Value;
            }
            else
            {
                command.Parameters.Add("DiaChi", SqlDbType.NVarChar).Value = ob.DIACHI;
            }
            if (ob.CREATEDTIME == DateTime.MinValue)
            {
                command.Parameters.Add("CreatedTime", SqlDbType.DateTime).Value = DBNull.Value;
            }
            else
            {
                command.Parameters.Add("CreatedTime", SqlDbType.DateTime).Value = ob.CREATEDTIME;
            }
            if (ob.CREATEDBY.Trim() == "")
            {
                command.Parameters.Add("CreatedBy", SqlDbType.VarChar).Value = DBNull.Value;
            }
            else
            {
                command.Parameters.Add("CreatedBy", SqlDbType.VarChar).Value = ob.CREATEDBY;
            }
            if (ob.MODIFIEDTIME == DateTime.MinValue)
            {
                command.Parameters.Add("ModifiedTime", SqlDbType.DateTime).Value = DBNull.Value;
            }
            else
            {
                command.Parameters.Add("ModifiedTime", SqlDbType.DateTime).Value = ob.MODIFIEDTIME;
            }
            if (ob.MODIFIEDBY.Trim() == "")
            {
                command.Parameters.Add("ModifiedBy", SqlDbType.VarChar).Value = DBNull.Value;
            }
            else
            {
                command.Parameters.Add("ModifiedBy", SqlDbType.VarChar).Value = ob.MODIFIEDBY;
            }
            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);
        }
        //InsertobGetId
        public int _InsertObGetId(Ob_POS_DM_KhachHang ob, SqlConnection conn)
        {
            int num = 0;

            if (conn.ConnectionString != "")
            {
                conn.Open();
                SqlCommand command = new SqlCommand
                {
                    Connection  = conn,
                    CommandType = CommandType.Text,
                    CommandText = @"Insert Into POS_DM_KhachHang (ID_KhachHang, TenKhachHang, SoDienThoai, NgaySinh, GioiTinh, DiaChi, CreatedTime, CreatedBy, ModifiedTime, ModifiedBy) Values(@ID_KhachHang, @TenKhachHang, @SoDienThoai, @NgaySinh, @GioiTinh, @DiaChi, @CreatedTime, @CreatedBy, @ModifiedTime, @ModifiedBy)Select @@IDENTITY"
                };
                command.Parameters.Add("ID_KhachHang", SqlDbType.UniqueIdentifier).Value = new SqlGuid(ob.ID_KHACHHANG);
                if (ob.TENKHACHHANG.Trim() == "")
                {
                    command.Parameters.Add("TenKhachHang", SqlDbType.NVarChar).Value = DBNull.Value;
                }
                else
                {
                    command.Parameters.Add("TenKhachHang", SqlDbType.NVarChar).Value = ob.TENKHACHHANG;
                }
                if (ob.SODIENTHOAI.Trim() == "")
                {
                    command.Parameters.Add("SoDienThoai", SqlDbType.VarChar).Value = DBNull.Value;
                }
                else
                {
                    command.Parameters.Add("SoDienThoai", SqlDbType.VarChar).Value = ob.SODIENTHOAI;
                }
                if (ob.NGAYSINH == DateTime.MinValue)
                {
                    command.Parameters.Add("NgaySinh", SqlDbType.DateTime).Value = DBNull.Value;
                }
                else
                {
                    command.Parameters.Add("NgaySinh", SqlDbType.DateTime).Value = ob.NGAYSINH;
                }
                command.Parameters.Add("GioiTinh", SqlDbType.Int).Value = ob.GIOITINH;
                if (ob.DIACHI.Trim() == "")
                {
                    command.Parameters.Add("DiaChi", SqlDbType.NVarChar).Value = DBNull.Value;
                }
                else
                {
                    command.Parameters.Add("DiaChi", SqlDbType.NVarChar).Value = ob.DIACHI;
                }
                if (ob.CREATEDTIME == DateTime.MinValue)
                {
                    command.Parameters.Add("CreatedTime", SqlDbType.DateTime).Value = DBNull.Value;
                }
                else
                {
                    command.Parameters.Add("CreatedTime", SqlDbType.DateTime).Value = ob.CREATEDTIME;
                }
                if (ob.CREATEDBY.Trim() == "")
                {
                    command.Parameters.Add("CreatedBy", SqlDbType.VarChar).Value = DBNull.Value;
                }
                else
                {
                    command.Parameters.Add("CreatedBy", SqlDbType.VarChar).Value = ob.CREATEDBY;
                }
                if (ob.MODIFIEDTIME == DateTime.MinValue)
                {
                    command.Parameters.Add("ModifiedTime", SqlDbType.DateTime).Value = DBNull.Value;
                }
                else
                {
                    command.Parameters.Add("ModifiedTime", SqlDbType.DateTime).Value = ob.MODIFIEDTIME;
                }
                if (ob.MODIFIEDBY.Trim() == "")
                {
                    command.Parameters.Add("ModifiedBy", SqlDbType.VarChar).Value = DBNull.Value;
                }
                else
                {
                    command.Parameters.Add("ModifiedBy", SqlDbType.VarChar).Value = ob.MODIFIEDBY;
                }
                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);
        }
        //GetObWhere
        public Ob_POS_DM_KhachHang _GetObWhere(string keyword, SqlConnection conn)
        {
            if (conn.ConnectionString == "")
            {
                return(null);
            }
            conn.Open();
            Ob_POS_DM_KhachHang ob      = new Ob_POS_DM_KhachHang();
            SqlCommand          command = new SqlCommand
            {
                Connection  = conn,
                CommandType = CommandType.Text,
                CommandText = @"Select ID_KhachHang, TenKhachHang, SoDienThoai, NgaySinh, GioiTinh, DiaChi, CreatedTime, CreatedBy, ModifiedTime, ModifiedBy From POS_DM_KhachHang Where " + keyword + ""
            };
            SqlDataReader reader = command.ExecuteReader();
            int           num    = 0;

            while (reader.Read())
            {
                num++;
                if (reader[0] != DBNull.Value)
                {
                    ob.ID_KHACHHANG = Convert.ToString(reader[0]);
                }
                if (reader[1] != DBNull.Value)
                {
                    ob.TENKHACHHANG = Convert.ToString(reader[1]);
                }
                if (reader[2] != DBNull.Value)
                {
                    ob.SODIENTHOAI = Convert.ToString(reader[2]);
                }
                if (reader[3] != DBNull.Value)
                {
                    ob.NGAYSINH = Convert.ToDateTime(reader[3]);
                }
                if (reader[4] != DBNull.Value)
                {
                    ob.GIOITINH = Convert.ToInt32(reader[4]);
                }
                if (reader[5] != DBNull.Value)
                {
                    ob.DIACHI = Convert.ToString(reader[5]);
                }
                if (reader[6] != DBNull.Value)
                {
                    ob.CREATEDTIME = Convert.ToDateTime(reader[6]);
                }
                if (reader[7] != DBNull.Value)
                {
                    ob.CREATEDBY = Convert.ToString(reader[7]);
                }
                if (reader[8] != DBNull.Value)
                {
                    ob.MODIFIEDTIME = Convert.ToDateTime(reader[8]);
                }
                if (reader[9] != DBNull.Value)
                {
                    ob.MODIFIEDBY = Convert.ToString(reader[9]);
                }
            }
            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);
        }