2012-10-03 169 views
0
Dim MyType as String = "numeric" 
Dim sdtype As SqlDbType sdtype = DirectCast([Enum].Parse(GetType(SqlDbType), MyType), SqlDbType) 

不适用于数字。我想知道如何处理这些数据类型。提前致谢。将数字字符串转换为sqldbtype

回答

1

SqlDbType枚举根本不包含值“数字”,因此不可能将这样的字符串转换为该枚举。

为了避免错误,可使用这样的代码:(C#,不熟悉VB.NET对不起)

SqlDbType dbType; 
if (Enum.TryParse<SqlDbType>(myType, true, out dbType)) 
{ 
    MessageBox.Show("type: " + dbType); 
} 
else 
{ 
    MessageBox.Show("invalid type: " + myType); 
} 

有各种可用的数字类型虽然,例如Float因此改变字符串其中的一个应该只是罚款:对SQL Server数据库提供商

Dim MyType as String = "float" 

所有可用的数据类型包括:

 
BigInt 
Binary 
Bit 
Char 
DateTime 
Decimal 
Float 
Image 
Int 
Money 
NChar 
NText 
NVarChar 
Real 
UniqueIdentifier 
SmallDateTime 
SmallInt 
SmallMoney 
Text 
Timestamp 
TinyInt 
VarBinary 
VarChar 
Variant 
Xml 
Udt 
Structured 
Date 
Time 
DateTime2 
DateTimeOffset 

任何试图在此列表中投字符串不是会导致错误。

编辑:第二个想法看起来像你正在使用Access数据库?在这种情况下,使用OleDbTypeSqlDbType,只是代码更改为:

Dim sdtype As OleDbType sdtype = DirectCast([Enum].Parse(GetType(OleDbType), MyType), OleDbType) 

,它应该工作,因为OleDbType确实包含了“数字”。

+0

你能告诉我什么是SqlDbType枚举中没有定义的其他数据类型。 – user1685652

+0

不是,我只能说出**是什么**。无论如何,看到我的编辑我想我找到了你的问题的根源。 –

+0

不,我正在使用SQL数据库..反正谢谢你的帮助 – user1685652