barcode generator in vb net 2008 Lesson 1: Creating and Executing Command Objects in Visual Studio .NET

Encoder Denso QR Bar Code in Visual Studio .NET Lesson 1: Creating and Executing Command Objects

Lesson 1: Creating and Executing Command Objects
Create Denso QR Bar Code In .NET
Using Barcode maker for ASP.NET Control to generate, create QR Code image in ASP.NET applications.
www.OnBarcode.com
Print Bar Code In .NET Framework
Using Barcode generator for ASP.NET Control to generate, create barcode image in ASP.NET applications.
www.OnBarcode.com
10. Double-click the Execute Sproc button to create the button-click event handler and switch to code view. For this handler, you will copy and reuse much of the code from the foregoing example. Typically, in an efficient production application, you would refactor similar code out into its own method but, for the sake of clarity in this demon stration, just copy the code you want to reuse into the additional button-click event handlers. The main difference between calling a standard SQL command and calling a stored procedure is that when calling a stored procedure you set the CommandType property to StoredProcedure and set the CommandText property to the name of the stored procedure. Then you call the ExecuteReader method just like execut ing a SQL statement and iterate through the reader exactly as before. 11. Add the following code to the ExecuteSprocButton_Click event handler:
Paint QR Code 2d Barcode In Visual C#.NET
Using Barcode generation for VS .NET Control to generate, create QR-Code image in .NET applications.
www.OnBarcode.com
Quick Response Code Drawer In .NET Framework
Using Barcode generation for .NET Control to generate, create QR-Code image in .NET framework applications.
www.OnBarcode.com
' VB Dim results As New System.Text.StringBuilder
Print QR Code In VB.NET
Using Barcode drawer for Visual Studio .NET Control to generate, create QR image in .NET applications.
www.OnBarcode.com
Drawing EAN-13 Supplement 5 In Visual Studio .NET
Using Barcode creation for ASP.NET Control to generate, create EAN-13 image in ASP.NET applications.
www.OnBarcode.com
Dim ExecuteSprocCommand As New SqlCommand
UCC.EAN - 128 Creator In Visual Studio .NET
Using Barcode printer for ASP.NET Control to generate, create GS1-128 image in ASP.NET applications.
www.OnBarcode.com
ECC200 Encoder In .NET Framework
Using Barcode creation for ASP.NET Control to generate, create DataMatrix image in ASP.NET applications.
www.OnBarcode.com
ExecuteSprocCommand.Connection = NorthwindConnection
USS Code 39 Generator In .NET Framework
Using Barcode maker for ASP.NET Control to generate, create Code 3/9 image in ASP.NET applications.
www.OnBarcode.com
Bar Code Maker In VS .NET
Using Barcode creation for ASP.NET Control to generate, create barcode image in ASP.NET applications.
www.OnBarcode.com
ExecuteSprocCommand.CommandType = CommandType.StoredProcedure
1D Creation In Visual Studio .NET
Using Barcode generation for ASP.NET Control to generate, create Linear Barcode image in ASP.NET applications.
www.OnBarcode.com
Encoding Identcode In VS .NET
Using Barcode creator for ASP.NET Control to generate, create Identcode image in ASP.NET applications.
www.OnBarcode.com
ExecuteSprocCommand.CommandText = "Ten Most Expensive Products"
Bar Code Creator In Java
Using Barcode creation for Android Control to generate, create bar code image in Android applications.
www.OnBarcode.com
Recognizing Data Matrix ECC200 In Visual C#.NET
Using Barcode scanner for .NET framework Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
ExecuteSprocCommand.Connection.Open()
Make Bar Code In Java
Using Barcode creator for Android Control to generate, create bar code image in Android applications.
www.OnBarcode.com
Barcode Creation In Objective-C
Using Barcode printer for iPhone Control to generate, create bar code image in iPhone applications.
www.OnBarcode.com
Dim reader As SqlDataReader = ExecuteSprocCommand.ExecuteReader
Code 128 Code Set C Scanner In Visual Studio .NET
Using Barcode reader for .NET framework Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
PDF417 Encoder In None
Using Barcode printer for Font Control to generate, create PDF-417 2d barcode image in Font applications.
www.OnBarcode.com
While reader.Read
Encode Barcode In .NET Framework
Using Barcode creator for .NET Control to generate, create barcode image in .NET applications.
www.OnBarcode.com
European Article Number 13 Recognizer In VB.NET
Using Barcode scanner for .NET framework Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
For i As Integer = 0 To reader.FieldCount - 1
results.Append(reader(i).ToString & vbTab)
Next
results.Append(Environment.NewLine)
End While reader.Close()
ExecuteSprocCommand.Connection.Close()
ResultsTextBox.Text = results.ToString // C# System.Text.StringBuilder results = new System.Text.StringBuilder(); SqlCommand ExecuteSprocCommand = new SqlCommand(); ExecuteSprocCommand.Connection = NorthwindConnection;
ExecuteSprocCommand.CommandType = CommandType.StoredProcedure;
ExecuteSprocCommand.CommandText = "Ten Most Expensive Products";
ExecuteSprocCommand.Connection.Open();
6
Working with Data in a Connected Environment
SqlDataReader reader = ExecuteSprocCommand.ExecuteReader(); while (reader.Read()) {
for (int i = 0; i<reader.FieldCount;i++)
results.Append(reader[i].ToString() + "\t"); } results.Append(Environment.NewLine); }
reader.Close();
ExecuteSprocCommand.Connection.Close();
ResultsTextBox.Text = results.ToString();
Let s add some more functionality by writing some code to perform a catalog operation and execute a command that creates a new table in the database. The execution methods in the preceding table show that performing catalog opera tions requires the use of the ExecuteNonQuery method of the Command object. For this method, you do not use a data reader because no data will be returned by the command. After creating the table, simply inspect the database in Server Explorer and verify that the command executed successfully. 12. Double-click the Create Table button to create the button-click event handler and switch to code view. 13. Add the following code to the CreateTableButton_Click event handler:
' VB Dim CreateTableCommand As New SqlCommand CreateTableCommand.Connection = NorthwindConnection CreateTableCommand.CommandType = CommandType.Text CreateTableCommand.CommandText = "CREATE TABLE SalesPersons (" & _ "[SalesPersonID] [int] IDENTITY(1,1) NOT NULL, " & _ "[FirstName] [nvarchar](50) NULL, " & _ "[LastName] [nvarchar](50) NULL)" CreateTableCommand.Connection.Open()
CreateTableCommand.ExecuteNonQuery()
CreateTableCommand.Connection.Close()
// C# SqlCommand CreateTableCommand = new SqlCommand();
CreateTableCommand.Connection = NorthwindConnection;
CreateTableCommand.CommandType = CommandType.Text;
CreateTableCommand.CommandText = "CREATE TABLE SalesPersons (" +
"[SalesPersonID] [int] IDENTITY(1,1) NOT NULL, " +
"[FirstName] [nvarchar](50) NULL, " +
"[LastName] [nvarchar](50) NULL)";
Lesson 1: Creating and Executing Command Objects
CreateTableCommand.Connection.Open();
CreateTableCommand.ExecuteNonQuery();
CreateTableCommand.Connection.Close();
14. Run the application and click the Create Table button. Navigate to Server Explorer and refresh the Tables node of the Northwind Traders database. Verify the existence of the new SalesPersons table.
Lesson Summary
Command objects are used to execute SQL statements and stored procedures against a database. There are specific Command objects for each of the .NET Framework Data Pro viders. The CommandType property determines whether a command executes a SQL statement or stored procedure. Commands can also be used to perform catalog operations on a database. Commands can be executed asynchronously. DataReader objects are created when command execution returns tabular data.
Lesson Review
The following questions are intended to reinforce key information presented in this lesson. The questions are also available on the companion CD if you prefer to review them in electronic form.
NOTE
Answers
Answers to these questions and explanations of why each choice is right or wrong are located in the Answers section at the end of the book.
1. What are the Command object property settings to execute a stored procedure (Choose all that apply.) A. CommandType = Text, CommandText = stored procedure name B. CommandType = Text, CommandText = SQL syntax to execute the stored procedure C. CommandType = StoredProcedure, CommandText = SQL syntax to execute the stored procedure D. CommandType = StoredProcedure, CommandText = stored procedure name
Copyright © OnBarcode.com . All rights reserved.