void AddParameter(global::System.Data.Common.DbCommand cm, string name, object value) { var param = cm.CreateParameter(); param.ParameterName = ":" + name; param.Value = value; cm.Parameters.Add(param); }
/// <summary> /// Add the parameter to the command... internal use only /// </summary> /// <param name="command"></param> /// <param name="name"></param> public void AddParameter(IDbCommand command, string name) { if (IsFixedLength && Length == -1) { throw new InvalidOperationException("If specifying IsFixedLength, a Length must also be specified"); } var param = command.CreateParameter(); param.ParameterName = name; param.Value = SqlMapper.SanitizeParameterValue(Value); if (Length == -1 && Value != null && Value.Length <= DefaultLength) { param.Size = DefaultLength; } else { param.Size = Length; } param.DbType = IsAnsi ? (IsFixedLength ? DbType.AnsiStringFixedLength : DbType.AnsiString) : (IsFixedLength ? DbType.StringFixedLength : DbType.String); command.Parameters.Add(param); }
/// <summary> /// Add all the parameters needed to the command just before it executes /// </summary> /// <param name="command">The raw command prior to execution</param> /// <param name="identity">Information about the query</param> protected void AddParameters(IDbCommand command, SqlMapper.Identity identity) { var literals = SqlMapper.GetLiteralTokens(identity.sql); if (templates != null) { foreach (var template in templates) { var newIdent = identity.ForDynamicParameters(template.GetType()); Action <IDbCommand, object> appender; lock (paramReaderCache) { if (!paramReaderCache.TryGetValue(newIdent, out appender)) { appender = SqlMapper.CreateParamInfoGenerator(newIdent, true, RemoveUnused, literals); paramReaderCache[newIdent] = appender; } } appender(command, template); } // The parameters were added to the command, but not the // DynamicParameters until now. foreach (IDbDataParameter param in command.Parameters) { // If someone makes a DynamicParameters with a template, // then explicitly adds a parameter of a matching name, // it will already exist in 'parameters'. if (!parameters.ContainsKey(param.ParameterName)) { parameters.Add(param.ParameterName, new ParamInfo { AttachedParam = param, CameFromTemplate = true, DbType = param.DbType, Name = param.ParameterName, ParameterDirection = param.Direction, Size = param.Size, Value = param.Value }); } } // Now that the parameters are added to the command, let's place our output callbacks var tmp = outputCallbacks; if (tmp != null) { foreach (var generator in tmp) { generator(); } } } foreach (var param in parameters.Values) { if (param.CameFromTemplate) { continue; } var dbType = param.DbType; var val = param.Value; string name = Clean(param.Name); var isCustomQueryParameter = val is SqlMapper.ICustomQueryParameter; SqlMapper.ITypeHandler handler = null; if (dbType == null && val != null && !isCustomQueryParameter) { dbType = SqlMapper.LookupDbType(val.GetType(), name, true, out handler); } if (isCustomQueryParameter) { ((SqlMapper.ICustomQueryParameter)val).AddParameter(command, name); } else if (dbType == DynamicParameters.EnumerableMultiParameter) { #pragma warning disable 612, 618 SqlMapper.PackListParameters(command, name, val); #pragma warning restore 612, 618 } else { bool add = !command.Parameters.Contains(name); IDbDataParameter p; if (add) { p = command.CreateParameter(); p.ParameterName = name; } else { p = (IDbDataParameter)command.Parameters[name]; } p.Direction = param.ParameterDirection; if (handler == null) { p.Value = SqlMapper.SanitizeParameterValue(val); if (dbType != null && p.DbType != dbType) { p.DbType = dbType.Value; } var s = val as string; if (s != null) { if (s.Length <= DbString.DefaultLength) { p.Size = DbString.DefaultLength; } } if (param.Size != null) { p.Size = param.Size.Value; } if (param.Precision != null) { p.Precision = param.Precision.Value; } if (param.Scale != null) { p.Scale = param.Scale.Value; } } else { if (dbType != null) { p.DbType = dbType.Value; } if (param.Size != null) { p.Size = param.Size.Value; } if (param.Precision != null) { p.Precision = param.Precision.Value; } if (param.Scale != null) { p.Scale = param.Scale.Value; } handler.SetValue(p, val ?? DBNull.Value); } if (add) { command.Parameters.Add(p); } param.AttachedParam = p; } } // note: most non-priveleged implementations would use: this.ReplaceLiterals(command); if (literals.Count != 0) { SqlMapper.ReplaceLiterals(this, command, literals); } }
/// <summary> /// Add the parameter to the command... internal use only /// </summary> /// <param name="command"></param> /// <param name="name"></param> public void AddParameter(IDbCommand command, string name) { if (IsFixedLength && Length == -1) { throw new InvalidOperationException("If specifying IsFixedLength, a Length must also be specified"); } var param = command.CreateParameter(); param.ParameterName = name; param.Value = SqlMapper.SanitizeParameterValue(Value); if (Length == -1 && Value != null && Value.Length <= DefaultLength) { param.Size = DefaultLength; } else { param.Size = Length; } param.DbType = IsAnsi ? (IsFixedLength ? DbType.AnsiStringFixedLength : DbType.AnsiString) : (IsFixedLength ? DbType.StringFixedLength : DbType.String); command.Parameters.Add(param); }