簡體   English   中英

鏈接 TypeCode 和 SqlDbType 或 DbType

[英]Linking TypeCode and SqlDbType or DbType

有沒有辦法可以鏈接 TypeCode 枚舉和 SqlDbType 枚舉。 我正在計划,當任何類型的 c 銳利中的特定變量標識其 TypeCode 時,我有一個相應的 SqlDbType。 我是否需要在我的數據庫中創建表來滿足我的要求。 TIA

這是獲取任何給定對象的 SqlDbType 值的技巧。 (它不是基於 TypeCode):

// create an SQL Parameter object
SqlParameter p = new SqlParameter("dummy", myObj);

// ask SQL code to compute its SqlDbType for us
Console.WriteLine(p.SqlDbType);

以下方法將TypeCode轉換為相應的SqlDbType

如果無法映射TypeCode ,則 error = true;

public static SqlDbType Typecode2SqlDbType(TypeCode typecode, out bool error)
{
    error=false;
    switch (typecode)
    {
        case TypeCode.Empty     :
             return SqlDbType.Variant   ;
        case TypeCode.Object    :
             return SqlDbType.Variant   ;
        case TypeCode.DBNull    :
             return SqlDbType.Variant   ;
        case TypeCode.Boolean   :
             return SqlDbType.Bit       ;
        case TypeCode.Char      :
             return SqlDbType.NChar     ;

        case TypeCode.Byte      :
             return SqlDbType.TinyInt   ;
        case TypeCode.Int16     :
             return SqlDbType.SmallInt  ;

        case TypeCode.Int32     :
             return SqlDbType.Int       ;

        case TypeCode.Int64     :
             return SqlDbType.BigInt    ;

        case TypeCode.Single    :
             return SqlDbType.Real      ;
        case TypeCode.Double    :
             return SqlDbType.Float     ;
        case TypeCode.Decimal   :
             return SqlDbType.Decimal   ;
        case TypeCode.DateTime  :
             return SqlDbType.DateTime  ;
        case TypeCode.String    :
             return SqlDbType.NVarChar  ;

        // can't map TypeCode.SByte  
        // can't map TypeCode.UInt16  
        // can't map TypeCode.UInt32  
        // can't map TypeCode.UInt64  
        default:
        {
            error =true;
            return SqlDbType.NVarChar;
        }
    }
}

}

必須處理可能是 SqlCommand 或 MySqlDbCommand 的通用 DbCommand,我創建了以下結構:

static readonly (Type type, DbType dbType, SqlDbType sqlDbType, MySqlDbType mySqlDbType)[] dbTypesRosetta = new[]
{
    (typeof(string), DbType.String, SqlDbType.VarChar, MySqlDbType.VarString),
    (typeof(string), DbType.String, SqlDbType.Text, MySqlDbType.LongText),
    (typeof(string), DbType.String, SqlDbType.Char, MySqlDbType.String),
    (typeof(char), DbType.StringFixedLength, SqlDbType.Char, MySqlDbType.String),
    (typeof(byte), DbType.Byte, SqlDbType.TinyInt, MySqlDbType.Byte),
    (typeof(short), DbType.Int16, SqlDbType.SmallInt, MySqlDbType.Int16),
    (typeof(int), DbType.Int32, SqlDbType.Int, MySqlDbType.Int32),
    (typeof(long), DbType.Int64, SqlDbType.BigInt, MySqlDbType.Int64),
    (typeof(byte[]), DbType.Binary, SqlDbType.Image, MySqlDbType.LongBlob), //Up to 8000 bytes for DbType
    (typeof(bool), DbType.Boolean, SqlDbType.Bit, MySqlDbType.Bit),
    (typeof(decimal), DbType.Currency, SqlDbType.Money, MySqlDbType.Decimal),
    (typeof(decimal), DbType.Decimal, SqlDbType.Decimal, MySqlDbType.Decimal),
    (typeof(float), DbType.Single, SqlDbType.Real, MySqlDbType.Float),
    (typeof(double), DbType.Double, SqlDbType.Float, MySqlDbType.Double),
    (typeof(TimeSpan), DbType.Time, SqlDbType.Time, MySqlDbType.Time),
    (typeof(DateTime), DbType.DateTime, SqlDbType.DateTime, MySqlDbType.DateTime),
    (typeof(DateTime), DbType.Date, SqlDbType.Date, MySqlDbType.Date),
    (typeof(DateTimeOffset), DbType.DateTimeOffset, SqlDbType.DateTimeOffset, MySqlDbType.DateTime),
    (typeof(byte[]), DbType.Binary, SqlDbType.Timestamp, MySqlDbType.Binary),
    (typeof(Guid), DbType.Guid, SqlDbType.UniqueIdentifier, MySqlDbType.Guid)
};

在源“列”上線性掃描元組列表,在第一次出現時我可以找到所有(大部分)對應的等效類型

可以這樣使用:

string s;
SqlDbType t = dbTypesRosetta.First(map => map.type == typeof(s)).sqlDbType;
//OR
MySqlDbType t = dbTypesRosetta.First(map => map.sqlDbType == SqlDbType.Int32).mySqlDbType;
//OR for creating a method like this:
public static DbCommand SetParamOutput<T>(this DbCommand cmd, string paramName, out DbParameter paramOutput)
    => SetParamOutput(cmd, paramName, dbTypesRosetta.First(map => map.type == typeof(T)).dbType, out paramOutput);

它實際上是一種羅塞塔石碑;))))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM