vb.net qr code generator free s EXECUTING COMMANDS in Visual Basic .NET

Maker Data Matrix ECC200 in Visual Basic .NET s EXECUTING COMMANDS

CHAPTER 11 s EXECUTING COMMANDS
Data Matrix ECC200 Printer In Visual Basic .NET
Using Barcode drawer for .NET Control to generate, create ECC200 image in .NET applications.
www.OnBarcode.com
DataMatrix Decoder In VB.NET
Using Barcode decoder for VS .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Executing Commands with Multiple Results
Code 128 Code Set C Generator In VB.NET
Using Barcode generation for VS .NET Control to generate, create Code 128B image in Visual Studio .NET applications.
www.OnBarcode.com
Barcode Generator In Visual Basic .NET
Using Barcode generation for .NET Control to generate, create 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.
Create USS Code 39 In Visual Basic .NET
Using Barcode creation for Visual Studio .NET Control to generate, create Code39 image in .NET applications.
www.OnBarcode.com
Make Data Matrix 2d Barcode In Visual Basic .NET
Using Barcode generator for .NET Control to generate, create DataMatrix image in .NET framework applications.
www.OnBarcode.com
Try It Out: Using the ExecuteReader Method
Matrix Drawer In VB.NET
Using Barcode generator for .NET Control to generate, create 2D image in .NET applications.
www.OnBarcode.com
Paint ISBN - 13 In Visual Basic .NET
Using Barcode drawer for VS .NET Control to generate, create Bookland EAN image in .NET applications.
www.OnBarcode.com
To use the ExecuteReader method, follow these steps: 1. Add a new Visual Basic Console Application project named CommandReader to your 11 solution. Rename Module1.vb to CommandReader.vb. 2. Replace the code in CommandReader.vb with the code in Listing 11-3. Listing 11-3. CommandReader.vb Imports System Imports System.Data Imports System.Data.SqlClient Module CommandReader Sub Main() 'create connection Dim conn As SqlConnection = New SqlConnection _ ("Data Source=.\sqlexpress;" & _ "Integrated Security=True;" & _ "database=northwind") 'create command (with both text and connection) Dim sql As String = "select firstname,lastname from employees"
Data Matrix Maker In Java
Using Barcode creator for Java Control to generate, create Data Matrix image in Java applications.
www.OnBarcode.com
Create Data Matrix ECC200 In Objective-C
Using Barcode maker for iPad Control to generate, create Data Matrix image in iPad applications.
www.OnBarcode.com
Dim cmd As SqlCommand = New SqlCommand(sql, conn) Console.WriteLine("Command created and connected.") Try 'Open connection conn.Open()
Encoding PDF 417 In VS .NET
Using Barcode generator for Reporting Service Control to generate, create PDF-417 2d barcode image in Reporting Service applications.
www.OnBarcode.com
Barcode Decoder In Java
Using Barcode scanner for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
CHAPTER 11 s EXECUTING COMMANDS
Generating Barcode In None
Using Barcode generator for Software Control to generate, create Barcode image in Software applications.
www.OnBarcode.com
Printing ECC200 In None
Using Barcode printer for Software Control to generate, create DataMatrix image in Software applications.
www.OnBarcode.com
'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 ex As SqlException Console.WriteLine(ex) Finally ' Close connection conn.Close() Console.WriteLine("Connection closed.") End Try End Sub End Module 3. Make CommandReader the startup project, and then run it by pressing Ctrl+F5. You should see the output in Figure 11-5, the first and last names of all nine employees.
Universal Product Code Version A Printer In Objective-C
Using Barcode creator for iPad Control to generate, create UPC-A Supplement 5 image in iPad applications.
www.OnBarcode.com
PDF-417 2d Barcode Decoder In .NET Framework
Using Barcode decoder for VS .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Figure 11-5. Using a data reader
Barcode Scanner In Visual Basic .NET
Using Barcode Control SDK for .NET Control to generate, create, read, scan barcode image in .NET applications.
www.OnBarcode.com
GS1 128 Creator In Java
Using Barcode encoder for Java Control to generate, create UCC.EAN - 128 image in Java applications.
www.OnBarcode.com
How It Works
Generate Barcode In Objective-C
Using Barcode encoder for iPad Control to generate, create Barcode image in iPad applications.
www.OnBarcode.com
Code 128 Code Set C Recognizer In Visual Basic .NET
Using Barcode decoder for .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
In this example, you use 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 use the ExecuteScalar method, you produce only a single scalar value. In contrast, using ExecuteReader() returns a SqlDataReader object.
CHAPTER 11 s EXECUTING COMMANDS
'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 0, the second column is column 1, 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.
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 new Visual Basic Console Application project named CommandNonQuery to your 11 solution. Rename Module1.vb to CommandNonQuery.vb. 2. Replace the code in CommandNonQuery.vb with the code in Listing 11-4. Listing 11-4. CommandNonQuery.vb Imports System Imports System.Data Imports System.Data.SqlClient Module CommandNonQuery Sub Main() 'create connection Dim conn As SqlConnection = New SqlConnection _ ("Data Source=.\sqlexpress;" & _ "Integrated Security=True;" & _ "database=northwind")
CHAPTER 11 s EXECUTING COMMANDS
'define scalar query Dim sqlqry As String = "select count(*)from employees" 'define insert statement Dim sqlins As String = "insert into employees " & _ "(firstname,lastname)values('Zachariah', 'Zinn')" 'define delete statement Dim sqldel As String = "delete from employees " & _ "where firstname = 'Zachariah'" & _ "and lastname = 'Zinn'" 'create commands Dim cmdqry As SqlCommand = New SqlCommand(sqlqry, conn) Dim cmdnon As SqlCommand = New SqlCommand(sqlins, conn) Console.WriteLine("Command created and connected.") Try 'Open connection conn.Open() 'execute query to get number of employees Console.WriteLine("Before INSERT: Number of employees {0}" _ , cmdqry.ExecuteScalar()) 'execute nonquery to insert an employee Console.WriteLine("Executing statement {0}" _ , cmdnon.CommandText) cmdnon.ExecuteNonQuery() Console.WriteLine("After INSERT: Number of employees {0}" _ , cmdqry.ExecuteScalar()) 'execute nonquery to delete an employee cmdnon.CommandText = sqldel Console.WriteLine("Executing statement {0}" _ , cmdnon.CommandText) cmdnon.ExecuteNonQuery() Console.WriteLine("After DELETE: Number of employees {0}" _ , cmdqry.ExecuteScalar()) Catch ex As SqlException Console.WriteLine(ex)
Copyright © OnBarcode.com . All rights reserved.