Convert string to sqldbtype

  • Thread starter Thread starter B.S. van Veen
  • Start date Start date
B

B.S. van Veen

Is there another easier way to convert a string to a sqldbtype than using a select case, as in:

Namespace ADOHelper
Public Class sqldbtype
Public Shared Function Convert(ByVal type As String) As System.Data.SqlDbType
Select Case type
Case "int"
Return System.Data.SqlDbType.Int
Case "NVarChar"
Return System.Data.SqlDbType.NVarChar
Case "VarChar"
Return System.Data.SqlDbType.VarChar
Etc .........
 
Hi,

You might loop through enums and compare the part of the string (out of my hat):
foreach (System.Data.SqlDbType db in Enum.GetValues(typeof(System.Data.SqlDbType))
if (db.ToString().IndexOf(type) >=0)
return db;

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com
Is there another easier way to convert a string to a sqldbtype than using a select case, as in:

Namespace ADOHelper
Public Class sqldbtype
Public Shared Function Convert(ByVal type As String) As System.Data.SqlDbType
Select Case type
Case "int"
Return System.Data.SqlDbType.Int
Case "NVarChar"
Return System.Data.SqlDbType.NVarChar
Case "VarChar"
Return System.Data.SqlDbType.VarChar
Etc .........
 
Another way is using Enum.Parse method
SqlDbType sd = (SqlDbType) Enum.Parse(typeof(SqlDbType),type);
--
HTH,
Sushil Chordia.
This posting is provided "AS IS" with no warranties, and confers no rights.
"Miha Markic [MVP C#]" <miha at rthand com> wrote in message
Hi,

You might loop through enums and compare the part of the string (out of my
hat):
foreach (System.Data.SqlDbType db in
Enum.GetValues(typeof(System.Data.SqlDbType))
if (db.ToString().IndexOf(type) >=0)
return db;

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com
Is there another easier way to convert a string to a sqldbtype than using a
select case, as in:

Namespace ADOHelper
Public Class sqldbtype
Public Shared Function Convert(ByVal type As String) As
System.Data.SqlDbType
Select Case type
Case "int"
Return System.Data.SqlDbType.Int
Case "NVarChar"
Return System.Data.SqlDbType.NVarChar
Case "VarChar"
Return System.Data.SqlDbType.VarChar
Etc .........
 
Back
Top