IEnumerator,IEnumerable怎样转换为datatable。
linq to sql~~用代码形式使用存储过程~~存储过程的参数的类型是~??应该不会只有sqlparameter的形式吧??~~请举出具体例子,谢谢诶

解决方案 »

  1.   

    传统写法:
    DataTable table = new DataTable();
    table.Columns.Add("UserID", typeof (long));
    table.Columns.Add("UserName", typeof (string));IEnumerable<T> database = //获取数据源;
        foreach (T info in database) {
            DataRow row = dt.NewRow();
            row["ID"] = database.ID;
            row["Name"] = database.Name;
            table.Rows.Add(row);
         }
      

  2.   

    扩展方法:
    public static DataTable ConvertToDataTable(this IEnumerable enumerable)
    {
        var dataTable = new DataTable();
        foreach (PropertyDescriptor pd in TypeDescriptor.GetProperties(typeof(T))) {
               dataTable.Columns.Add(pd.Name, pd.PropertyType);
        }
        foreach (T item in enumerable) {
               var Row = dataTable.NewRow();
                   
           foreach (PropertyDescriptor dp in TypeDescriptor.GetProperties(typeof(T)))  {
                     Row[pd.Name] = pd.GetValue(item);
           }
        dataTable.Rows.Add(Row);
       }
       return dataTable;}
      

  3.   

        public static DataTable ToDataTable<TResult>(this IEnumerable<TResult> value) where TResult : class
            {
                //创建属性的集合
                List<PropertyInfo> pList = new List<PropertyInfo>();
                Type type = typeof(TResult);
              DataTable dt = new DataTable();
                Array.ForEach<PropertyInfo>(type.GetProperties(), p => { pList.Add(p);dt.Columns.Add(p.Name, p.PropertyType); });      
                foreach (var item in value)
                {
                    //创建一个DataRow实例
                    DataRow row = dt.NewRow();
                    pList.ForEach(p => row[p.Name] = p.GetValue(item, null));
                    dt.Rows.Add(row);
                }
                return dt;
            }
        }
      

  4.   

    http://www.cnblogs.com/haoliansheng/archive/2010/07/06/1772308.html
      

  5.   

    存储过曾
     SqlConnection con = new SqlConnection("xxxxx");
           
                SqlCommand command = new SqlCommand("xxxxxxx",con);
                // 指定类型为存储过程
                command.CommandType = CommandType.StoredProcedure;
                 //参数名字,类型,大小 最后赋值
                command.Parameters.Add("name", SqlDbType.VarChar, 20).Value = "Value";
      

  6.   

    http://www.extensionmethod.net/Details.aspx?ID=249
      

  7.   


    PropertyInfo这个是????