Solved

VB.NET Get SQLDBType ENUM from String

Posted on 2004-04-13
6
2,535 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET 2008 Winforms Signing 13 42
Add a Condition in ASP Repeater 6 48
Change to file doesn't show up 16 80
Selenium: Transitioning from unit testing to automated testing 4 62
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

726 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