コード例 #1
0
        public static IDbValueReader GetDbValueReader(Type type)
        {
            var handler = GetValueHandlerCache.GetOrAdd(type, valueType =>
            {
                valueType = valueType.GetUnderlyingType();
                if (valueType.IsEnum)
                {
                    return(new DbValueReader((IDataReader reader, int ordinal) =>
                    {
                        object value = reader.GetEnum(ordinal, valueType);
                        return value;
                    }));
                }

                Infrastructure.MappingType mappingType = MappingTypeSystem.GetMappingType(valueType);

                return(new DbValueReader((IDataReader reader, int ordinal) =>
                {
                    object value = DataReaderExtension.GetValue(reader, ordinal);
                    if (value == null)
                    {
                        return null;
                    }

                    if (value.GetType() == mappingType.Type)
                    {
                        return value;
                    }

                    return mappingType.DbValueConverter.Convert(value);
                }));
            });

            return(handler);
        }
コード例 #2
0
        protected virtual IDbCommand PrepareCommand(string cmdText, DbParam[] parameters, CommandType cmdType, out List <OutputParameter> outputParameters)
        {
            outputParameters = null;

            IDbCommand cmd = this.DbConnection.CreateCommand();

            cmd.CommandText    = cmdText;
            cmd.CommandType    = cmdType;
            cmd.CommandTimeout = this.CommandTimeout;
            if (this.IsInTransaction)
            {
                cmd.Transaction = this.DbTransaction;
            }

            if (parameters != null)
            {
                for (int i = 0; i < parameters.Length; i++)
                {
                    DbParam param = parameters[i];
                    if (param == null)
                    {
                        continue;
                    }

                    if (param.ExplicitParameter != null)/* 如果存在创建好了的 IDbDataParameter,则直接用它。同时也忽视了 DbParam 的其他属性 */
                    {
                        cmd.Parameters.Add(param.ExplicitParameter);
                        continue;
                    }

                    Type parameterType;
                    if (param.Value == null || param.Value == DBNull.Value)
                    {
                        parameterType = param.Type ?? typeof(object);
                    }
                    else
                    {
                        parameterType = param.Value.GetType();
                        if (parameterType.IsEnum)
                        {
                            parameterType = Enum.GetUnderlyingType(parameterType);
                        }
                    }

                    IDbDataParameter           parameter   = cmd.CreateParameter();
                    Infrastructure.MappingType mappingType = MappingTypeSystem.GetMappingType(parameterType);
                    mappingType.DbParameterAssembler.SetupParameter(parameter, param);

                    cmd.Parameters.Add(parameter);

                    OutputParameter outputParameter = null;
                    if (param.Direction == ParamDirection.Output || param.Direction == ParamDirection.InputOutput)
                    {
                        outputParameter = new OutputParameter(param, parameter);
                        if (outputParameters == null)
                        {
                            outputParameters = new List <OutputParameter>();
                        }
                        outputParameters.Add(outputParameter);
                    }
                }
            }

            return(cmd);
        }