public OutputParameter(DbParam param, IDbDataParameter parameter) { this._param = param; this._parameter = parameter; }
IDbDataParameter CreateDataParameter(IDbCommand cmd, DbParam param, MappingTypeInfo mappingTypeInfo) { IDbDataParameter parameter = cmd.CreateParameter(); parameter.ParameterName = param.Name; Type parameterType = null; if (param.Value == null || param.Value == DBNull.Value) { parameter.Value = DBNull.Value; parameterType = param.Type ?? typeof(object); } else { parameterType = param.Value.GetType(); if (parameterType.IsEnum) { parameterType = Enum.GetUnderlyingType(parameterType); parameter.Value = Convert.ChangeType(param.Value, parameterType); } else { parameter.Value = param.Value; } } if (param.Precision != null) { parameter.Precision = param.Precision.Value; } if (param.Scale != null) { parameter.Scale = param.Scale.Value; } if (param.Size != null) { parameter.Size = param.Size.Value; } if (param.DbType != null) { parameter.DbType = param.DbType.Value; } else if (mappingTypeInfo != null) { parameter.DbType = mappingTypeInfo.MapDbType; } const int defaultSizeOfStringOutputParameter = 4000;/* 当一个 string 类型输出参数未显示指定 Size 时使用的默认大小。如果有需要更大或者该值不足以满足需求,需显示指定 DbParam.Size 值 */ if (param.Direction == ParamDirection.Input) { parameter.Direction = ParameterDirection.Input; } else if (param.Direction == ParamDirection.Output) { parameter.Direction = ParameterDirection.Output; param.Value = null; if (param.Size == null && param.Type == UtilConstants.TypeOfString) { parameter.Size = defaultSizeOfStringOutputParameter; } } else if (param.Direction == ParamDirection.InputOutput) { parameter.Direction = ParameterDirection.InputOutput; if (param.Size == null && param.Type == UtilConstants.TypeOfString) { parameter.Size = defaultSizeOfStringOutputParameter; } } else { throw new NotSupportedException(string.Format("ParamDirection '{0}' is not supported.", param.Direction)); } return(parameter); }
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; } IDbDataParameter parameter = cmd.CreateParameter(); parameter.ParameterName = param.Name; Type parameterType; if (param.Value == null || param.Value == DBNull.Value) { parameter.Value = DBNull.Value; parameterType = param.Type; } else { parameter.Value = param.Value; parameterType = param.Value.GetType(); } if (param.Precision != null) { parameter.Precision = param.Precision.Value; } if (param.Scale != null) { parameter.Scale = param.Scale.Value; } if (param.Size != null) { parameter.Size = param.Size.Value; } if (param.DbType != null) { parameter.DbType = param.DbType.Value; } else { DbType?dbType = MappingTypeSystem.GetDbType(parameterType); if (dbType != null) { parameter.DbType = dbType.Value; } } const int defaultSizeOfStringOutputParameter = 4000;/* 当一个 string 类型输出参数未显示指定 Size 时使用的默认大小。如果有需要更大或者该值不足以满足需求,需显示指定 DbParam.Size 值 */ OutputParameter outputParameter = null; if (param.Direction == ParamDirection.Input) { parameter.Direction = ParameterDirection.Input; } else if (param.Direction == ParamDirection.Output) { parameter.Direction = ParameterDirection.Output; param.Value = null; if (param.Size == null && param.Type == UtilConstants.TypeOfString) { parameter.Size = defaultSizeOfStringOutputParameter; } outputParameter = new OutputParameter(param, parameter); } else if (param.Direction == ParamDirection.InputOutput) { parameter.Direction = ParameterDirection.InputOutput; if (param.Size == null && param.Type == UtilConstants.TypeOfString) { parameter.Size = defaultSizeOfStringOutputParameter; } outputParameter = new OutputParameter(param, parameter); } else { throw new NotSupportedException(string.Format("ParamDirection '{0}' is not supported.", param.Direction)); } cmd.Parameters.Add(parameter); if (outputParameter != null) { if (outputParameters == null) { outputParameters = new List <OutputParameter>(); } outputParameters.Add(outputParameter); } } } return(cmd); }
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); }