本文整理汇总了C#中TypeUsage.IsPrimitiveType方法的典型用法代码示例。如果您正苦于以下问题:C# TypeUsage.IsPrimitiveType方法的具体用法?C# TypeUsage.IsPrimitiveType怎么用?C# TypeUsage.IsPrimitiveType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TypeUsage
的用法示例。
在下文中一共展示了TypeUsage.IsPrimitiveType方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SetDbParameterValue
/// <summary>
/// Sets the parameter value and appropriate facets for the given <see cref="TypeUsage"/>.
/// </summary>
/// <param name="parameter">The parameter.</param>
/// <param name="parameterType">The type of the parameter.</param>
/// <param name="value">The value of the parameter.</param>
protected override void SetDbParameterValue(DbParameter parameter, TypeUsage parameterType, object value)
{
Check.NotNull(parameter, "parameter");
Check.NotNull(parameterType, "parameterType");
// Ensure a value that can be used with SqlParameter
value = EnsureSqlParameterValue(value);
if (parameterType.IsPrimitiveType(PrimitiveTypeKind.String)
|| parameterType.IsPrimitiveType(PrimitiveTypeKind.Binary))
{
var size = GetParameterSize(parameterType, ((parameter.Direction & ParameterDirection.Output) == ParameterDirection.Output));
if (!size.HasValue)
{
// Remember the current Size
var previousSize = parameter.Size;
// Infer the Size from the value
parameter.Size = 0;
parameter.Value = value;
if (previousSize > -1)
{
// The 'max' length was chosen as a specific value for the parameter's Size property on Sql8 (4000 or 8000)
// because no MaxLength was specified in the TypeUsage and the provider is Sql8.
// If the value's length is less than or equal to this preset size, then the Size value can be retained,
// otherwise this preset size must be removed in favor of the Size inferred from the value itself.
// If the inferred Size is less than the preset 'max' size, restore that preset size
if (parameter.Size < previousSize)
{
parameter.Size = previousSize;
}
}
else
{
// -1 was chosen as the parameter's size because no MaxLength was specified in the TypeUsage and the
// provider is more recent than Sql8. However, it is more optimal to specify a non-max (-1) value for
// the size where possible, since 'max' parameters may prevent, for example, filter pushdown.
// (see Dev10#617447 for more details)
var suggestedLength = GetNonMaxLength(((SqlParameter)parameter).SqlDbType);
if (parameter.Size < suggestedLength)
{
parameter.Size = suggestedLength;
}
else if (parameter.Size > suggestedLength)
{
// The parameter size is greater than the suggested length, so the suggested length cannot be used.
// Since the provider is Sql9 or newer, set the size to max (-1) instead of the inferred size for better plan reuse.
parameter.Size = -1;
}
}
}
else
{
// Just set the value
parameter.Value = value;
}
}
else
{
// Not a string or binary parameter - just set the value
parameter.Value = value;
}
}