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

  • 相关阅读:
    41.分词器简单介绍
    40.倒排索引核心原理
    39.exact value and full text
    38.mapping小例子
    37.query string、_all metadata
    36.分页及deep paging
    35.multi-index和multi-type搜索模式
    BZOJ 1680 [Usaco2005 Mar]Yogurt factory:贪心【只用考虑上一个】
    BZOJ 1637 [Usaco2007 Mar]Balanced Lineup:前缀和 + 差分
    BZOJ 1647 [Usaco2007 Open]Fliptile 翻格子游戏:部分枚举 位运算
  • 原文地址:https://www.cnblogs.com/1001tao/p/1943015.html
Copyright © 2011-2022 走看看