Beispiel #1
0
        protected DbCommand GetDbCommandByKeyValue(TypeSchema entityInfo, object keyValue)
        {
            DbCommand dbCommand = DbAccess.CreateDbCommand();

            foreach (SchemaItem mfi in entityInfo.GetKeyFieldInfos())
            {
                Type type = mfi.ProInfo.PropertyType;
                type = Nullable.GetUnderlyingType(type) ?? type;
                DbType      dbType    = ORMHelper.GetDbTypeByName(type.Name);
                DbParameter parameter = DbAccess.CreateDbParameter();
                parameter.ParameterName = "@" + mfi.ProInfo.Name;
                parameter.DbType        = dbType;

                //parameter.Value = Convert.ChangeType(keyValue, fieldProperty.PropertyType);
                parameter.Value = Convert.ChangeType(keyValue, type);
                dbCommand.Parameters.Add(parameter);
            }
            return(dbCommand);
        }
Beispiel #2
0
        internal virtual void Init(Type objectType, IDbAccess dbAccess)
        {
            TypeSchema entityInfo = ORMSchemaCache.GetTypeSchema(objectType);
            SchemaItem fieldInfo  = entityInfo.GetFieldInfoByPropertyName(this._propertyName);

            if (fieldInfo == null)
            {
                throw new ArgumentException(string.Format("{0} - Property \"{1}\" not define a mapping field.", this.GetType(), _propertyName));
            }
            Type type = fieldInfo.ProInfo.PropertyType;

            type = Nullable.GetUnderlyingType(type) ?? type;
            if (type.IsEnum)
            {
                this._dbType = DbType.Int32;
            }
            else
            {
                this._dbType = ORMHelper.GetDbTypeByName(type.Name);
            }
            this._fieldName = fieldInfo.MappingFieldAttribute.FieldName;
            this._dbAccess  = dbAccess;
        }
Beispiel #3
0
        protected DbParameter GetDbParameter(PropertyInfo fieldProperty, object entity)
        {
            DbParameter parameter = DbAccess.CreateDbParameter();
            Type        tempType  = fieldProperty.PropertyType;
            DbType      dbType;

            if (tempType.IsEnum)
            {
                dbType = DbType.Int32;
            }
            else
            {
                if (Nullable.GetUnderlyingType(tempType) != null)//如果是 int? 类型则进行处理
                {
                    tempType = Nullable.GetUnderlyingType(tempType);
                }
                dbType = ORMHelper.GetDbTypeByName(tempType.Name);
            }
            parameter.ParameterName = "@" + fieldProperty.Name;
            parameter.DbType        = dbType;
            parameter.Value         = fieldProperty.GetValue(entity, null) ?? DBNull.Value;

            return(parameter);
        }