Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VB.NET Get SQLDBType ENUM from String

Posted on 2004-04-13
6
Medium Priority
?
2,595 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

618 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