DbType與Type之間的轉換(object)

來源:互聯網
上載者:User

從Type轉換到DB

public DbType TypeToDbType(Type t){    DbType dbt;    try    {        dbt = (DbType)Enum.Parse(typeof(DbType), t.Name);    }    catch    {        dbt = DbType.Object;    }    return dbt;} 

DB轉換到Type沒有直接的方式,只能通過判斷

 Type ConvertType(DbType dbType){    Type toReturn = typeof(DBNull);     switch (dbType)    {        case DbType.UInt64:            toReturn = typeof(UInt64);            break;         case DbType.Int64:            toReturn = typeof(Int64);            break;         case DbType.Int32:            toReturn = typeof(Int32);            break;         case DbType.UInt32:            toReturn = typeof(UInt32);            break;         case DbType.Single:            toReturn = typeof(float);            break;         case DbType.Date:        case DbType.DateTime:        case DbType.Time:            toReturn = typeof(DateTime);            break;         case DbType.String:        case DbType.StringFixedLength:        case DbType.AnsiString:        case DbType.AnsiStringFixedLength:            toReturn = typeof(string);            break;         case DbType.UInt16:            toReturn = typeof(UInt16);            break;         case DbType.Int16:            toReturn = typeof(Int16);            break;         case DbType.SByte:            toReturn = typeof(byte);            break;         case DbType.Object:            toReturn = typeof(object);            break;         case DbType.VarNumeric:        case DbType.Decimal:            toReturn = typeof(decimal);            break;         case DbType.Currency:            toReturn = typeof(double);            break;         case DbType.Binary:            toReturn = typeof(byte[]);            break;         case DbType.Double:            toReturn = typeof(Double);            break;         case DbType.Guid:            toReturn = typeof(Guid);            break;         case DbType.Boolean:            toReturn = typeof(bool);            break;    }     return toReturn;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.