Solved

VB.NET Get SQLDBType ENUM from String

Posted on 2004-04-13
6
2,492 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

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…
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now