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
Test Search by Job Title
Find a Personal Trainer
Job Resources
Free Self Tests and Quizzes
Find a Lawyer
Project Management Certification
Six Sigma Certification
Six Sigma Black Belt Certification
Six Sigma Green Belt Certification
Lean Management Certification
First Aid Certification
CPR Certification
Yoga Certification
Aerobics Certification
Pilates Certification
SEO Certification
Online Photography Course & Certification
Online Business Writing Course & Certification
Baby Sitting Course & Certification
Time Manangement Certification
Health Club Management Certification
Selling Skills Certification
Business Analysis Course
Green Living Course  free!
Master Trainer Certification
Total Quality Management Certification
Kaizen Certification
Creative Writing Certification
  All Online Courses
   

   
   
   
   
   
   

   
  Home > Courses, Tutorials & eBooks > ASP.NET Tutorial > Working with DataSets
 

ASP.NET Tutorial - Working with DataSets

                                                                                                                 Page 2 of 5
 

Adding DataTable to a DataSet

The contents of existing database can be added to the tables of a DataSet by using the DataAdapter. To add a database table to a DataSet, use the following steps:

  • Create a DataSet.
  • Create a database connection.
  • Create a DataAdapter with a SQL Select statement and the database connection.
  • Call the Fill method of the Da±aAdapter, passing the name of the DataSet and the name of the new DataTable.

The following example, illustrates how the Employees can be added from the table from the Northwind database to a DataSet.

 

Example 51 ExpertSqlDataAdapter.aspx

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

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

<%

Dim dstEmployees As DataSet

Dim conNorthwind As SqlConnection

Dim dadEmployees As SqlDataAdapter

dstEmployees = New DataSet()

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

dadEmployees = New SqlDataAdapter( "Select * From Employees", conNorthwind )

dadEmployees.Fill( dstEmployees, "Employees" )

%>

<b>Employees table has been added to DataSet dstEmployees!</b>

 

 The output of above example is shown below:

In the above example when the Fill method is called , a new DataTable is added to the DataSet, named Employees. Therefore the connection to a database is never open. The Fill method by itself opens a database connection and then auto­matically closes the connection that it opens.

The page in above example works only with a Microsoft SQL Server (version 7.0 and higher) database. It uses the SqlConnection and SqlDataAdapter classes from the System. Data. SqlClient namespace. If another type of database, is to be used such as Microsoft Access or Oracle, the classes must be used from the System. Data. OleDbnamespace.

The following example illustrates how a DataTable can be added from a Microsoft Access database to a DataSet.

 

Example 51 ExpertOledbDataAdapter.aspx

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

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

 <%

Dim dstEmployees As DataSet

Dim conEmployees As OleDbConnection

Dim dadEmployees As OleDbDataAdapter

 dstEmployees = New DataSet()

conEmployees=New OleDbConnection( "PROVIDER= Microsoft. Jet.OLEDB.4.0;DATA Source=c:\Employees.mdb" )

dadEmployees = New OleDbDataAdapter( "Select * From Employees", conEmployees )

dadEmployees.Fill( dstEmployees, "Employees" )

%>

<b>Employees table has been added to DataSet dstEmployees! </b>

 

 The output of above example is shown below:

In this example the classes from the System.Data.OleDb namespace are used. Instances of the oleDbConnection and OleDbAdapter classes are created.

       

 

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