Solved

VB.NET Get SQLDBType ENUM from String

Posted on 2004-04-13
6
2,517 Views
Last Modified: 2007-12-19
I am trying to convert a string to SQLDBType Enum.  I have looked and looked at this and cannot seem to make it work (only examples I can find are in C#)  Someone have the syntax for this?

Sample:
Dim SQLDB As SqlDbType = [Enum].Parse(System.Type.GetType("SqlDbType"), "varchar", True).GetType.ToString
0
Comment
Question by:dmoring
  • 5
6 Comments
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10816059
you're gonna have a bunch of functions like ...

            public static Type GetTypeForSqlType(SqlDbType _Type) {
                  switch(_Type) {
                        case SqlDbType.BigInt :
                              return typeof(System.Int64) ;
                        case SqlDbType.Bit :
                              return typeof(System.Boolean);
                        case SqlDbType.Char :
                              return typeof(string);
                        case SqlDbType.DateTime :
                              return typeof(System.DateTime);
                        case SqlDbType.Decimal :
                              return typeof(System.Double);
                        case SqlDbType.Float:
                              return typeof(System.Double);
                        case SqlDbType.Image:
                              return typeof(byte []);
                        case SqlDbType.Int:
                              return typeof(System.Int32);
                        case SqlDbType.Money:
                              return typeof(System.Decimal);
                        case SqlDbType.NChar:
                              return typeof(string);
                        case SqlDbType.NText:
                              return typeof(string);
                        case SqlDbType.NVarChar:
                              return typeof(string);
                        case SqlDbType.Real:
                              return typeof(System.Single);
                        case SqlDbType.SmallDateTime:
                              return typeof(System.DateTime);
                        case SqlDbType.SmallInt:
                              return typeof(System.Int16);
                        case SqlDbType.SmallMoney:
                              return typeof(System.Decimal);
                        case SqlDbType.Text:
                              return typeof(string) ;
                        case SqlDbType.Timestamp:
                              return typeof(System.DateTime) ;
                        case SqlDbType.TinyInt:
                              return typeof(byte);
                        case SqlDbType.UniqueIdentifier:
                              return typeof(Guid);
                        case SqlDbType.VarBinary:
                              return typeof(byte []);
                        case SqlDbType.VarChar:
                              return typeof(string);
                        case SqlDbType.Variant:
                              return typeof(object);
                        default :
                              throw new System.Exception("Unknown SqlType " + _Type);
                  }
            }
            
            public static SqlDbType TranslateSqlType(string _Type) {
                  switch(_Type.ToLower()) {
                        case "smallint" :
                              return SqlDbType.SmallInt ;
                        case "int" :
                              return SqlDbType.Int ;
                        case "bigint" :
                              return SqlDbType.BigInt ;
                        case "tinyint" :
                              return SqlDbType.TinyInt ;
                        case "float" :
                              return SqlDbType.Float ;
                        case "real" :
                              return SqlDbType.Real ;
                        case "decimal" :
                              return SqlDbType.Decimal ;
                        case "ntext" :
                              return SqlDbType.NText ;
                        case "nchar" :
                              return SqlDbType.NChar ;
                        case "nvarchar" :
                              return SqlDbType.NVarChar;
                        case "uniqueidentifer" :
                              return SqlDbType.UniqueIdentifier;
                        case "image" :
                              return SqlDbType.Image;
                        case "timestamp":
                              return SqlDbType.Timestamp ;
                        case "binary" :
                              return SqlDbType.Binary;
                        case "varbinary" :
                              return SqlDbType.VarBinary ;
                        case "char" :
                              return SqlDbType.Char ;
                        case "datetime" :
                              return SqlDbType.DateTime ;
                        case "smalldatetime" :
                              return SqlDbType.SmallDateTime ;
                        case "varchar" :
                              return SqlDbType.VarChar;
                        case "bit" :
                              return SqlDbType.Bit ;
                        case "money" :
                              return SqlDbType.Money ;
                        case "smallmoney" :
                              return SqlDbType.SmallMoney ;
                        case "text" :
                              return SqlDbType.Text ;
                        case "numeric" :
                              return SqlDbType.Decimal;
                        default :
                              throw new System.Exception("Unknown SQL Type " + _Type) ;
                  }
            }
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10816076
I did it your way to start but eventually gave up due to trying to support other database etc...

you can make it work with SQLServer  only though
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10816154
also the code above is trying to put a string into the enum which isnt right ...

 SqlDbType ret = (SqlDbType) Enum.Parse(typeof(SqlDbType), _Type, true); is the correct C# code

so VB is ...

dim ret as SqlDbType = directcast( Enum.Parse(TypeOf(SqlDbType), _Type, true) ,SqlDbType)
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 37

Accepted Solution

by:
gregoryyoung earned 500 total points
ID: 10816246
my vb is way off latelyhavnt worked in it for too long ...


        Dim ret As SqlDbType
        ret = DirectCast([Enum].Parse(ret.GetType(), _Type, True), SqlDbType)
0
 

Author Comment

by:dmoring
ID: 10817793
Yup... direct cast worked!  Thanks (again!)
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10817841
no thank you ! I just got to replace about 40 lines of code with 1 in my DAL :)
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question