数据库和c#类型映射

注意:

View Code
/// <summary>
        /// Returns the SQL type whose name is given, or Variant if the name is invalid.
        /// </summary>
        /// <param name="dbTypeName">The SQL type name.</param>
        /// <param name="raise">True to raise an exception in case the name is invalid.</param>
        /// <returns>The SQL type associated with the name given, or Variant as the default value.</returns>
        public static SqlDbType ParseDbType( string dbTypeName, bool raise = true )
        {
            dbTypeName = dbTypeName.Validated( "SQL Type name" );

            switch( dbTypeName.ToLower() ) {
                case "bigint": return SqlDbType.BigInt;
                case "binary": return SqlDbType.Binary;
                case "bit": return SqlDbType.Bit;
                case "char": return SqlDbType.Char;
                case "date": return SqlDbType.Date;
                case "datetime": return SqlDbType.DateTime;
                case "datetime2": return SqlDbType.DateTime2;
                case "datetimeoffset": return SqlDbType.DateTimeOffset;
                case "numeric":
                case "decimal": return SqlDbType.Decimal;
                case "float": return SqlDbType.Float;
                case "image": return SqlDbType.Image;
                case "int": return SqlDbType.Int;
                case "money": return SqlDbType.Money;
                case "nchar": return SqlDbType.NChar;
                case "ntext": return SqlDbType.NText;
                case "nvarchar": return SqlDbType.NVarChar;
                case "real": return SqlDbType.Real;
                case "smalldatetime": return SqlDbType.SmallDateTime;
                case "smallint": return SqlDbType.SmallInt;
                case "smallmoney": return SqlDbType.SmallMoney;
                case "structured": return SqlDbType.Structured;
                case "text": return SqlDbType.Text;
                case "time": return SqlDbType.Time;
                case "timestamp": return SqlDbType.Timestamp;
                case "tinyint": return SqlDbType.TinyInt;
                case "udt": return SqlDbType.Udt;
                case "uniqueidentifier": return SqlDbType.UniqueIdentifier;
                case "varbinary": return SqlDbType.VarBinary;
                case "varchar": return SqlDbType.VarChar;
                case "sql_variant":
                case "variant": return SqlDbType.Variant;
                case "xml": return SqlDbType.Xml;
            }
            if( raise ) throw new ArgumentException( "SqlDbType not found: " + dbTypeName );
            return SqlDbType.Variant;
        }

 

// ========================================================
namespace MB.Tools
{
    using global::System;
    using global::System.Text;

    // =====================================================
    public static class StringHelper
    {
        /// <summary>
        /// Returns a new string containing the n left-most characters from the source string.
        /// </summary>
        /// <param name="source">The source string.</param>
        /// <param name="n">The number of left-most characters.</param>
        /// <returns>A new string containing the n left-most characters.</returns>
        public static string Left( this string source, int n )
        {
            if( source == null ) throw new ArgumentNullException( "source", "Souce string cannot be null." );
            if( n < 0 ) throw new ArgumentException( string.Format( "Number of characters cannot be less than cero: {0}." + n ) );

            if( n == 0 ) return string.Empty;
            string s = ( source.Length != 0 && n < source.Length ) ? source.Substring( 0, n ) : source;
            return s;
        }

        /// <summary>
        /// Returns a new string containing the n right-most characters from the source string.
        /// </summary>
        /// <param name="source">The source string.</param>
        /// <param name="n">The number of right-most characters.</param>
        /// <returns>A new string containing the n right-most characters.</returns>
        public static string Right( this string source, int n )
        {
            if( source == null ) throw new ArgumentNullException( "source", "Souce string cannot be null." );
            if( n < 0 ) throw new ArgumentException( string.Format( "Number of characters cannot be less than cero: {0}." + n ) );

            if( n == 0 ) return string.Empty;
            string s = ( source.Length != 0 && n < source.Length ) ? source.Substring( source.Length - n ) : source;
            return s;
        }

