FYI, I'm blogging most of my stuff over at More Wally now.
You might want to add my rss feed to your reader at:http://morewally.com/cs/blogs/wallym/rss.aspx
GetProviderSpecificFieldType() in .NET 2.0 Whidbey - Wallace B. McClure

Wallace B. McClure

All About Wally McClure - The musings of Wallym on .NET, Sql, ASP.NET, and other crazy shenanigans

News

Personal Blog

Work Blog

.NET

Book Authors

Business

Family

Friends

Georgia Tech Bloggers

Personal

GetProviderSpecificFieldType() in .NET 2.0 Whidbey

Back with Classic ADO, you could always get the datatype of the field in a recordset (adVarChar and such).  I wanted to do that again and get the data in the form of the System.Data.SqlTypes  I never did find a way to do that under .NET 1.x.  With .NET 2.0 Whidbey, I can use the GetProviderSpecificFieldType() method to get the type of the field as defined by the System.Data.SqlTypes.  Here is some quicky code that I wrote to do this:

        Dim i As Integer

        Dim strSql As String = "select * from tblTest where 1=2"

        Dim scsbObj As New SqlConnectionStringBuilder(Me.gstrSyncString)

        Dim sqlCn As New SqlConnection(scsbObj.ConnectionString)

        Dim sqlCm As New SqlCommand(strSql, sqlCn)

        Dim sqlDr As SqlDataReader

       

        Try

            sqlCn.Open()

            sqlDr = sqlCm.ExecuteReader()

            sqlDr.Read()

            For i = 0 To (sqlDr.FieldCount - 1)

                MsgBox(sqlDr.GetName(i) + vbCrLf + "SqlType: " + _

                    Convert.ToString(sqlDr.GetProviderSpecificFieldType(i)) + vbCrLf + _

                    "Database Type: " + Convert.ToString(sqlDr.GetDataTypeName(i)) + vbCrLf + _

                    ".NET Type: " + Convert.ToString(sqlDr.GetFieldType(i)))

            Next

            If Not (sqlDr.IsClosed = True) Then

                sqlDr.Close()

            End If

            sqlDr.Dispose()

            sqlDr = Nothing

        Catch sqlExc As SqlException

            MsgBox("Error: " & sqlExc.Message.ToString())

        Finally

            sqlCm.Dispose()

            sqlCm = Nothing

            If sqlCn.State <> ConnectionState.Closed Then

                sqlCn.Close()

            End If

            sqlCn.Dispose()

            sqlCn = Nothing

        End Try

Enjoy yourself.  I have no idea if the code works on Win64 or not.  I have only tested it on Win32 talking to a Sql Server 2005 database.

Wally

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required)