Sponsored Links
VB.NET 2005 Tutorials
- VB.NET 2005 Free Training
- Shared Assembly
- The .NET Framework Architecture Part 1
- Tracing VB.NET Windows Application
- The .NET Framework Architecture Part 2
- VB.NET Windows Application Testing
- Implementing Inheritance
- The File Types Editor
- Visual Studio.NET Namespaces
- Differences between VB.NET 1.0 and VB.NET 2.0
- Visual Studio Windows Forms Designer
- Introducing VB.NET Windows Forms
- Event Handling In Visual Basic .NET
- Exploring the Forms Designer generated code
- Building Graphical Interface elements
- Microsoft .NET Creating Installation Components
- Application Class and Message Class
- Visual Studio Adding Controls to Windows Form
- Common Controls and Handling Control Events
- Implementing Class Library Object
Tutorials
VB.NET 2005SQL Server Ad Hoc Queries
SQL Server Ad Hoc Queries
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
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:

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
Next Page: SQL Server Ad Hoc Queries - Page 2
Comments
pankaj jha said:
| it was great help in understanding the connectivity with sql |
Andrew Fugier said:
| Great examples of dynamic SQL in VB.NET |
nikhil said:
| how to vb.net2005 connectivity with sql ,ms-access? |
Sponsored Links
