?
Solved

VB.NET Get SQLDBType ENUM from String

Posted on 2004-04-13
6
Medium Priority
?
2,581 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 37

Accepted Solution

by:
gregoryyoung earned 2000 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

Basic Security of Your VPC

So, you’ve got this shiny new VPC and a fancy new application configured on your EC2 servers ready to go. This application is only accessible from your computer, which is great for security, but you need your users to be able to access it! So, what’s the easiest way to do this?

Question has a verified solution.

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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
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 …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

752 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