• 模型


     public static T GetModel<T>(T model, string wherestr) where T : new()
            {
                DBHelp db = new DBHelp();
                string typeName = model.GetType().Name;
                typeName = typeName.Substring(0, typeName.Length - 4);

                string strF = DbFields.FieldsBuilingInfo;


                string sql = string.Format("select {0} from {1} where {2}", strF, typeName, wherestr);

                PropertyInfo[] propertys = model.GetType().GetProperties();

                Hashtable dt = db.GetRecord(sql);


                foreach (PropertyInfo property in propertys)
                {
                    string str = property.Name.ToLower();

                    foreach (DictionaryEntry de in dt)
                    {
                        //注意HastTable内存储的默认类型是object,需要进行转换才可以输出

                        string t = de.Value.GetType().ToString();
                        if (de.Key.ToString().ToLower() == str)
                        {

                            if (de.Value.GetType().Equals(typeof(String)))
                            {
                                property.SetValue(model, de.Value.ToString(), null);
                                break;

                            }
                            else if (de.Value.GetType().Equals(typeof(DateTime)))
                            {

                                property.SetValue(model, DateTime.Parse(de.Value.ToString()), null);
                                break;

                            }
                            else if (de.Value.GetType().Equals(typeof(byte)))
                            {
                                property.SetValue(model, byte.Parse(de.Value.ToString()), null);
                                break;
                            }
                            else if (de.Value.GetType().Equals(typeof(short)))
                            {
                                property.SetValue(model, short.Parse(de.Value.ToString()), null);
                                break;
                            }
                            else if (de.Value.GetType().Equals(typeof(int)))
                            {
                                property.SetValue(model, int.Parse(de.Value.ToString()), null);
                                break;
                            }
                            else if (de.Value.GetType().Equals(typeof(float)))
                            {
                                property.SetValue(model, float.Parse(de.Value.ToString()), null);
                                break;
                            }
                            else if (de.Value.GetType().Equals(typeof(double)))
                            {
                                property.SetValue(model, System.Convert.ToDouble(de.Value.ToString()), null);
                                break;
                            }
                            else if (de.Value.GetType().Equals(typeof(decimal)))
                            {
                                property.SetValue(model, System.Convert.ToDecimal(de.Value.ToString()), null);
                                break;
                            }
                            else if (de.Value.GetType().Equals(typeof(Single)))
                            {
                                property.SetValue(model, System.Convert.ToSingle(de.Value.ToString()), null);
                                break;
                            }

                        }
                    }


                    //}
                }

                return model;
            }

            public static T GetModel2<T>(T model, string wherestr, string strField) where T : new()
            {
                DBHelp db = new DBHelp();
                string typeName = model.GetType().Name;
                typeName = typeName.Substring(0, typeName.Length - 4);

                string sql = string.Format("select {0} from {1} where {2}", strField, typeName, wherestr);

                PropertyInfo[] propertys = model.GetType().GetProperties();

                Hashtable dt = db.GetRecord(sql);

                try
                {
                    foreach (PropertyInfo property in propertys)
                    {
                        string str = property.Name.ToLower();

                        foreach (DictionaryEntry de in dt)
                        {
                            //注意HastTable内存储的默认类型是object,需要进行转换才可以输出
                            if (de.Key.ToString().ToLower() == str)
                            {

                                if (de.Value.GetType().Equals(typeof(String)))
                                {

                                    property.SetValue(model, de.Value.ToString(), null);

                                }
                                else if (de.Value.GetType().Equals(typeof(bool)))
                                {

                                    property.SetValue(model, bool.Parse(de.Value.ToString()), null);

                                }
                                else if (de.Value.GetType().Equals(typeof(DateTime)))
                                {

                                    property.SetValue(model, de.Value, null);

                                }
                                else if (de.Value.GetType().Equals(typeof(byte)))
                                {
                                    property.SetValue(model, byte.Parse(de.Value.ToString()), null);
                                }
                                else if (de.Value.GetType().Equals(typeof(short)))
                                {
                                    property.SetValue(model, short.Parse(de.Value.ToString()), null);
                                }
                                else if (de.Value.GetType().Equals(typeof(int)))
                                {
                                    property.SetValue(model, int.Parse(de.Value.ToString()), null);
                                }
                                else if (de.Value.GetType().Equals(typeof(float)))
                                {
                                    property.SetValue(model, float.Parse(de.Value.ToString()), null);
                                }
                                else if (de.Value.GetType().Equals(typeof(double)))
                                {
                                    property.SetValue(model, double.Parse(de.Value.ToString()), null);
                                }
                                else if (de.Value.GetType().Equals(typeof(decimal)))
                                {
                                    property.SetValue(model, System.Convert.ToDecimal(de.Value.ToString()), null);

                                    break;
                                }
                                else if (de.Value.GetType().Equals(typeof(Single)))
                                {
                                    property.SetValue(model, System.Convert.ToSingle(de.Value.ToString()), null);
                                    break;
                                }

                            }
                        }

                    }
                }
                    catch(Exception ex){}
               

              

                return model;
            }

    coolly

    专注于企业信息化

    技术领域:MOSS,ASP.net,SQLServer

    目前行业:房地产,重庆商用物业

    Email:1001tao@gmail.com

    Blog:www.cnblogs.com/1001tao

  • 相关阅读:
    plsql各版本官网下载地址
    docker容器启动rabbitmq并开启web控制台
    SM3杂凑算法实现
    快速设置IP、DNS等信息的bat脚本
    Windows下nginx的启动,重启,关闭等功能bat脚本
    记一次docker容器中运行springboot程序日志打印的中文变成问号
    calico 指定pod 网段
    helm 的安装使用
    K8s 节点断开连接后,本在运行的 Pod 会如何?
    nginx docker 安装ping 命令
  • 原文地址:https://www.cnblogs.com/1001tao/p/1943015.html
Copyright © 2020-2023  润新知