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

  • 相关阅读:
    App测试从入门到精通之安装、卸载和运行测试
    App测试从入门到精通之App分类和场景操作系统
    一步到位带你入门Selenium
    MAMP和WAMP搭建Web环境,数据库,数据分布可视化
    爬虫 Http请求,urllib2获取数据,第三方库requests获取数据,BeautifulSoup处理数据,使用Chrome浏览器开发者工具显示检查网页源代码,json模块的dumps,loads,dump,load方法介绍
    Python 基本语法,文件读写,数据结构和类型
    python 数据工程 and 开发工具Sublime
    jieba user guide
    python各类项目模块记录
    python parse xml using DOM
  • 原文地址:https://www.cnblogs.com/1001tao/p/1943015.html
Copyright © 2011-2022 走看看