zoukankan      html  css  js  c++  java
  • Ado.net利用反射执行SQL得到实体

    public Model.orderParent GetTraceIDforID(string orderid)
            {
                string sql = string.Format(" select * from orderParent where Id='{0}'", orderid);
                DataTable dt = new BaseBLL().DataAccess.QueryDataTable(sql);
                if (dt != null && dt.Rows.Count > 0)
                {
                    Model.orderParent data = (Model.orderParent)ReflectionHelper.AssignDataSetToModel(dt, (new Model.orderParent()).GetType());
                    return data;
                }
                else
                {
                    return null;
                }
            }
    

      

     public static Object AssignDataSetToModel(System.Data.DataTable dt, Type objectType)
            {
                try
                {
                    if (dt.Rows.Count <= 0)
                    {
                        return null;
                    }
                    System.Reflection.PropertyInfo[] pis = objectType.GetProperties();
                    Object obj = null;
                    if (null != pis)
                    {
                        Type[] paramTypes = new Type[0];
                        object[] paramArray = new object[0];
                        obj = objectType.GetConstructor(paramTypes).Invoke(paramArray);
                        foreach (PropertyInfo pi in pis)
                        {
                            if (pi.DeclaringType.Equals(objectType))
                            {
                                int colIndex = getColindex(pi.Name, dt);
                                if (pi.PropertyType.Name == "Char" || pi.PropertyType.Name == "Int32" || pi.PropertyType.Name == "Single" || pi.PropertyType.Name == "Decimal" || pi.PropertyType.Name == "DateTime" || pi.PropertyType.Name == "Boolean")
                                {
    
                                    if (pi.PropertyType.Name == "Int32")
                                        pi.SetValue(obj, dt.Rows[0][colIndex] == DBNull.Value ? 0 : Convert.ToInt32(dt.Rows[0][colIndex]), null);
                                    else if (pi.PropertyType.Name == "Single")
                                        pi.SetValue(obj, dt.Rows[0][colIndex] == DBNull.Value ? 0 : Convert.ToSingle(dt.Rows[0][colIndex]), null);
                                    else if (pi.PropertyType.Name == "Decimal")
                                        pi.SetValue(obj, dt.Rows[0][colIndex] == DBNull.Value ? 0 : Convert.ToDecimal(dt.Rows[0][colIndex]), null);
                                    else if (pi.PropertyType.Name == "DateTime")
                                        pi.SetValue(obj, dt.Rows[0][colIndex] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dt.Rows[0][colIndex]), null);
                                    else if (pi.PropertyType.Name == "Boolean")
                                        pi.SetValue(obj, dt.Rows[0][colIndex] == DBNull.Value ? false : Convert.ToBoolean(dt.Rows[0][colIndex]), null);
                                    else if (pi.PropertyType.Name == "Char")
                                        pi.SetValue(obj, dt.Rows[0][colIndex] == DBNull.Value ? '0' : Convert.ToChar(dt.Rows[0][colIndex]), null);
                                }
                                else
                                    pi.SetValue(obj, dt.Rows[0][colIndex] == DBNull.Value ? "" : dt.Rows[0][colIndex], null);
    
                            }
                        }
                    }
                    return obj;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
    

      

  • 相关阅读:
    c++3种内存管理方式
    什么是向上兼容和向下兼容?
    回溯法解马的遍历问题
    c++内联函数
    2009年NCRE考试有新变化
    sql server日期时间函数
    Web开发工具大集合
    javascript屏幕高度和宽度等信息代码
    gridview无数据行时显示表头的方法
    IE, FF, Safari前端开发常用调试工具
  • 原文地址:https://www.cnblogs.com/jordan2009/p/3433225.html
Copyright © 2011-2022 走看看