.net barcode reader Using Multiple Result Sets with a Data Reader in VB.NET

Creating Data Matrix 2d barcode in VB.NET Using Multiple Result Sets with a Data Reader

Using Multiple Result Sets with a Data Reader
Encoding DataMatrix In Visual Basic .NET
Using Barcode generation for .NET Control to generate, create Data Matrix ECC200 image in Visual Studio .NET applications.
www.OnBarcode.com
Scan ECC200 In VB.NET
Using Barcode reader for VS .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Sometimes you might want to get a job done quickly and also want to query the database with two or more queries at the same time. And, you wouldn t want the overall application performance to suffer in any way either by instantiating more than one command or data reader, or by exhaustively using the same objects over and over again, adding to the code as you go. So, is there a way you can get a single data reader to loop through multiple result sets Yes, data readers have a method, NextResult(), that advances the reader to the next result set.
PDF 417 Generation In VB.NET
Using Barcode drawer for Visual Studio .NET Control to generate, create PDF-417 2d barcode image in VS .NET applications.
www.OnBarcode.com
EAN-13 Supplement 5 Generator In VB.NET
Using Barcode generation for .NET Control to generate, create EAN 13 image in Visual Studio .NET applications.
www.OnBarcode.com
CHAPTER 7 s INTRODUCING DATA READERS
Print Matrix 2D Barcode In VB.NET
Using Barcode printer for VS .NET Control to generate, create Matrix 2D Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Barcode Generator In Visual Basic .NET
Using Barcode printer for VS .NET Control to generate, create Barcode image in VS .NET applications.
www.OnBarcode.com
Try It Out: Handling Multiple Result Sets
GS1 - 12 Printer In VB.NET
Using Barcode generator for .NET Control to generate, create UPC-A Supplement 2 image in .NET framework applications.
www.OnBarcode.com
Draw USD - 8 In VB.NET
Using Barcode generator for VS .NET Control to generate, create Code 11 image in Visual Studio .NET applications.
www.OnBarcode.com
Let s use NextResult() to process multiple result sets. 1. Add a new Console Application project named MultipleResults to your 07 solution. 2. Replace the code in Module1.vb with the code in Listing 7-6.
Recognizing DataMatrix In Visual C#.NET
Using Barcode decoder for VS .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
Painting Data Matrix In .NET
Using Barcode creation for ASP.NET Control to generate, create Data Matrix ECC200 image in ASP.NET applications.
www.OnBarcode.com
Listing 7-6. MultipleResults: Module1.vb
Generating Universal Product Code Version A In Java
Using Barcode generator for Java Control to generate, create UPC-A image in Java applications.
www.OnBarcode.com
Make PDF 417 In Java
Using Barcode generator for Java Control to generate, create PDF-417 2d barcode image in Java applications.
www.OnBarcode.com
Imports System Imports System.Data Imports System.Data.SqlClient Module Module1 Sub Main() ' Set up connection string Dim connString As String = _ "server = .\sqlexpress;" _ & "integrated security = true;" _ & "database = northwind" ' Set up first query Dim sql1 As String = _ "select " _ & " companyname, " _ & " contactname " _ & "from " _ & " customers " _ & "where " _ & "companyname like 'A%' " ' Set up second query Dim sql2 As String = _ "select " _ & " firstname, " _ & " lastname " _ & "from " _ & " employees "
EAN13 Creation In Objective-C
Using Barcode generator for iPhone Control to generate, create EAN-13 image in iPhone applications.
www.OnBarcode.com
Generating ECC200 In Objective-C
Using Barcode generator for iPhone Control to generate, create DataMatrix image in iPhone applications.
www.OnBarcode.com
CHAPTER 7 s INTRODUCING DATA READERS
PDF-417 2d Barcode Maker In Java
Using Barcode drawer for Eclipse BIRT Control to generate, create PDF417 image in BIRT reports applications.
www.OnBarcode.com
Data Matrix ECC200 Maker In None
Using Barcode printer for Online Control to generate, create Data Matrix image in Online applications.
www.OnBarcode.com
' Combine queries Dim sql As String = sql1 & sql2 ' Create connection Dim conn As SqlConnection = New SqlConnection(connString) Try ' Open connection conn.Open() ' Create command Dim cmd As SqlCommand = New SqlCommand(sql, conn) ' Create data reader Dim rdr As SqlDataReader = cmd.ExecuteReader() ' Loop through result sets Do While (rdr.Read()) Console.WriteLine("{0} : {1}", rdr(0), rdr(1)) End While Console.WriteLine("".PadLeft(60, "=")) Loop While (rdr.NextResult()) ' Close data reader rdr.Close() Catch e As Exception ' Display error Console.WriteLine("Error: " & e.ToString) Finally ' Close connection conn.Close() End Try End Sub End Module
Printing Code 39 Full ASCII In None
Using Barcode generator for Software Control to generate, create Code-39 image in Software applications.
www.OnBarcode.com
Recognizing QR-Code In C#.NET
Using Barcode decoder for .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
3. Make this the startup project, and run it with Ctrl+F5. You should see the result in Figure 7-6.
Barcode Reader In .NET
Using Barcode reader for .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
Create QR Code In Java
Using Barcode creator for Java Control to generate, create QR Code image in Java applications.
www.OnBarcode.com
CHAPTER 7 s INTRODUCING DATA READERS
Figure 7-6. Displaying multiple result sets
How It Works
This program is essentially the same as the first, DataLooper.cs (Listing 7-1). Here, you defined two separate queries and then combined them:
' Set up first query Dim sql1 As String = _ "select " _ & " companyname, " _ & " contactname " _ & "from " _ & " customers " _ & "where " _ & "companyname like 'A%' " ' Set up second query Dim sql2 As String = _ "select " _ & " firstname, " _ & " lastname " _ & "from " _ & " employees" ' Combine queries Dim sql As String = sql1 & sql2
CHAPTER 7 s INTRODUCING DATA READERS
s Caution Some DBMSs require an explicit character as a separator between multiple queries, but SQL Server requires only whitespace before subsequent SELECT keywords, which you carefully placed after the LIKE predicate in sql1.
The only other change is that you looped through result sets. You nest the loop that retrieves rows inside one that loops through result sets:
' Loop through result sets Do While (rdr.Read()) Console.WriteLine("{0} : {1}", rdr(0), rdr(1)) End While Console.WriteLine("".PadLeft(60, "=")) Loop While (rdr.NextResult())
You chose only two character-string columns per query to simplify things. Extending this to handle result tables with different numbers of columns and column data types isn t hard.
Summary
In this chapter you used data readers to perform a variety of common tasks, from simply looping through single result sets to handling multiple result sets. You learned how to retrieve values for columns by column name and index, and learned about methods available for handling values of different data types. You also learned how to get information about result sets and get schema information. In the next chapter, we ll cover the really interesting aspect of ADO.NET: handling database data while disconnected from a database.
Copyright © OnBarcode.com . All rights reserved.