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

 

posted on 2013-03-30 15:56  过往云烟  阅读(606)  评论(0编辑  收藏  举报