Exemple #1
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;
            this._dbType    = ORMHelper.GetDbTypeByName(type);
            this._fieldName = fieldInfo.MappingFieldAttribute.FieldName;
            this._dbAccess  = dbAccess;
        }
        protected DbParameter GetDbParameter(PropertyInfo fieldProperty, object entity)
        {
            DbParameter parameter = DbAccess.CreateDbParameter();
            Type        tempType  = fieldProperty.PropertyType;

            if (Nullable.GetUnderlyingType(tempType) != null)//如果是 int? 类型则进行处理
            {
                tempType = Nullable.GetUnderlyingType(tempType);
            }

            parameter.ParameterName = "@" + fieldProperty.Name;
            parameter.DbType        = ORMHelper.GetDbTypeByName(tempType);
            object value = fieldProperty.GetValue(entity, null);

            parameter.Value = value ?? DBNull.Value;    //如果对象的属性为null,则把此参数设置为DBNull
            return(parameter);
        }
        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);
                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);
        }