Home > Working with typed DataReader and SqlResultSet data
Book Excerpt:
EMAIL THIS

Working with typed DataReader and SqlResultSet data

15 May 2006 | Wiley

Expert advice on database development
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google

The following tip was excerpted from Chapter 1, 'Migrating from ADO to ADO.NET,' of the book Expert One-on-one Visual Basic 2005 Database Programming by Roger Jennings, courtesy of Wiley Publishers. Click here for the complete collection of book excerpts.

The preceding code examples use Reader.Item(ColumnIndex).ToString, Reader.GetString (ColumnIndex) and Reader.GetInt32(ColumnIndex) methods to extract column values to native .NET data types, which the System namespace defines. ADO.NET 2.0 provides the following data-specific enumerations:

  • System.Data.DbType is a generic enumeration for setting the data types of OleDb and Odbc parameters, fields, and properties.
  • System.Data.SqlDbType is an enumeration for use with SqlParameter objects only. VS 2005 automatically adds SqlParameters when you create typed DataSets from SQL Server tables in the following sections.
  • System.Data.SqlTypes is a namespace that contains structures for all SQL Server 2000 and 2005 data types, except timestamp, and related classes and enumerations. Using SqlTypes structures improves data-access performance by eliminating conversion to native .NET types, and assures that column values aren't truncated.

VS 2005's online help provides adequate documentation for DbType and SqlDbType enumerations, and SqlTypes structures, so this chapter doesn't provide a table to relate these enumerations and types.

The following OpenDataReaderSqlTypes listing shows examples of the use of typical GetSqlDataType(ColumnIndex) methods:

     Private Sub OpenDataReaderSqlTypes()
          'Define and open the SqlConnection object
          Dim strConn As String = "Server=localhost;Database=Northwind;" + _
            "Integrated Security=SSPI"
          Dim cnnNwind As SqlConnection = New SqlConnection(strConn)
          Dim sdrReader As SqlDataReader
          Try
              cnnNwind.Open()

              'Define the SqlCommand
             Dim strSQL As String = "SELECT Orders.*, " + _
               "ProductID, UnitPrice, Quantity, Discount " + _
               "FROM Orders INNER JOIN [Order Details] ON " + _
               "Orders.OrderID = [Order Details].OrderID WHERE CustomerID = 'ALFKI'"
            Dim cmdReader As SqlCommand = New SqlCommand(strSQL, cnnNwind)

            'Create, and traverse the SqlDataReader, assigning SqlTypes to variables
            sdrReader = cmdReader.ExecuteReader(CommandBehavior.CloseConnection)
           With sdrReader
                 If .HasRows Then
                   While .Read
                          'Get typical SqlTypes
                         Dim s_intOrderID As SqlInt32 = .GetSqlInt32(0)
                         Dim s_strCustomerID As SqlString = .GetSqlString(1)
                         Dim s_datOrderDate As SqlDateTime = .GetSqlDateTime(3)
                         Dim s_curUnitPrice As SqlMoney = .GetSqlMoney(15)
                         Dim s_sngDiscount As SqlSingle = .GetSqlSingle(17)
                   End While
               End If
         End With
      Catch exc As Exception
           MsgBox(exc.Message + exc.StackTrace)
      Finally
           'Close the SqlDataReader and the SqlConnection
           sdrReader.Close()
      End Try
End Sub

You can update SqlResultSet object column values with strongly typed variables by invoking the SqlResultSet.SetSqlDataType(ColumnIndex) method. You'll see more examples of strongly typed SQL Server data retrieval and update operations that use these methods in later chapters.

The above tip was excerpted from Chapter 1, 'Migrating from ADO to ADO.NET,' of the book Expert One-on-one Visual Basic 2005 Database Programming by Roger Jennings, courtesy of Wiley Publishers. Click here for the complete collection of book excerpts.

Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google



RELATED CONTENT
.NET Development for SQL Server
Creating Windows PowerShell scripts to manage SQL Server 2008 instances
Manipulate column names in a SQL Server table
Code to restore SQL Server databases in VB.NET
Custom VB.Net scripting in SQL Server Integration Services
Retrieve images from SQL Server and store in VB.Net
Connect to SQL Server database with Visual Basics
Top 10 SQL Server development questions
Developing CLR database objects: 10 tips, 10 minutes
CLR architecture
CLR stored procedures
.NET Development for SQL Server Research

SQL/Transact SQL (T-SQL)
Using DELETE and TRUNCATE TABLE statements to delete data in SQL Server
SQL language crash course (just enough to be dangerous)
Working with IntelliSense in SQL Server 2008 Management Studio
SQL Server Mailbag: Stored procedures, triggers and SSRS reports
Working with sparse columns in SQL Server 2008
Determining the source of full transaction logs in SQL Server
New GROUP BY option provides better data control in SQL Server 2008
Using the OPENROWSET function in SQL Server
Loading data files with SQL Server's BULK INSERT statement
Importing and exporting bulk data with SQL Server's bcp utility
SQL/Transact SQL (T-SQL) Research

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
ACID  (SearchSQLServer.com)
Collaboration Data Objects  (SearchSQLServer.com)
commit  (SearchSQLServer.com)
container  (SearchSQLServer.com)
DAO  (SearchSQLServer.com)
fetch  (SearchSQLServer.com)
OLE DB  (SearchSQLServer.com)
query  (SearchSQLServer.com)
SQL  (SearchSQLServer.com)
T-SQL  (SearchSQLServer.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary




Secure SQL - Data Security for Your Database
HomeNewsTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersIT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2005 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts