convert DataColumn.GetType() into a DbType or SqlDbType?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Bordeman

    convert DataColumn.GetType() into a DbType or SqlDbType?

    I have a DataColumn, want to derive the DbType. I can do column.GetType( )
    but that's a system type, not a db type. How do I convert it to the
    corresponding type?

    Thanks much!

    Chris B.


  • Chester

    #2
    RE: convert DataColumn.GetT ype() into a DbType or SqlDbType?

    Hi Chris Bordeman,

    Its not possible to get the DBType via GetType(). You need to have a mapping
    to get the relevent type. For eg,

    private static Hashtable dbTypeTable;

    private SqlDbType ConvertToDbType (Type t)
    {
    if(dbTypeTable == null)
    {
    dbTypeTable = new Hashtable();
    dbTypeTable.Add (typeof(System. Boolean), SqlDbType.Bit);
    dbTypeTable.Add (typeof(System. Int16), SqlDbType.Small Int);
    dbTypeTable.Add (typeof(System. Int32), SqlDbType.Int);
    dbTypeTable.Add (typeof(System. Int64), SqlDbType.BigIn t);
    dbTypeTable.Add (typeof(System. Double), SqlDbType.Float );
    dbTypeTable.Add (typeof(System. Decimal), SqlDbType.Decim al);
    dbTypeTable.Add (typeof(System. String), SqlDbType.VarCh ar);
    dbTypeTable.Add (typeof(System. DateTime), SqlDbType.DateT ime);
    dbTypeTable.Add (typeof(System. Byte[]), SqlDbType.VarBi nary);
    dbTypeTable.Add (typeof(System. Guid), SqlDbType.Uniqu eIdentifier);
    }
    SqlDbType dbtype;
    try
    {
    dbtype = (SqlDbType)dbTy peTable[t];
    }
    catch
    {
    dbtype = SqlDbType.Varia nt;
    }
    return dbtype;
    }

    can be used to get the type.

    Cheers,
    Chester


    "Chris Bordeman" wrote:
    I have a DataColumn, want to derive the DbType. I can do column.GetType( )
    but that's a system type, not a db type. How do I convert it to the
    corresponding type?
    >
    Thanks much!
    >
    Chris B.
    >
    >
    >

    Comment

    • Chris Bordeman

      #3
      Re: convert DataColumn.GetT ype() into a DbType or SqlDbType?

      Gotcha, thanks for the mappings.

      "Chester" <chestermr@comm unity.nospamwro te in message
      news:08FDC800-AF84-4D12-8644-A1B76D807734@mi crosoft.com...
      Hi Chris Bordeman,
      >
      Its not possible to get the DBType via GetType(). You need to have a
      mapping
      to get the relevent type. For eg,
      >
      private static Hashtable dbTypeTable;
      >
      private SqlDbType ConvertToDbType (Type t)
      {
      if(dbTypeTable == null)
      {
      dbTypeTable = new Hashtable();
      dbTypeTable.Add (typeof(System. Boolean), SqlDbType.Bit);
      dbTypeTable.Add (typeof(System. Int16), SqlDbType.Small Int);
      dbTypeTable.Add (typeof(System. Int32), SqlDbType.Int);
      dbTypeTable.Add (typeof(System. Int64), SqlDbType.BigIn t);
      dbTypeTable.Add (typeof(System. Double), SqlDbType.Float );
      dbTypeTable.Add (typeof(System. Decimal), SqlDbType.Decim al);
      dbTypeTable.Add (typeof(System. String), SqlDbType.VarCh ar);
      dbTypeTable.Add (typeof(System. DateTime), SqlDbType.DateT ime);
      dbTypeTable.Add (typeof(System. Byte[]), SqlDbType.VarBi nary);
      dbTypeTable.Add (typeof(System. Guid), SqlDbType.Uniqu eIdentifier);
      }
      SqlDbType dbtype;
      try
      {
      dbtype = (SqlDbType)dbTy peTable[t];
      }
      catch
      {
      dbtype = SqlDbType.Varia nt;
      }
      return dbtype;
      }
      >
      can be used to get the type.
      >
      Cheers,
      Chester
      >
      >
      "Chris Bordeman" wrote:
      >
      >I have a DataColumn, want to derive the DbType. I can do
      >column.GetType ()
      >but that's a system type, not a db type. How do I convert it to the
      >correspondin g type?
      >>
      >Thanks much!
      >>
      >Chris B.
      >>
      >>
      >>

      Comment

      • Rahul

        #4
        Re: convert DataColumn.GetT ype() into a DbType or SqlDbType?

        Hi Chester,
        I was also trying to do the same in my application. But i got stuck
        with few things.

        Like if you have a currency column or a nvarchar column the gettype
        function will return you decimal and string respectively.

        Just wanted to check if you know have a work around for this.

        Thanks,
        Rahul

        Chester wrote:
        Hi Chris Bordeman,
        >
        Its not possible to get the DBType via GetType(). You need to have a mapping
        to get the relevent type. For eg,
        >
        private static Hashtable dbTypeTable;
        >
        private SqlDbType ConvertToDbType (Type t)
        {
        if(dbTypeTable == null)
        {
        dbTypeTable = new Hashtable();
        dbTypeTable.Add (typeof(System. Boolean), SqlDbType.Bit);
        dbTypeTable.Add (typeof(System. Int16), SqlDbType.Small Int);
        dbTypeTable.Add (typeof(System. Int32), SqlDbType.Int);
        dbTypeTable.Add (typeof(System. Int64), SqlDbType.BigIn t);
        dbTypeTable.Add (typeof(System. Double), SqlDbType.Float );
        dbTypeTable.Add (typeof(System. Decimal), SqlDbType.Decim al);
        dbTypeTable.Add (typeof(System. String), SqlDbType.VarCh ar);
        dbTypeTable.Add (typeof(System. DateTime), SqlDbType.DateT ime);
        dbTypeTable.Add (typeof(System. Byte[]), SqlDbType.VarBi nary);
        dbTypeTable.Add (typeof(System. Guid), SqlDbType.Uniqu eIdentifier);
        }
        SqlDbType dbtype;
        try
        {
        dbtype = (SqlDbType)dbTy peTable[t];
        }
        catch
        {
        dbtype = SqlDbType.Varia nt;
        }
        return dbtype;
        }
        >
        can be used to get the type.
        >
        Cheers,
        Chester
        >
        >
        "Chris Bordeman" wrote:
        >
        I have a DataColumn, want to derive the DbType. I can do column.GetType( )
        but that's a system type, not a db type. How do I convert it to the
        corresponding type?

        Thanks much!

        Chris B.

        Comment

        • Rahul

          #5
          Re: convert DataColumn.GetT ype() into a DbType or SqlDbType?

          Hi Chester,
          I was also trying to do the same in my application. But i got stuck
          with few things.

          Like if you have a currency column or a nvarchar column the gettype
          function will return you decimal and string respectively.

          Just wanted to check if you know any work-around for this.

          Thanks,
          Rahul

          Chester wrote:
          Hi Chris Bordeman,
          >
          Its not possible to get the DBType via GetType(). You need to have a mapping
          to get the relevent type. For eg,
          >
          private static Hashtable dbTypeTable;
          >
          private SqlDbType ConvertToDbType (Type t)
          {
          if(dbTypeTable == null)
          {
          dbTypeTable = new Hashtable();
          dbTypeTable.Add (typeof(System. Boolean), SqlDbType.Bit);
          dbTypeTable.Add (typeof(System. Int16), SqlDbType.Small Int);
          dbTypeTable.Add (typeof(System. Int32), SqlDbType.Int);
          dbTypeTable.Add (typeof(System. Int64), SqlDbType.BigIn t);
          dbTypeTable.Add (typeof(System. Double), SqlDbType.Float );
          dbTypeTable.Add (typeof(System. Decimal), SqlDbType.Decim al);
          dbTypeTable.Add (typeof(System. String), SqlDbType.VarCh ar);
          dbTypeTable.Add (typeof(System. DateTime), SqlDbType.DateT ime);
          dbTypeTable.Add (typeof(System. Byte[]), SqlDbType.VarBi nary);
          dbTypeTable.Add (typeof(System. Guid), SqlDbType.Uniqu eIdentifier);
          }
          SqlDbType dbtype;
          try
          {
          dbtype = (SqlDbType)dbTy peTable[t];
          }
          catch
          {
          dbtype = SqlDbType.Varia nt;
          }
          return dbtype;
          }
          >
          can be used to get the type.
          >
          Cheers,
          Chester
          >
          >
          "Chris Bordeman" wrote:
          >
          I have a DataColumn, want to derive the DbType. I can do column.GetType( )
          but that's a system type, not a db type. How do I convert it to the
          corresponding type?

          Thanks much!

          Chris B.

          Comment

          Working...