Exforsys

VB.NET 2005

  1. VB.NET 2005 Free Training
  2. The .NET Framework Architecture Part 1
  3. The .NET Framework Architecture Part 2
  4. Application Class and Message Class
  5. Implementing Class Library Object
  6. Visual Studio.NET Namespaces
  7. .NET Assemblies
  8. Differences between VB.NET 1.0 and VB.NET 2.0
  9. Introducing VB.NET Windows Forms
  10. Visual Studio Windows Forms Designer
  11. Exploring the Forms Designer generated code
  12. Setting and Adding Properties to Windows Form
  13. Implementing Inheritance
  14. Event Handling In Visual Basic .NET
  15. Building Graphical Interface elements
  16. .NET Common Windows Forms Controls Part 1
  17. .NET Common Windows Forms Controls Part 2
  18. Common Controls and Handling Control Events
  19. DomainUpDown and NumericUpDown Controls
  20. Dialog Boxes in Visual Basic .NET
  21. Visual Studio Adding Controls to Windows Form
  22. VB.NET Validation Controls
  23. Working with Menu Controls
  24. VB.NET MDI Applications
  25. .NET Exceptions
  26. VB.NET Creating and Managing Components Part 1
  27. VB.NET Creating and Managing Components Part 2
  28. Simple Data Binding
  29. .NET Complex Data Binding
  30. .NET Data Form Wizard
  31. Data Manipulation with ADO.NET
  32. SQL Server Stored Procedures
  33. SQL Server Ad Hoc Queries
  34. Finding and Sorting Data in DataSets
  35. ADO.NET Object Model
  36. Working with DataSets
  37. Using XML Data
  38. Working with File System in .NET
  39. Creating Web Service
  40. Instantiating - Invoking Web Services, Creating Proxy Classes with WSDL
  41. Web Reference and Web Services
  42. Web Services - SOAP, WSDL, Disco and UDDI
  43. Web Application Testing in VB.NET 2005
  44. Web Application Tracing and Debugging
  45. Working with Legacy Code and COM Components
  46. ActiveX Controls and Legacy Code
  47. Windows Application Testing
  48. VB.NET Windows Application Testing
  49. Tracing VB.NET Windows Application
  50. Debugging Windows Applications In Visual Studio.NET 2005
  51. Deploying Windows Applications In Visual Studio.NET 2005
  52. Customizing Setup Project in Visual Studio.NET 2005
  53. Shared Assembly
  54. Microsoft .NET Creating Installation Components
  55. The Registry Editor in Visual Studio.NET 2005
  56. The File Types Editor

Ads


Home arrow Technical Training arrow VB.NET 2005

SQL Server Ad Hoc Queries

Page 1 of 2
Author : Exforsys Inc.     Published on: 9th Jul 2005    |   Last Updated on: 24th Dec 2007

In this tutorial you will learn about Consuming and Manipulating Data Viz. Access and Manipulate SQL Server data - Using Ad Hoc Queries; Running Queries, The SELECT Statement, The INSERT Statement, The UPDATE Statement and The DELETE Statement.

Access and Manipulate SQL Server data - Using Ad Hoc Queries

Ads

Consuming and Manipulating Data

Modern day enterprises deal with online transaction processing databases that need to store huge volumes of data as well as carryout database operations such as UPDATE, ADD, and DELETE or retrieve data for viewing and decision making. The emergent need is to device a software that connects to the database and allows the user perform all these operations and more. Visual Basic.Net coupled with SQL server provides for a system that is dynamic and efficient. In this lesson we shall be studying how we can connect to SQL server and manipulate data.

Access and Manipulate SQL Server Data

The data stored in an SQL server becomes accessible if the data source is defined in the system or the user connects to the database using an application with a connection and object and a connect string. VB.NET, as we saw in the earlier lessons allows the user create statements dynamically and execute queries implicitly. In this section we shall use queries explicitly to manipulate data in the SQL Server database using the DataCommand object.

Before actually launching onto manipulation of data, the user must instantiate the SqlCommand.

Dim sqlCommand1 As New SqlCommand()

This object exposes the following method that can be used to query the database.

Methods

Description

BeginExecuteNonQuery

Initates the asynchronous execution of the T-SQL statement or stored procedure. Usually used for execution commands such as SELECT, DELETE, UPDATE, and SETstatements.

BeginExecuteReader

Initiates the asynchronous execution of the Transact-SQL statement or stored procedure. This should receive one or more result sets from the Server.

BeginExecuteXmlReader

Initiates the asynchronous execution of the Transact-SQL statement or stored procedure

ExecuteReader

executes commands that return rows.

ExecuteNonQuery

Executes commands such as Transact-SQL INSERT, DELETE, UPDATE, and SET statements.

