ExpertRating - Online Certification and Employment Testing ExpertRating - Online Certification and Employment Testing ExpertRating - Online Certification and Employment Testing

ExpertRating Home
ExpertRating Benefits
Recommend ExpertRating
Suggest More Tests
Privacy Policy
ASP Dot Net Tutorial - Index
ASP Dot Net Tutorial - Microsofts ASP.NET
ASP Dot Net Tutorial - Begining Web Forms
ASP Dot Net Tutorial - User Controls
ASP Dot Net Tutorial - ADO .NET
ASP Dot Net Tutorial - DataBinding to WebControls
ASP Dot Net Tutorial - DataList Controls
ASP Dot Net Tutorial - DataGrid Control
ASP Dot Net Tutorial - Advance Practical Examples on DataGrid
ASP Dot Net Tutorial - Working with DataSets
ASP Dot Net Tutorial - Working with XML
ASP Dot Net Tutorial - Forms Based Authentication
ASP Dot Net Tutorial - Windows Based Authentication

Online Personal Trainer Certification
Personal Trainer Certification (Spanish)
Six Sigma Certification
Find a Personal Trainer
Project Management Certification
Six Sigma Certification
Six Sigma Black Belt Certification
Master Black Belt Certification
Six Sigma Green Belt Certification
Life Coach Certification
Lean Management Certification
Lean Six Sigma Certification
First Aid Certification
CPR Certification
Yoga Certification
Aerobics Certification
Pilates Certification
Business Analyst Certification
SEO Certification
Online Business Writing Course & Certification
Makeup Artist Certification
Baby Sitting Course & Certification
Time Management Certification
Health Club Management Certification
Medical Coding Certification
Medical Billing Certification
Business Analysis Course
Master Trainer Certification
Total Quality Management Certification
Kaizen Certification
Creative Writing Certification
Event Management Certification
Test Search by Job Title
Administrative Assistant Certification
Virtual Assistant Certification
  All Online Courses


  Home > Courses, Tutorials & eBooks > ASP.NET Tutorial > ADO.NET

ASP.NET Tutorial - ADO.NET

                                                                                                               Page 2 of 14

In the first line (in the above example) we import the necessary namespace, System.Data.SqlClient, for working with SQL Server. The connection to SQL Server is created and opened in the Page_Load subroutine. First, a SqlConnection class named conPubs is created. Passing a connection string as a parameter to the constructor for the SqlConnection class initializes the conPubs class. Finally, the connection is actually opened by calling the Open () method of the SqlConnection class.

The connection string contains all the necessary location and authenticated information to connect to SQL Server. Also the connection string contains the name of the server, name of the database, SQL Server login, and password as shown below: -

conPubs=NewSqlConnection("Server=localhost;uid=sa;pwd=secret;database= Northwind" )

You will use similar code to create a connection in a Microsoft Access database by changing the namespaces and classes. In Example 2, a database connection is created and opened for a Microsoft Access databases named employees.

Connection has been opened!

The output is shown below.

As you are creating a connection for Microsoft Access, you must import the System.Data.OleDb namespace rather than the System.Data.SqlClient namespace.

Next, create an instance of the OleDbConnection class and initialize it with a connec­tion string appropriate for Microsoft Access. Finally, calling the open () method of the OleDbConnection class actually opens the database connection.

In Example18, the name of the OLEDB provider for Microsoft Access (Microsoft.Jet.OLEDB.4.0) and the path to the Access database on the server is seen. If you want to connect to another type of database, you have to specify a different provider.

By default when you call the Open () method with either the SqlConnection or OleDbConnection class, the connection takes 15 seconds to open before timing out. You can overwrite this default behavior by supplying a Connect Timeout attribute in the connection string. For example, to allow a connection to open in a SQL Server up to 90 seconds you have to initialize the connection like this:

MyConnection=NewSqlConnection("Server=localhost;UID=sa;PWD=secret; Connect _ -Timeout=90" )

Finally Database has limited number of connections. Thus, whenever the task is completed it is advisable to close the connection quickly so that it is free to be used by other pages. To close either a SqlConnection or OleDbConnection, use the following statement:

myConnection. Close ()

Retrieving Records from a Table

 The SQL statement that will be used most often in ASP.NET pages is Select. The Select statement enables you to retrieve records that match a certain condition from a database table. The syntax for a basic Select statement is as follows:

For example, If you want to retrieve the empno and ename columns, from the employee’s table where the empno column has the value 1, you would use the follow­ing Select statement:

If you simply want to retrieve all the columns and all the rows from the Authors table, you would use the following Select statement:

The asterisk (*) is a wildcard character that represents all the columns. If you use a WHERE clause, all the rows from the Authors table are automatically returned.

(For more information learn SqlServer and MS Access.)

To execute a Select statement in an ASP.NET page follow these four steps:

1. Create and open a database connection.

2. Create a database command that represents the SQL Select statement to execute.

3. Execute the command with the ExecuteReader () method returning a DataReader.

4. Loop through the DataReader displaying the results of the query.

When a query is executed using ADO.NET, the results are returned in a DataReader. Precisely the results of that query are represented by either a SqlDataReader or OleDbDataReader, depending on the database from which you are retrieving the records.

A DataReader represents a forward-only stream of database records; i.e. the DataReader represents only a single record at a time. To get the next record in the stream, and display them you must call the Read () method repeatedly until you the end of the stream is reached. Once the record is passed, there's no going back.

The following example, displays all the records from a SQL Server database named Northwind and table named employees as shown below in example.




Home  |  About Us  |  Privacy Policy  |  Site Map  |  FAQs  |  Contact Us
© ExpertRating 2015. All Rights Reserved.