博观而约取,厚积而薄发。这篇文章主要讲述检测空值,以及会不会出现mapping类型不一致的问题相关的知识,希望能为你提供帮助。
/// < summary> /// 检测空值,以及会不会出现mapping类型不一致的问题 /// < /summary> /// < typeparam name="T"> < /typeparam> /// < param name="nullsb1"> < /param> /// < param name="errorsb"> < /param> public static void CheckHibernateMappingType< T> (out StringBuilder nullsb1,out StringBuilder errorsb) where T:Framework.Domain.Entity,new() { nullsb1 = new StringBuilder(); errorsb = new StringBuilder(); var ctx = Spring.Context.Support.ContextRegistry.GetContext(); var Dao = GetDao(); var mapping = ((Spring.Data.NHibernate.LocalSessionFactoryObject)ctx.GetObject("& SessionFactory")).Configuration.GetClassMapping(typeof(T)); var pmap = mapping.PropertyIterator; var tableName1 = mapping.Table.Name; var table = Dao.ExecuteDataSet(new QueryInfo() { CustomSQL = "select * from " + tableName1 }).Tables[0]; Hashtable columnsType = new Hashtable(); foreach (DataColumn col in table.Columns) { columnsType.Add(col.ColumnName, col.DataType.Name); } string primaryKey = ((NHibernate.Mapping.Column)(mapping.Key.ColumnIterator.FirstOrDefault())).Name; StringBuilder sb = new StringBuilder(); StringBuilder sb1 = new StringBuilder(); foreach (DataRow dr in table.Rows) { foreach (var item in pmap) { NHibernate.Mapping.Column c = (NHibernate.Mapping.Column)item.ColumnIterator.FirstOrDefault(); //1对多的属性如children没有对应的列 if (c != null) { string columnTypeName = columnsType[c.Name].ToString(); try { if (dr[c.Name] is DBNull) { nullsb1.Append("主键" + dr[primaryKey].ToString() + "列" + c.Name + "约定类型为:" + columnTypeName + "值为:NULL\r\n"); continue; } switch (columnTypeName) { case "String":break; case "Int16": Convert.ToInt16(dr[c.Name]); break; case "Int32": Convert.ToInt32(dr[c.Name]); break; case "Int64": Convert.ToInt64(dr[c.Name]); break; case "Decimal": Convert.ToDecimal(dr[c.Name]); break; case "DateTime":Convert.ToDateTime(dr[c.Name]); break; case "Boolean": case "bool": Convert.ToBoolean(dr[c.Name]); break; default: break; } } catch (Exception ex) {sb.Append("主键" + dr[primaryKey].ToString() + "列" + c.Name + "约定类型为:" + columnTypeName + "值为:" + dr[c.Name] + "??类型转换异常\r\n"); }} } } }
【检测空值,以及会不会出现mapping类型不一致的问题】
推荐阅读
- appium入门基础
- 在Android上仿百度贴吧客户端Loading图标小球
- Android线性渐变
- appium===出错时截图的方法,自动截图
- cordova 跨平台APP版本升级
- 解决Azure Stack App Service部署报错一例
- android如何判断控件的显示或者隐藏
- Java PushbackInputStream类
- Java PrintWriter类