using System; using System.Data; namespace Dapper { /// /// This class represents a SQL string, it can be used if you need to denote your parameter is a Char vs VarChar vs nVarChar vs nChar /// public sealed class DbString : SqlMapper.ICustomQueryParameter { /// /// Default value for IsAnsi. /// public static bool IsAnsiDefault { get; set; } /// /// A value to set the default value of strings /// going through Dapper. Default is 4000, any value larger than this /// field will not have the default value applied. /// public const int DefaultLength = 4000; /// /// Create a new DbString /// public DbString() { Length = -1; IsAnsi = IsAnsiDefault; } /// /// Ansi vs Unicode /// public bool IsAnsi { get; set; } /// /// Fixed length /// public bool IsFixedLength { get; set; } /// /// Length of the string -1 for max /// public int Length { get; set; } /// /// The value of the string /// public string Value { get; set; } /// /// Add the parameter to the command... internal use only /// /// /// 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; #pragma warning disable 0618 param.Value = SqlMapper.SanitizeParameterValue(Value); #pragma warning restore 0618 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); } } }