/// <summary> /// 对新增和修改的检测 /// 1、是否可为空 /// 2、字符长度 /// 3、数字范围 /// </summary> /// <param name="datamoudle"></param> /// <param name="f"></param> /// <param name="v"></param> /// <param name="errs"></param> private void CheckAddOrUpdateFieldValue( IDataModel datamoudle, IDataFieldModel f, object v, IList <DataModalFieldException> errs) { if (!DataValueParase.PassNullValue(f, v)) { errs.Add(ErrorCreateHelper.CreateFieldIsNullError(f.Name, f.Title)); return; } if (f.FieldDbType == System.Data.DbType.String) { //字符是否超出最大长度 int mx = f.GetFieldSize(); //等于int.maxvalue则不限制字符大小 if (mx != int.MaxValue) { mx = mx * 2; //字符类型在数据库存储均使用nvarchar类型(Unicode:每个字符占两字节) string vv = (string)v; if (!string.IsNullOrEmpty(vv)) { int sl = System.Text.Encoding.Unicode.GetByteCount(vv); if (sl > mx) { errs.Add(ErrorCreateHelper.CreateFieldValueOutOfRangeError(f.Name, f.Title, mx)); return; } } } } else { //数字是否超出范围 Type fieldType = f.GetClrType(); //数字是否超出范围 NumberType nt = DataValueParase.IsNumberType(fieldType); if ((nt != NumberType.Unkown) || (fieldType == typeof(DateTime) || fieldType == typeof(TimeSpan))) { string exp = f.GetMinMaxExpress(); if (!string.IsNullOrWhiteSpace(exp) && !DataValueParase.IsDbNullValue(v)) { if (!MinMaxValueHelper.ValueIsInRangle(exp, v)) { errs.Add(ErrorCreateHelper.CreateFieldValueOutOfRangeError(f.Name, f.Title, exp)); return; } } } } }
/// <summary> /// 添加参数到paramets中去 /// </summary> /// <param name="paramets">参数集合,返回的参数将添加到该集合中,如果不为空的话</param> /// <param name="ft">参数后缀,可为为null</param> /// <param name="field">字段信息</param> /// <param name="value">行值</param> /// <returns></returns> private static DataParamet AppendParamet(List <IDataParamet> paramets, string parhz, IDataFieldModel field, object rowvalue) { object value = field.GetModelFieldValue(rowvalue); value = ConvertFieldValueToDbValue(field, value); DataParamet p = new DataParamet() { Name = GenerateParametName(field.Name, parhz), Value = value, ValueType = field.GetClrType() }; int s = field.GetFieldSize(); if (s > 0) { p.ValueSize = s; } if (paramets != null) { paramets.Add(p); } return(p); }
/// <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()); }