注意:
View Code
////// Returns the SQL type whose name is given, or Variant if the name is invalid. /// /// The SQL type name. /// True to raise an exception in case the name is invalid. ///The SQL type associated with the name given, or Variant as the default value. 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 { ////// Returns a new string containing the n left-most characters from the source string. /// /// The source string. /// The number of left-most characters. ///A new string containing the n left-most characters. 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; } ////// Returns a new string containing the n right-most characters from the source string. /// /// The source string. /// The number of right-most characters. ///A new string containing the n right-most characters. 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; } ////// Returns true if the target string contains any of the characters given. /// /// The target string. /// The array of characters to validate. ///True if the target string contains any of the characters given. 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; } ////// Returns a new validated string built from the source string using the rules given, or throws an exception is any /// error is found. /// /// The source string. /// A description of the source string used to build the exception messages. /// If the returned string can be null. /// True to trim the string before returning. /// True to trim for the start of the string. /// True to trim from the end of the string. /// The min valid lenght of the returned string. /// The max valid lenght of the returned string. /// The character to use to left pad the returned string. /// The character to use to right pad the returned string. /// If the returned string can be empty. /// An array containing the invalid characters. /// An array containing the only valid characters. ///A new validated string. 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; } }}// ========================================================