        /// <summary>
        /// Returns true if the target string contains any of the characters given.
        /// </summary>
        /// <param name="target">The target string.</param>
        /// <param name="array">The array of characters to validate.</param>
        /// <returns>True if the target string contains any of the characters given.</returns>
        public static bool ContainsAny( this string target, char[] array )
        {
            if( target == null ) throw new ArgumentNullException( "target", "Target string cannot be null." );
            if( array == null || array.Length == 0 ) return false; // No characters to validate

            int ix = target.IndexOfAny( array );
            return ix >= 0 ? true : false;
        }

        /// <summary>
        /// Returns a new validated string built from the source string using the rules given, or throws an exception is any
        /// error is found.
        /// </summary>
        /// <param name="source">The source string.</param>
        /// <param name="desc">A description of the source string used to build the exception messages.</param>
        /// <param name="canbeNull">If the returned string can be null.</param>
        /// <param name="trim">True to trim the string before returning.</param>
        /// <param name="trimStart">True to trim for the start of the string.</param>
        /// <param name="trimEnd">True to trim from the end of the string.</param>
        /// <param name="minLen">The min valid lenght of the returned string.</param>
        /// <param name="maxLen">The max valid lenght of the returned string.</param>
        /// <param name="padLeft">The character to use to left pad the returned string.</param>
        /// <param name="padRight">The character to use to right pad the returned string.</param>
        /// <param name="canbeEmpty">If the returned string can be empty.</param>
        /// <param name="invalidChars">An array containing the invalid characters.</param>
        /// <param name="validChars">An array containing the only valid characters.</param>
        /// <returns>A new validated string.</returns>
        public static string Validated( this string source, string desc = null,
            bool canbeNull = false, bool trim = true, bool trimStart = false, bool trimEnd = false,
            int minLen = -1, int maxLen = -1, char padLeft = '\0', char padRight = '\0', bool canbeEmpty = false,
            char[] invalidChars = null, char[] validChars = null )
        {
            // Assuring a valid descriptor...
            if( string.IsNullOrWhiteSpace( desc ) ) desc = "Source";

            // Validating if null sources are accepted...
            if( source == null ) {
                if( !canbeNull ) throw new ArgumentNullException( "source", string.Format( "{0} cannot be null.", desc ) );
                return null;
            }

            // Trimming if needed...
            if( trim && !( trimStart || trimEnd ) ) source = source.Trim();
            else {
                if( trimStart ) source = source.TrimStart( ' ' );
                if( trimEnd ) source = source.TrimEnd( ' ' );
            }

            // Adjusting lenght...
            if( minLen > 0 ) {
                if( padLeft != '\0' ) source = source.PadLeft( minLen, padLeft );
                if( padRight != '\0' ) source = source.PadRight( minLen, padRight );
            }
            if( maxLen > 0 ) {
                if( padLeft != '\0' ) source = source.PadLeft( maxLen, padLeft );
                if( padRight != '\0' ) source = source.PadRight( maxLen, padRight );
            }

            // Validating emptyness and lenghts...
            if( source.Length == 0 ) {
                if( !canbeEmpty ) throw new ArgumentException( desc + " cannot be empty." );
                return string.Empty;
            }
            if( minLen >= 0 && source.Length < minLen ) throw new ArgumentException( string.Format( "{0} '{1}' lenghth is lower than: {2}.", desc, source, minLen ) );
            if( maxLen >= 0 && source.Length > maxLen ) throw new ArgumentException( string.Format( "{0} '{1}' lenghth is bigger than: {2}.", desc, source, maxLen ) );

            // Checking invalid chars...
            if( invalidChars != null ) {
                int n = source.IndexOfAny( invalidChars );
                if( n >= 0 ) throw new ArgumentException( string.Format( "Invalid character '{0}' found in {1}: {2}", source[n], desc, source ) );
            }

            // Checking valid chars...
            if( validChars != null ) {
                int n = validChars.ToString().IndexOfAny( source.ToCharArray() );
                if( n >= 0 ) throw new ArgumentException( string.Format( "Invalid character '{0}' found in {1}: {2}", validChars.ToString()[n], desc, source ) );
            }

            return source;
        }
    }
}
// ========================================================

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值