简体   繁体   中英

Determining data types using ADO.NET in VB.NET

In the past I have used the code below to find out what datatypes the columns of a table. Now that I am using ADO.NET, I cannot a find a way of doing that with VB.NET and ADO.NET. How would I do it using ADO.NET? Do I have to enumerate all the data types or is there a "isnumeric" or a "isstring" type function?

Select Case rs(sColumnName).Type
    Case ADODB.DataTypeEnum.adUnsignedSmallInt, ADODB.DataTypeEnum.adUnsignedTinyInt, ADODB.DataTypeEnum.adUnsignedBigInt, ADODB.DataTypeEnum.adUnsignedInt, ADODB.DataTypeEnum.adSingle, ADODB.DataTypeEnum.adTinyInt, ADODB.DataTypeEnum.adSmallInt, ADODB.DataTypeEnum.adInteger, ADODB.DataTypeEnum.adBigInt, ADODB.DataTypeEnum.adDecimal, ADODB.DataTypeEnum.adDouble, ADODB.DataTypeEnum.adNumeric, ADODB.DataTypeEnum.adCurrency '
        sType="NUMERIC"
    Case ADODB.DataTypeEnum.adVarChar, ADODB.DataTypeEnum.adLongVarChar, ADODB.DataTypeEnum.adLongVarWChar, ADODB.DataTypeEnum.adVarWChar, ADODB.DataTypeEnum.adWChar, ADODB.DataTypeEnum.adChar ' ADD ALL THE STRING TYPES
        sType="STRING"                                
End Select

You can use GetDataTypeName from SqlDataReader class.

Example:

Dim con As New SqlConnection("Data Source=(local);Database=mydb;Integrated Security=SSPI;")
Dim reader As SqlDataReader
Dim cmd As New SqlCommand
cmd.CommandText = "select * from table1"
cmd.Connection = con
reader = cmd.ExecuteReader

MessageBox.Show(reader.GetDataTypeName(1))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM