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
FAQ
Login
 
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 4 of 14
 

The output is shown below:

Example 20 is same as example 19 but with two differences.

Firstly the System.Data.OleDb namespace is imported at the top of the page rather in the System. Data.SqlClient namespace.

Secondly,Instead of SQL classes OleDb classes are used in all the statements.

After you finish displaying the records from a DataReader, be sure to explicitly close it (using the Close () method).

Retrieving a Single Database Record

The developers generally need a single record from a database. Therefore it is very important to know how to retrieve it. For example, given a username you might have to look up someone's password or retrieve a phone number for John from a database table.

Another common situation which requires retrieval of single database record is aggre­gate functions like MAX (), MIN (), COUNT () etc.

The previous section dealt with the use of a DataReader representing the results of a database query. But if there is a need, to retrieve a single result from a query, the ExecuteScalar () method is more efficient than the ExecuteReader () method, as it requires less coding.

The following example counts the no. of records from Employees table. We use the ExecuteScalar () method to retrieve and display the value of the SQL Count (*) aggregate function.

Example 21 SqlExecuteScalarCount.aspx

<%@ Import Namespace="System.Data.SqlClient" %>

<Script Runat="Server">

Sub Page_Load

Dim conNorthwind As SqlConnection

Dim cmdSelectCount As SqlCommand

conNorthwind = New SqlConnection( “Server=localhost;UID=sa;PWD=secret;database=Northwind" )

conNorthwind.Open()

cmdSelectCount = New SqlCommand( "Select Count(*) From Employees", conNorthwind )

lblEmp.Text = cmdSelectCount.ExecuteScalar()

conNorthwind.Close()

End Sub

</Script>

<html>

<body>

There are

<asp:Label

ID="lblEmp"

Runat="Server" />

Employees in this Table

</body>

</html>

 

The output is shown below:

To execute the page with an Access or Oracle database, classes have to be used from the System.Data.OleDb or System.Data.OracleClient namespace rather than the System.Data.SqlClient namespace as shown

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