Skip to main content
 首页 » 编程设计

c#之是否可以使用泛型简化我的 dbtype

2024年02月20日11sharpest

我有一个结构,我想让它更容易填充

    public struct DBParameter 
    { 
        public string parameterName; 
        public object value; 
        public DbType dbType; 
 
        public DBParameter(string paramName, object val, DbType type) 
        { 
            parameterName = paramName; 
            value = val; 
            dbType = type; 
        } 
        public SqlParameter ToSqlParameter() 
        { 
            if (parameterName == string.Empty || parameterName == null) 
                throw new ArgumentException("No parameter, fieldname is mandatory", "parameterName"); 
 
            SqlParameter me = new SqlParameter(parameterName, value); 
            me.DbType = dbType; 
 
            return me; 
        } 

我在想这样的事情......

        public void LoadDBParameter<T>(string paramName, T val) 
        { 
            parameterName = paramName; 
            value = val; 
            if (val is Int32) 
            { 
                dbType = DbType.Int32; 
            } 
            if (val is String) 
            { 
                dbType = DbType.String; 
            } 
            if (val is Int64) 
            { 
                dbType = DbType.Int64; 
            } 
            if (true) 
            { 
                //(...) 
            } 
        } 
    } 

但我一直看到名称几乎相同,是否有比编写每个可能的通用数据结构更简单的方法?

请您参考如下方法:

要从“dapper”中窃取一些代码,我们只需这样做:

    static readonly Dictionary<Type, DbType> typeMap; 
 
    static SqlMapper() 
    { 
        typeMap = new Dictionary<Type, DbType>(); 
        typeMap[typeof(byte)] = DbType.Byte; 
        typeMap[typeof(sbyte)] = DbType.SByte; 
        typeMap[typeof(short)] = DbType.Int16; 
        typeMap[typeof(ushort)] = DbType.UInt16; 
        typeMap[typeof(int)] = DbType.Int32; 
        typeMap[typeof(uint)] = DbType.UInt32; 
        typeMap[typeof(long)] = DbType.Int64; 
        typeMap[typeof(ulong)] = DbType.UInt64; 
        typeMap[typeof(float)] = DbType.Single; 
        typeMap[typeof(double)] = DbType.Double; 
        typeMap[typeof(decimal)] = DbType.Decimal; 
        typeMap[typeof(bool)] = DbType.Boolean; 
        typeMap[typeof(string)] = DbType.String; 
        typeMap[typeof(char)] = DbType.StringFixedLength; 
        typeMap[typeof(Guid)] = DbType.Guid; 
        typeMap[typeof(DateTime)] = DbType.DateTime; 
        typeMap[typeof(DateTimeOffset)] = DbType.DateTimeOffset; 
        typeMap[typeof(TimeSpan)] = DbType.Time; 
        typeMap[typeof(byte[])] = DbType.Binary; 
        typeMap[typeof(byte?)] = DbType.Byte; 
        typeMap[typeof(sbyte?)] = DbType.SByte; 
        typeMap[typeof(short?)] = DbType.Int16; 
        typeMap[typeof(ushort?)] = DbType.UInt16; 
        typeMap[typeof(int?)] = DbType.Int32; 
        typeMap[typeof(uint?)] = DbType.UInt32; 
        typeMap[typeof(long?)] = DbType.Int64; 
        typeMap[typeof(ulong?)] = DbType.UInt64; 
        typeMap[typeof(float?)] = DbType.Single; 
        typeMap[typeof(double?)] = DbType.Double; 
        typeMap[typeof(decimal?)] = DbType.Decimal; 
        typeMap[typeof(bool?)] = DbType.Boolean; 
        typeMap[typeof(char?)] = DbType.StringFixedLength; 
        typeMap[typeof(Guid?)] = DbType.Guid; 
        typeMap[typeof(DateTime?)] = DbType.DateTime; 
        typeMap[typeof(DateTimeOffset?)] = DbType.DateTimeOffset; 
        typeMap[typeof(TimeSpan?)] = DbType.Time; 
        typeMap[typeof(Object)] = DbType.Object; 
    } 
 
    internal static DbType LookupDbType(Type type, string name) 
    { 
        DbType dbType; 
        var nullUnderlyingType = Nullable.GetUnderlyingType(type); 
        if (nullUnderlyingType != null) type = nullUnderlyingType; 
        if (type.IsEnum) 
        { 
            type = Enum.GetUnderlyingType(type); 
        } 
        if (typeMap.TryGetValue(type, out dbType)) 
        { 
            return dbType; 
        } 
        ... what to do if no match 
    }