コード例 #1
0
ファイル: AdoSession.cs プロジェクト: zr10728/Chloe
        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;
                    IDbParameterAssembler      dbParameterAssembler = MappingTypeSystem.IsMappingType(parameterType, out mappingType) ? mappingType.DbParameterAssembler : DbParameterAssembler.Default;
                    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);
        }