/// <summary> /// 是否通过空值验证 /// </summary> /// <param name="field"></param> /// <param name="value"></param> /// <returns></returns> public static bool PassNullValue(IDataFieldModel field, object value) { if (field.CanbeNull()) { return(true); } if (IsDbNullValue(value)) { return(false); } var x = field.GetDataIdRefence(); if (x != null) { if (value is long) { return(IsValidId((long)value)); } if (value is long?) { return(IsValidId((long?)value)); } } return(true); }
private static bool IsNullableFieldForBooleanOrNum(IDataFieldModel f) { if (f == null) { return(false); } return(f.CanbeNull() && (f.FieldDbType == System.Data.DbType.Boolean || DataValueParase.IsNumberType(f.GetClrType()) != NumberType.Unkown)); }
/// <summary> /// 创建字段定义的SQL /// </summary> /// <param name="field">字段模型</param> /// <param name="tablesourcename">表名称</param> /// <param name="stringcollatename">字符排序</param> /// <returns></returns> private static string CreateTableFieldSql( IDataFieldModel field, string tablesourcename, string stringcollatename) { StringBuilder xx = new StringBuilder(); //添加 "[fieldname]" xx.Append(MsSqlServerHelper.GetFeildNameInSqlExpress(field.Name)); if (field.IsRowVersion()) { xx.Append(" [timestamp] NULL"); } else { //添加 " [dbtype]" -> "[fieldname] [dbtype]" xx.AppendFormat(" [{0}]", DbTypeToSqlDbType(field.FieldDbType).ToString().ToLower()); switch (field.FieldDbType) { case System.Data.DbType.AnsiString: case System.Data.DbType.String: { int size = field.GetFieldSize(); if (size <= 0) { size = 50; } if (size == int.MaxValue) { xx.AppendFormat("(max) COLLATE {0}", stringcollatename); } else { xx.AppendFormat("({0}) COLLATE {1}", size, stringcollatename); } //添加 "(size) COLLATE ....." -> "[fieldname] [dbtype](size) COLLATE...." break; } case System.Data.DbType.Decimal: { int l = 18; int dot = 4; var d = field.GetDecimalDefine(); if (d != null) { l = d.DecimalLength; dot = d.DotLength; if (l <= 0) { l = 18; } if (dot < 0) { dot = 0; } } //添加 "(length,dotlength)" -> "[fieldname] [dbtype](length,dotlength)" xx.AppendFormat("({0},{1})", l, dot); break; } case System.Data.DbType.Int16: case System.Data.DbType.Int32: case System.Data.DbType.Int64: { var g = field.GetDbGeneratedDefine(); if (g != null) { //添加 " IDENTITY(start,step)" -> "[fieldname] [dbtype]IDENTITY(start,step)" xx.AppendFormat(" IDENTITY({0},{1})", g.StartValue, g.Step); } break; } } if (field.CanbeNull()) { xx.Append(" NULL"); } else { xx.Append(" NOT NULL"); } string defaultvalue = field.GetDefaultValue(); if (!string.IsNullOrEmpty(defaultvalue)) { xx.AppendFormat(" CONSTRAINT [DF_{0}_{1}_defaultvalue] DEFAULT ({2})", tablesourcename, field.Name, defaultvalue); } } return(xx.ToString()); }