vb.net qr code dll s INTRODUCING COMMANDS in Visual Basic .NET

Paint Data Matrix ECC200 in Visual Basic .NET s INTRODUCING COMMANDS

CHAPTER 6 s INTRODUCING COMMANDS
Creating DataMatrix In Visual Basic .NET
Using Barcode creation for Visual Studio .NET Control to generate, create Data Matrix image in Visual Studio .NET applications.
www.OnBarcode.com
Read DataMatrix In Visual Basic .NET
Using Barcode reader for .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
then ExecuteScalar() would return the string Nancy instead of an integer, and you d get this exception:
Data Matrix 2d Barcode Maker In Visual Basic .NET
Using Barcode creation for .NET Control to generate, create Data Matrix image in Visual Studio .NET applications.
www.OnBarcode.com
Encode Barcode In Visual Basic .NET
Using Barcode generation for Visual Studio .NET Control to generate, create Barcode image in VS .NET applications.
www.OnBarcode.com
Unhandled Exception: System.InvalidCastException: Specified cast is not valid.
GTIN - 13 Creator In VB.NET
Using Barcode generator for .NET framework Control to generate, create GTIN - 13 image in VS .NET applications.
www.OnBarcode.com
Generating PDF 417 In Visual Basic .NET
Using Barcode generation for .NET framework Control to generate, create PDF-417 2d barcode image in Visual Studio .NET applications.
www.OnBarcode.com
because you can t cast a String to an Integer. Another problem may occur if a query actually returns multiple rows where you thought it would return only one; for example, what if there were multiple employees with the last name Davolio In this case, ExecuteScalar() just returns the first row of the result and ignores the rest. If you use ExecuteScalar(), make sure you not only expect but actually get a single value returned.
Making 2D In Visual Basic .NET
Using Barcode creation for .NET Control to generate, create 2D Barcode image in VS .NET applications.
www.OnBarcode.com
ISSN - 13 Encoder In Visual Basic .NET
Using Barcode creator for VS .NET Control to generate, create ISSN - 13 image in .NET applications.
www.OnBarcode.com
Executing Commands with Multiple Results
Data Matrix 2d Barcode Generation In None
Using Barcode creator for Microsoft Excel Control to generate, create Data Matrix 2d barcode image in Excel applications.
www.OnBarcode.com
Data Matrix ECC200 Generator In C#
Using Barcode drawer for VS .NET Control to generate, create Data Matrix 2d barcode image in .NET applications.
www.OnBarcode.com
For queries where you re expecting multiple rows and columns to be returned, use the command s ExecuteReader method. ExecuteReader() returns a data reader, an instance of the SqlDataReader class that you ll study in the next chapter. Data readers have methods that allow you to read successive rows in result sets and retrieve individual column values. We ll leave the details of data readers for the next chapter, but for comparison s sake, we ll give a brief example here of using the ExecuteReader method to create a SqlDataReader from a command to display query results.
QR Code Maker In Objective-C
Using Barcode creator for iPhone Control to generate, create Denso QR Bar Code image in iPhone applications.
www.OnBarcode.com
Recognize QR-Code In C#
Using Barcode recognizer for Visual Studio .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
Try It Out: Using the ExecuteReader Method
Print GS1 - 12 In None
Using Barcode encoder for Software Control to generate, create GS1 - 12 image in Software applications.
www.OnBarcode.com
QR-Code Creation In None
Using Barcode creation for Excel Control to generate, create Quick Response Code image in Microsoft Excel applications.
www.OnBarcode.com
To use the ExecuteReader method, follow these steps: 1. Add a Visual Basic Console Application project named CommandReader to the 06 solution. 2. Replace the code in Module1.vb with the code in Listing 6-3.
Barcode Maker In Java
Using Barcode creation for Java Control to generate, create Barcode image in Java applications.
www.OnBarcode.com
Scan UPCA In C#
Using Barcode scanner for VS .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Listing 6-3. CommandReader: Module1.vb
ANSI/AIM Code 39 Creator In None
Using Barcode maker for Excel Control to generate, create Code 39 Full ASCII image in Microsoft Excel applications.
www.OnBarcode.com
Creating Code 128C In Java
Using Barcode creation for Java Control to generate, create Code 128C image in Java applications.
www.OnBarcode.com
Imports System Imports System.Data Imports System.Data.SqlClient Module Module1 Sub Main() ' Set up connection string
PDF417 Reader In Visual C#.NET
Using Barcode recognizer for .NET framework Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Reading Barcode In Java
Using Barcode Control SDK for BIRT reports Control to generate, create, read, scan barcode image in Eclipse BIRT applications.
www.OnBarcode.com
CHAPTER 6 s INTRODUCING COMMANDS
Dim connString As String = _ "server = .\sqlexpress;" _ & "integrated security = true;" _ & "database = northwind" ' Set up query string Dim sql As String = _ "select " _ & " firstname, " _ & " & " lastname " _ " _ employees" & "from
' Create connection Dim conn As SqlConnection = New SqlConnection(connString) ' Create command Dim cmd as SqlCommand = new SqlCommand(sql, conn) Console.WriteLine("Command created and connected.") Try ' Open connection conn.Open() ' Execute query Dim rdr as SqlDataReader = cmd.ExecuteReader() While rdr.Read Console.WriteLine( _ "Employee name: {0} {1}", _ rdr.GetValue(0), _ rdr.GetValue(1)) End While Catch e As Exception ' Display error Console.WriteLine("Error: " & e.ToString) Finally ' Close connection conn.Close() End Try End Sub End Module
CHAPTER 6 s INTRODUCING COMMANDS
s Note We typically use the C# syntax for calling WriteLine(), since it s more succinct for formatting
multiple output values.
3. Make it the startup project, and then run it with Ctrl+F5. You should see the result in Figure 6-5, displaying the first and last names of all nine employees.
Figure 6-5. Using a data reader
How It Works
In this example you used the ExecuteReader method to retrieve and then output the first and last names of all the employees in the Employees table. As with ExecuteScalar(), ExecuteReader() takes the CommandText property and sends it to the database using the connection from the Connection property. When you used the ExecuteScalar method, you produced only a single scalar value. In contrast, using ExecuteReader() returned a SqlDataReader object.
' Execute query Dim rdr as SqlDataReader = cmd.ExecuteReader() While rdr.Read Console.WriteLine( _ "Employee name: {0} {1}", _ rdr.GetValue(0), _ rdr.GetValue(1)) End While
The SqlDataReader object has a Read method that gets each row in turn and a GetValue method that gets the value of a column in the row. The particular column whose value it retrieves is given by the integer parameter indicating the index of the column. Note that GetValue() uses a zero-based index, so the first column is column zero, the second column is column one, and so on. Since the query asked for two columns, FirstName and LastName, these are the columns numbered 0 and 1 in this query result.
CHAPTER 6 s INTRODUCING COMMANDS
Executing Statements
The ExecuteNonQuery method of the command executes SQL statements instead of queries. Let s try it.
Try It Out: Using the ExecuteNonQuery Method
To use the ExecuteNonQuery method, follow these steps: 1. Add a Visual Basic Console Application project named CommandNonQuery to the 06 solution. 2. Replace the code in Module1.vb with the code in Listing 6-4.
Copyright © OnBarcode.com . All rights reserved.