博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数据库和c#类型映射
阅读量:5970 次
发布时间:2019-06-19

本文共 8631 字,大约阅读时间需要 28 分钟。

注意:

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; } }}// ========================================================

 

 

 

 

转载地址:http://xkwox.baihongyu.com/

你可能感兴趣的文章
CSU Double Shortest Paths 湖南省第十届省赛
查看>>
webgl像机世界
查看>>
php正则怎么使用(最全最细致)
查看>>
javascript数学运算符
查看>>
LC.155. Min Stack(非优化,两个stack 同步 + -)
查看>>
交互设计[3]--点石成金
查看>>
SCCM TP4部署Office2013
查看>>
Android创建启动画面
查看>>
Linux中date命令的各种实用方法--转载
查看>>
mysqld -install命令时出现install/remove of the service denied错误的原因和解决办法
查看>>
苹果企业版帐号申请记录
查看>>
C++ Error: error LNK2019: unresolved external symbol
查看>>
Bitmap 和Drawable 的区别
查看>>
Java操作mongoDB2.6的常见API使用方法
查看>>
如何给服务器设置邮件警报。
查看>>
麦克劳林
查看>>
Eclipse SVN修改用户名和密码
查看>>
架构师的职责都有哪些?
查看>>
SVN: bdb: BDB1538 Program version 5.3 doesn't match environment version 4.7
查看>>
jsp内置对象作业3-application用户注册
查看>>