检测空值,以及会不会出现mapping类型不一致的问题
Posted 听哥哥的话
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了检测空值,以及会不会出现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类型不一致的问题的主要内容,如果未能解决你的问题,请参考以下文章