Exemple #1
0
        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       = null;
                    MappingTypeInfo  mappingTypeInfo = MappingTypeSystem.GetMappingTypeInfo(parameterType);
                    if (mappingTypeInfo != null && mappingTypeInfo.MappingType != null)
                    {
                        parameter = mappingTypeInfo.MappingType.CreateDataParameter(cmd, param);
                    }
                    else
                    {
                        parameter = this.CreateDataParameter(cmd, param, mappingTypeInfo);
                    }

                    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);
        }