ExecuteScalar

Retrieves a single value (for example, an aggregate value) from a database.

ExecuteXmlReader

Builds an Xml Object.

The DataReader object can also be used to access data. The dataReader is usually declared in Visual Basic using the following command:

Dim MyReader As New SqlDataReader.

 

 

 

 

 

 

 

The select statement is the simplest method of accessing data from the database. The user can direct the system to select the columns and display the data in the console.

VB.NET uses the classes contained in System.Data.SqlClient. These classes have to be referenced before they can be used. Let us understand how this is done by working out an example.

(a) Create a new windows application in Visual Basic 2005 Express.

(b) Double click on the Form1(design) to go to the codes page.

(c) Import the Namespace System.Data.SqlClient, using the following statement.

Imports System.Data.SqlClient

(d) Define a ConnectString that the Connection Object will use to establish the connection with the database. The value for the ConnectString will specify the server, initial database and the type of database connection and the UserName and the password.(see code below).

(e) Define a query which is a string variable.

(f) Add some query that will be returning rows from the database.

(g) Define a datacommand, SqlCommand object whose constructor will take two parameters viz., the query and the connection object.

(h) At this stage the connection is established and available.

(i) Use the SqlDataReader to retrieve the data and write to console.

The following code shows the implementation of the data access using the SELECT statement to retrieve and display data on the console

Click here to view sample code

The data output extracted is shown below:

Ads

The INSERT Statement

The insert statement is used to add new data rows to the table. Create the SqlAdapter object as above. Now call the ExecuteNonQuery method of the SqlAdapter. This method executes queries in instances where the query statement is either an insert, update or a Delete method.

Let us understand how to insert data by working out an example.

1. Start a new windows application and to the form Form1

2. Add one label and one TextBox and two buttons.

3. Add the codes as in the case of previous demo.

4. The method InsertData will have two arguments, one for the connect string and the other for the ProductCategory name.

5. Add an insert data command.

The final code will look like the screenshot below:

Click here to veiw sample code



 
This tutorial is part of a VB.NET 2005 tutorial series. Read it from the beginning and learn yourself.

VB.NET 2005

  1. VB.NET 2005 Free Training
  2. The .NET Framework Architecture Part 1
  3. The .NET Framework Architecture Part 2
  4. Application Class and Message Class
  5. Implementing Class Library Object
  6. Visual Studio.NET Namespaces
  7. .NET Assemblies
  8. Differences between VB.NET 1.0 and VB.NET 2.0
  9. Introducing VB.NET Windows Forms
  10. Visual Studio Windows Forms Designer
  11. Exploring the Forms Designer generated code
  12. Setting and Adding Properties to Windows Form
  13. Implementing Inheritance
  14. Event Handling In Visual Basic .NET
  15. Building Graphical Interface elements
  16. .NET Common Windows Forms Controls Part 1
  17. .NET Common Windows Forms Controls Part 2
  18. Common Controls and Handling Control Events
  19. DomainUpDown and NumericUpDown Controls
  20. Dialog Boxes in Visual Basic .NET
  21. Visual Studio Adding Controls to Windows Form
  22. VB.NET Validation Controls
  23. Working with Menu Controls
  24. VB.NET MDI Applications
  25. .NET Exceptions
  26. VB.NET Creating and Managing Components Part 1
  27. VB.NET Creating and Managing Components Part 2
  28. Simple Data Binding
  29. .NET Complex Data Binding
  30. .NET Data Form Wizard
  31. Data Manipulation with ADO.NET
  32. SQL Server Stored Procedures
  33. SQL Server Ad Hoc Queries
  34. Finding and Sorting Data in DataSets
  35. ADO.NET Object Model
  36. Working with DataSets
  37. Using XML Data
  38. Working with File System in .NET
  39. Creating Web Service
  40. Instantiating - Invoking Web Services, Creating Proxy Classes with WSDL
  41. Web Reference and Web Services
  42. Web Services - SOAP, WSDL, Disco and UDDI
  43. Web Application Testing in VB.NET 2005
  44. Web Application Tracing and Debugging
  45. Working with Legacy Code and COM Components
  46. ActiveX Controls and Legacy Code
  47. Windows Application Testing
  48. VB.NET Windows Application Testing
  49. Tracing VB.NET Windows Application
  50. Debugging Windows Applications In Visual Studio.NET 2005
  51. Deploying Windows Applications In Visual Studio.NET 2005
  52. Customizing Setup Project in Visual Studio.NET 2005
  53. Shared Assembly
  54. Microsoft .NET Creating Installation Components
  55. The Registry Editor in Visual Studio.NET 2005
  56. The File Types Editor
 

Comments