zoukankan      html  css  js  c++  java
  • 从数据库读取数据Table后转成对应的实体泛型方法

    1 每次读取数据库的数据都是一个DataTable表,以前是傻傻的每个表都写一个转换的类,后来自己研究一个泛型方法,适用于所有转换

          /// <summary>
           /// 返回一个集合
           /// </summary>
           /// <typeparam name="T2">要传入的实体</typeparam>
           /// <param name="strSql">sql语句或者存储过程类型</param>
           /// <param name="sqlComType">sql语句或者存储过程类型</param>
           /// <param name="pars">Sql参数数组</param>
           /// <returns></returns>
           public static List<T2> ExcuteList<T2>(string strSql, CommandType sqlComType, ref string errMsg, params SqlParameter[] pars)
           {
               try
               {
                   using (SqlConnection conn = new SqlConnection(s_ConnectionString))
                   {
                       SqlDataAdapter sda = new SqlDataAdapter(strSql, conn);
                       if (pars != null)
                       {
                           sda.SelectCommand.Parameters.AddRange(pars);
                       }
                       sda.SelectCommand.CommandType = sqlComType;
                       DataTable dt = new DataTable();
                       sda.Fill(dt);
                       if (dt.Rows.Count > 0)
                       {
                           List<T2> list = new List<T2>();
                           Type t = typeof(T2);
                           foreach (DataRow dr in dt.Rows)
                           {
                               T2 model = (T2)Activator.CreateInstance(t);
                               PropertyInfo[] pros = t.GetProperties();
                               foreach (PropertyInfo p in pros)
                               {
                                   string colName = p.Name;
                                   if (dt.Columns.Contains(colName))
                                   {
                                       if (p.CanWrite == false) continue;
                                       object cellValue = dr[colName];
                                       if (cellValue != DBNull.Value)
                                       {
                                           p.SetValue(model, cellValue, null);
                                       }
                                   }
                               }
                               list.Add(model);
                           }
                           return list;
                       }
                       else
                       {
                           return null;
                       }
                   }
               }
               catch (Exception ex)
               {
                   errMsg =    ex.ToString();
                   return null;
               }
           }

    调用
       public IList<C_AccountModel> GetAccountList(ref string errMsg)
            {
                return C_SQLHelper.ExcuteList<C_AccountModel>("select * from Account  order by ID desc ", CommandType.Text, ref errMsg, null);
            }
    
    
    

      2 注意的地方

           a: 数据库表的字段必须与实体一样否则反射的时候装载不了。

     
  • 相关阅读:
    从属性赋值到MVVM模式详解
    C#综合揭秘——细说事务
    Action与Trigger
    C#综合揭秘——细说多线程(下)
    继承BitmapSource并使用独立存储来缓存远程的图片
    Windows Phone 7 MVVM模式数据绑定和传递参数
    Lambda表达式总结
    Windows Phone页面导航和独立存储开发总结
    RegisterHotKey设置系统级热键《转》
    隐藏统计代码或者任何不想被看见的东西《转》
  • 原文地址:https://www.cnblogs.com/cdaq/p/6033615.html
Copyright © 2011-2022 走看看