• DataSet与List的转换


            /// <summary>
            /// DataSet转为List
            /// </summary>
            /// <typeparam name="T">实体</typeparam>
            /// <param name="DataSet">dataset</param>
            /// <param name="TableIndex">dataset中的第几个table</param>
            /// <returns></returns>
            public  static IList<T> DataSetToIList<T>(DataSet DataSet, int TableIndex)
            {
                if (DataSet == null || DataSet.Tables.Count < 0)
                    return null;
                if (TableIndex > DataSet.Tables.Count - 1)
                    return null;
                if (TableIndex < 0)
                    TableIndex = 0;
    
                DataTable p_Data = DataSet.Tables[TableIndex];
                //返回值初始化 
                IList<T> result = new List<T>();
                for (int j = 0; j < p_Data.Rows.Count; j++)
                {
                    T _t = (T)Activator.CreateInstance(typeof(T));
                    PropertyInfo[] propertys = _t.GetType().GetProperties();
                    foreach (PropertyInfo pi in propertys)
                    {
                        for (int i = 0; i < p_Data.Columns.Count; i++)
                        {
                            // 属性与字段名称一致的进行赋值 
                            if (pi.Name.Equals(p_Data.Columns[i].ColumnName))
                            {
                                // 数据库NULL值单独处理 
                                if (p_Data.Rows[j][i] != DBNull.Value)
                                {
                                    if (pi.PropertyType.Name.ToString() == "Double")
                                    {
                                        pi.SetValue(_t, Convert.ToDouble(p_Data.Rows[j][i]), null);
                                    }
    
                                    if (pi.PropertyType.Name.ToString() == "Int32")
                                    {
                                        pi.SetValue(_t, Convert.ToInt32(p_Data.Rows[j][i]), null);
                                    }
                                    else
                                    {
                                        pi.SetValue(_t, p_Data.Rows[j][i], null);
                                    }
                                    
                                }
                                else
                                {
                                    pi.SetValue(_t, null, null);
                                }
                                break;
                            }
                        }
                    }
                    result.Add(_t);
                }
                return result;
            }
    
            /// <summary>
            /// 从webservice中读取的XML转为DataSet
            /// </summary>
            /// <param name="XmlNopde"></param>
            /// <returns></returns>
            public static DataSet GetDataSet(XmlNode XmlNopde)
            {
                DataSet ds = new DataSet();
                
                XmlDataDocument xd = new XmlDataDocument();
                StringBuilder xmlString = new StringBuilder(XmlNopde.OuterXml);
    
                xd.LoadXml(xmlString.ToString());
                ds.ReadXml(new XmlNodeReader(xd));
                return ds;
            }
            /// <summary>
            /// list转DataSet
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="list"></param>
            /// <returns></returns>
            public static DataSet ConvertToDataSet<T>(List<T> list)
            {
                if (list == null || list.Count <= 0)
                {
                    return null;
                }
    
                DataSet ds = new DataSet();
                DataTable dt = new DataTable(typeof(T).Name);
                DataColumn column;
                DataRow row;
    
                System.Reflection.PropertyInfo[] myPropertyInfo = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
    
                foreach (T t in list)
                {
                    if (t == null)
                    {
                        continue;
                    }
    
                    row = dt.NewRow();
    
                    for (int i = 0, j = myPropertyInfo.Length; i < j; i++)
                    {
                        System.Reflection.PropertyInfo pi = myPropertyInfo[i];
    
                        string name = pi.Name;
    
                        if (dt.Columns[name] == null)
                        {
                            column = new DataColumn(name, pi.PropertyType);
                            dt.Columns.Add(column);
                        }
    
                        row[name] = pi.GetValue(t, null);
                    }
    
                    dt.Rows.Add(row);
                }
    
                ds.Tables.Add(dt);
    
                return ds;
            }
  • 相关阅读:
    maven继承父工程统一版本号
    shiro权限控制参考
    动态查询列表页面的分页
    SVN服务器更改ip地址后怎么办
    cookie记住密码功能
    分享小插件的问题
    阿里云短信验证
    从svn上更新maven项目时,所有文件变成包的形式
    Maven工具
    Mybatis的dao层传递单参出现的问题
  • 原文地址:https://www.cnblogs.com/gaoshuai/p/2990474.html
Copyright © 2020-2023  润新知