コード例 #1
0
        private static string GetTypeSpecifierLabel(DataType dataType, UserDefinedDataTypeCollection uddts)
        {
            string typeName = string.Empty;

            if (dataType != null)
            {
                // typeSpecifier might still be in a resolve candidate status.  If so then the
                // name might be null.  Don't ask for the type specifier name in this case.
                typeName = dataType.Name;

                // This may return [dbo].[MyType], but for the purposes of display we only want MyType
                if (!string.IsNullOrWhiteSpace(typeName) &&
                    typeName.EndsWith("]", StringComparison.Ordinal))
                {
                    int nameStart = typeName.LastIndexOf('[');
                    typeName = typeName.Substring(nameStart + 1, typeName.Length - nameStart - 2);
                }

                if (dataType.SqlDataType == SqlDataType.UserDefinedDataType && uddts != null)
                {
                    foreach (UserDefinedDataType item in uddts)
                    {
                        if (item.Name == dataType.Name)
                        {
                            typeName += $"({item.SystemType})";
                            break;
                        }
                    }
                }

                // These types support Length
                switch (dataType.SqlDataType)
                {
                case SqlDataType.Char:
                case SqlDataType.NChar:
                case SqlDataType.Binary:
                case SqlDataType.VarChar:       // Supports Max Length
                case SqlDataType.NVarChar:      // Supports Max Length
                case SqlDataType.VarBinary:     // Supports Max Length
                    typeName += "(";
                    if (dataType.MaximumLength == 0)
                    {
                        typeName += "max";
                    }
                    else
                    {
                        typeName += dataType.MaximumLength;
                    }
                    typeName += ")";
                    break;

                case SqlDataType.Numeric:
                case SqlDataType.Decimal:
                    typeName += $"({dataType.NumericPrecision},{dataType.NumericScale})";
                    break;

                case SqlDataType.DateTime2:
                case SqlDataType.Time:
                case SqlDataType.DateTimeOffset:
                    typeName += $"({dataType.NumericScale})";
                    break;
                }
            }
            return(typeName);
        }