链接 TypeCode 和 SqlDbType 或 DbType

发布于 2024-12-11 07:46:59 字数 128 浏览 0 评论 0原文

有没有办法可以链接 TypeCode 枚举和 SqlDbType 枚举。 我计划,当任何类型的 c Sharp 中的特定变量识别其 TypeCode 时,我都有一个相应的 SqlDbType。我是否需要在数据库中创建表来满足我的要求。 TIA

Is there a way i can link TypeCode enums and SqlDbType enums.
I'm planning on, when a specific variable in c sharp of any type identify its TypeCode i have a corresponding SqlDbType. Do i need to create table in my database to suffice my requirements. T.I.A

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清浅ˋ旧时光 2024-12-18 07:46:59

这是获取任何给定对象的 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);

Here is a trick to get the SqlDbType value for any given object. (It's not based on the 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);
故事↓在人 2024-12-18 07:46:59

必须处理可能是 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);

实际上,它是一种罗塞塔石碑;))))

Having to deal with a generic DbCommand which could be SqlCommand or MySqlDbCommand, i created this structure:

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)
};

Scanning the tuple list linearly on the source "column", at the first occurrence i can find all the (mostly) corresponding equivalent types

That can be used in this way:

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);

It is a type rosetta stone, actually ;))))

花开半夏魅人心 2024-12-18 07:46:59

以下方法将 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;
        }
    }
}

}

The following method convert TypeCode to the corresponding SqlDbType.

If the TypeCode can't be mapped, 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;
        }
    }
}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文