.NET餐厅管理系统sql数据帮助类获得数据库服务器当前时间、执行对单个Entity的更新、C#利用反射获取对象属性值
admin
2024-03-02 10:32:02

 #region //获得数据库服务器当前时间
    ///


    /// 获得数据库服务器当前时间
    ///

    ///
    public DateTime Get_DbServerTime()
    {
        SqlDataReader rs = null;
        DateTime dt = new DateTime();
        try
        {
            CloseDb();
            OpenDb();
            SqlCommand cm = new SqlCommand("select getdate() ", conn);
            rs = cm.ExecuteReader(CommandBehavior.CloseConnection);
            cm.Dispose();
            cm = null;
            //Close();
            if (rs.Read())
            { dt = DateTime.Parse(rs[0].ToString()); }
                
            CloseDb();
        }
        catch
        {
            throw new Exception("取服务器时间出错!");
        }
        return dt;
    }
    #endregion

    #region  //执行对单个Entity的更新
    ///


    /// 执行对单个Entity的更新
    ///

    ///
    public void ExecuteObjectUpdate(BaseEntity baseEntity)
    {
        ExecuteObjectUpdate(baseEntity, "");
    }
    #endregion

 #region  //C#利用反射获取对象属性值
    ///


    /// C#利用反射获取对象属性值
    ///

    ///
    ///
    /// 需要更改的值
    ///
    public static string GetObjectPropertyValue(Type type, BaseEntity baseEntity, string propertyname)
    {

        PropertyInfo property = type.GetProperty(propertyname);//根据变量名得到变量对象

        //if (property == null) return null;//BaseEntity.cs属性名与BaseModel.cs中属性名不相同时,并不进行编辑

        object o = property.GetValue(baseEntity, null);//从实体中获取具体值 重要

        if (o == null) return null;

        return o.ToString();
    }
    #endregion

相关内容

热门资讯

丽江古城迷路,被客栈老板塞了一... 石板路上的暖光 一、走错的青石板 我攥着打印了三遍的攻略,在丽江古城的岔路口站成了一块被风吹得发僵的...
蛟河游船好去处,宴宾园活鱼馆美... 在蛟河游玩,游船是领略湖光山色的绝佳方式,而游玩之后,一顿美味的餐食更是不可或缺。宴宾园活鱼馆就是这...
必去的景点有免费WiFi、三级... 在旅游的世界里,寻找那些拥有独特魅力且设施完备的景点是每一位旅行者的心愿。今天,我们要为大家介绍一个...