- 浏览: 302397 次
- 性别:
- 来自: 太原
文章分类
最新评论
-
天使建站:
只有代码,不能测试,太不方便,还是结合这里的一起看吧 ...
JQuery遍历JSON -
zxyzcmpy:
设置了之后依然没用怎么办?
HTTP 错误 404.2 – Not Found. -
gongcao:
这个ext和amchart都是那个版本啊
amChart覆盖Div层 -
wanmingtom:
谁能看完估计得成神经病
amchart柱状图、折线图配置解析 -
falloutxxx2:
终于发现一个 2.0 的破解谢谢!~!!
amcharts 常用SWF破解
using System; using System.Data; using System.Collections; using System.Collections.Generic; using System.Configuration; using System.Reflection; using System.Linq; using System.Xml.Linq; namespace UserFunction { /// <summary> /// Summary description for LinqToDataTable /// </summary> static public class LinqToDataTable { static public DataTable ToDataTable<T>(this IEnumerable<T> varlist, CreateRowDelegate<T> fn) { DataTable dtReturn = new DataTable(); // column names PropertyInfo[] oProps = null; // Could add a check to verify that there is an element 0 foreach (T rec in varlist) { // Use reflection to get property names, to create table, Only first time, others will follow if (oProps == null) { oProps = ((Type)rec.GetType()).GetProperties(); foreach (PropertyInfo pi in oProps) { Type colType = pi.PropertyType; if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>))) { colType = colType.GetGenericArguments()[0]; } dtReturn.Columns.Add(new DataColumn(pi.Name, colType)); } } DataRow dr = dtReturn.NewRow(); foreach (PropertyInfo pi in oProps) { dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue(rec, null); } dtReturn.Rows.Add(dr); } return (dtReturn); } public delegate object[] CreateRowDelegate<T>(T t); } } /* * sample: * var query = from ....; * DataTable dt = query.ToDataTable(rec => new object[] { query }); * */
公司有个新项目,使用sqlite作为数据库,其中有一个非常不爽的地方,如果当日期列出现NULL值时,通用DataAdepter.Fill方法填充到DateTable中时会发生错误,后来没办法只好使用dataReader循环读取数据来代替Fill,但又出现一个问题,如果保留表中的数据类型,当日期字段为空时无法插入到DataRow中,最后整个DataTable全使用string类型,这个问题一直困扰了很久,今日在网上无意中得到这样一篇代码,解决了这个问题,关键语句是 dataTable.LoadDataRow(array, true);用此方式可以将为NULL的字写入到值类型的数据单元中.
方法类: using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; //== using System.Collections; using System.Reflection; using System.Collections.Generic; /// <summary> ///tool 的摘要说明 /// </summary> public class tool { //public tool() //{ // // // //TODO: 在此处添加构造函数逻辑 // // //} /// <summary> /// 将泛型集合类转换成DataTable /// </summary> /// <typeparam name="T">集合项类型</typeparam> /// <param name="list">集合</param> /// <returns>数据集(表)</returns> /// //====表中无数据时使用: public static DataTable nullListToDataTable(IList list) { DataTable result = new DataTable(); object temp; if (list.Count > 0) { PropertyInfo[] propertys = list[0].GetType().GetProperties(); foreach (PropertyInfo pi in propertys) { //if (!(pi.Name.GetType() is System.Nullable)) if (pi != null) { //pi = (PropertyInfo)temp; result.Columns.Add(pi.Name, pi.PropertyType); } } for (int i = 0; i < list.Count; i++) { ArrayList tempList = new ArrayList(); foreach (PropertyInfo pi in propertys) { object obj = pi.GetValue(list[i], null); tempList.Add(obj); } object[] array = tempList.ToArray(); result.LoadDataRow(array, true); } } return result; } //====表中有数据时使用: public static DataTable noNullListToDataTable<T>(IList<T> list) { 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) { if (pi.PropertyType.UnderlyingSystemType.ToString() == "System.Nullable`1[System.Int32]") { column = new DataColumn(name, typeof(Int32)); dt.Columns.Add(column); //row[name] = pi.GetValue(t, new object[] {i});//PropertyInfo.GetValue(object,object[]) if (pi.GetValue(t, null) != null) row[name] = pi.GetValue(t, null); else row[name] = System.DBNull.Value; } else { 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.Tables[0]; } //表中有数据或无数据时使用,可排除DATASET不支持System.Nullable错误 public static DataTable ConvertToDataSet<T>(IList<T> list) { if (list == null || list.Count <= 0) //return null; { DataTable result = new DataTable(); object temp; if (list.Count > 0) { PropertyInfo[] propertys = list[0].GetType().GetProperties(); foreach (PropertyInfo pi in propertys) { //if (!(pi.Name.GetType() is System.Nullable)) //if (pi!=null) { //pi = (PropertyInfo)temp; result.Columns.Add(pi.Name, pi.PropertyType); } } for (int i = 0; i < list.Count; i++) { ArrayList tempList = new ArrayList(); foreach (PropertyInfo pi in propertys) { object obj = pi.GetValue(list[i], null); tempList.Add(obj); } object[] array = tempList.ToArray(); result.LoadDataRow(array, true); } } return result; } else { 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) { if (pi.PropertyType.UnderlyingSystemType.ToString() == "System.Nullable`1[System.Int32]") { column = new DataColumn(name, typeof(Int32)); dt.Columns.Add(column); //row[name] = pi.GetValue(t, new object[] {i});//PropertyInfo.GetValue(object,object[]) if (pi.GetValue(t, null) != null) row[name] = pi.GetValue(t, null); else row[name] = System.DBNull.Value; } else { 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.Tables[0]; } } } =============================================================================================== 调用: protected void Bind() { //var s = (from p in _7ctourDct.city // orderby p.cityName descending // select p); //List<city> c = new List<city>(); //c.AddRange(s.ToList()); //GridView1.DataSource = c; //GridView1.DataBind(); var s = from subsectionRoute in _7ctourDct.subsectionRoute select subsectionRoute; DataTable dt = new DataTable(); //var s = from subsectionRoute in _7ctourDct.subsectionRoute select subsectionRoute; //IList list = s.ToList(); //dt = ToDataTable(list); //or //dt = tool.ToDataTable(s.ToList()); if (s.ToList().Count >= 1) { dt = tool.noNullListToDataTable(s.ToList()); } else { dt = tool.nullListToDataTable(s.ToList()); } DataRow dr; //如果GRIDVIEW是自定义绑定字段,则必须为DT增加列,否则报错:数据源不包含字段 dt.Columns.Add(new DataColumn("subsectionRouteId", typeof(Int32))); dt.Columns.Add(new DataColumn("loginId", typeof(Int32))); dt.Columns.Add(new DataColumn("dayOrder", typeof(Int32))); dt.Columns.Add(new DataColumn("placeOrder", typeof(Int32))); dt.Columns.Add(new DataColumn("placeInDayOrder", typeof(String))); dt.Columns.Add(new DataColumn("cityId", typeof(Int32))); dt.Columns.Add(new DataColumn("actionTime", typeof(Int32))); dt.Columns.Add(new DataColumn("routeDetail", typeof(String))); dt.Columns.Add(new DataColumn("trafficPriceId", typeof(Int32))); dt.Columns.Add(new DataColumn("trafficDetail", typeof(Int32))); dt.Columns.Add(new DataColumn("trafficRemark", typeof(Int32))); dt.Columns.Add(new DataColumn("eateryPriceId", typeof(Int32))); dt.Columns.Add(new DataColumn("hotelPriceId", typeof(String))); dt.Columns.Add(new DataColumn("remark", typeof(Int32))); dt.Columns.Add(new DataColumn("basicGroupId", typeof(Int32))); dt.Columns.Add(new DataColumn("trafficType", typeof(Int32))); dt.Columns.Add(new DataColumn("trafficId", typeof(String))); for (int nIndex = 1; nIndex <= 8 - s.ToList().Count; nIndex++) { dr = dt.NewRow(); dt.Rows.Add(dr); } GridView1.DataSource = dt; GridView1.DataBind(); }
#region ListToDataTable /// <summary> /// ListToDataTable /// </summary> /// <typeparam name="T"></typeparam> /// <param name="list"></param> /// <returns></returns> public static DataTable ToDataTable<T>(this IEnumerable<T> list) { List<PropertyInfo> pList = new List<PropertyInfo>(); Type type = typeof(T); DataTable dt = new DataTable(); Array.ForEach<PropertyInfo>(type.GetProperties(), p => { pList.Add(p); dt.Columns.Add(p.Name, p.PropertyType); }); foreach (var item in list) { DataRow row = dt.NewRow(); pList.ForEach(p => row[p.Name] = p.GetValue(item, null)); dt.Rows.Add(row); } return dt; } #endregion
发表评论
-
asp.net中Session过期设置方法
2012-04-11 08:49 1390在Asp.net应用中,很多人会遇到Session过期设置 ... -
禁止复制文本的代码 HTML
2012-02-09 15:37 1483<!DOCTYPE HTML PUBLIC &qu ... -
Word检索COM类工厂中CLSID的组件时失败80070005解决办法
2012-01-18 10:36 2442在网上找了很多很多 ... -
word引用错误
2012-01-12 15:55 1289错误 4317 无法嵌入互操作类型“Microsoft.Off ... -
C#去掉HTML标记
2012-01-10 15:45 1024using System.Text.RegularExp ... -
C#文件操作
2012-01-10 10:07 967using System; usin ... -
Binary转换byte[] Linq方式
2012-01-04 14:46 5901把图片存放到数据库里,存为Image类型: ... -
c#Word操作3
2011-12-31 15:36 9171.建立word模板文件 person.dot 用书签 ... -
C# Word操作
2011-12-30 17:59 1140//生成WORD程序对象和 ... -
C#操作Word生成目录
2011-12-30 17:38 4305OperateWord ow = new OperateWor ... -
关于"不能启用此约束,因为不是所有的值都具有相应的父值。"的问题
2011-12-27 15:12 709ds.Tables[0].Rows[0]["S ... -
Linq Like
2011-12-13 11:49 1038System.Data.Linq.SqlClient Lik ... -
SSO解决方案大全(cookie跨域)
2011-12-02 17:46 1865前段时间为我们的系统做SSO(单点登录)参考了很多资料,其中包 ... -
ASP.NET的Cookie跨域问题
2011-12-02 17:45 2311将Cookie的有效范围限制到域。 默认情况下,Cookie ... -
.nET2.0小技巧
2011-12-01 17:16 16571. 在提交页面之后,保持滚动条的位置 可以在page ... -
.NET日期格式化
2011-11-30 10:41 978GridView中Dataformatstring 格式化日期 ... -
HTTP 错误 404.2 – Not Found.
2011-11-18 19:55 2356HTTP 错误 404.2 – Not Found. 由于 W ... -
WCF布署问题1 :HTTP 错误 404.17 - Not Found 请求的内容似乎是脚本,因而将无法由静态
2011-11-18 19:03 4127.Net3.5的WCF服务在IIS7中发布后,在IE中访问.s ... -
amcharts (.net)相关属性说明
2011-11-18 16:36 1984Axes 轴设置 CategoryA ... -
我在win7下装vs2008 90天破解
2011-11-18 13:51 17001. 打开 控制面板 –> ...
相关推荐
### 二、排除 DataSet 不支持 System.Nullable 错误 #### 2.1 Nullable 类型的概念 在 C# 中,Nullable<T> 类型允许一个值类型能够表示 null。这对于数据库字段来说非常有用,因为数据库字段可能为空 (NULL)。 ###...