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;
                }
            }
    

      

  • 相关阅读:
    HTTP缓存——协商缓存(缓存验证)
    谷粒商城学习——P119-121映射
    powershell download, 兼容低版本 powershell
    InstallShield 2020 R3 破解补丁 支持 VS2019
    解决react使用antd table组件固定表头后,表头和表体列不对齐以及配置fixed固定左右侧后行高度不对齐
    numFormat 用于千分位的操作
    Jquery+NProgress实现网页进度条显示
    js变量前的+是什么意思
    有没有人遇到过用charles做js文件map时,文件若比较大,会被截掉一些(即映射到的文件不完整)的问题?
    如何写代码
  • 原文地址:https://www.cnblogs.com/jordan2009/p/3433225.html
Copyright © 2011-2022 走看看