zoukankan      html  css  js  c++  java
  • 模型

     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

  • 相关阅读:
    netty的基本使用
    netty 实现简单的rpc调用
    NIO 的基本使用
    BIO实现 Socket 通信
    springboot使用ElasticSearch
    docker-compose安装rabbitmq集群(主从集群---》镜像集群)
    杂谈:面向微服务的体系结构评审中需要问的三个问题
    使用Spring Boot和RxJava的构建响应式REST API
    JVM体系结构详解
    如何成为更好的程序员?
  • 原文地址:https://www.cnblogs.com/1001tao/p/1943015.html
Copyright © 2011-2022 走看看