Convert String to SqlDbType

I am trying to convert string values ​​to SqlDbType. My code is able to hide the "Text" in SqlDbType.Text without any errors, but when I try to convert the "bit" to SqlDbType.Bit, I get the following error: "Required value" bit "not found".

The same thing happens when trying to convert "int" to SqlDbType.Int Error Message: "The requested value" int "was not found."

Why does this work for β€œtext” but not β€œbit” or β€œint”?

Dim MyType as String = "bit" Dim sdtype As SqlDbType sdtype = DirectCast([Enum].Parse(GetType(SqlDbType), MyType), SqlDbType) 
+4
source share
3 answers

Since the value of the enum is SqlDbType.Bit, not SqlDbType.bit. Enum.Parse () is case sensitive WRT for input.

You need to use this overload of Enum.Parse:

 SqlDbType type = (SqlDbType) Enum.Parse( typeof(SqlDbType) , "bit" , true ) ; 

The third parameter indicates whether to ignore the case (true) or not (false) when analyzing the value of an enumeration from a string.

+13
source

It seems to be case sensitive. Try the β€œbit” and see if it works.

0
source

What is the result of [Enum] .Parse (GetType (SqlDbType))? Is this the result you expect? Because I think the error may be there.

As a final alternative, could you try CTYpe instead of live streaming? (Http://msdn.microsoft.com/en-us/library/7k6y2h6x (VS.71) .aspx)

CType is a little more flexible, however I think this is a long shot, but maybe worth a try.

0
source

Source: https://habr.com/ru/post/1337380/


All Articles