Exforsys.com
 
Home Tutorials ODP.NET
 

ODP.NET - Populating a Dataset with a Single Data Table

 

ODP.NET - Populating a Dataset with a Single Data Table

A dataset is simply a group of data tables. These data tables can be identified with their own unique names within a dataset. You can also add relations between data tables available in a dataset.



The following code gives you the details of all employees available in the emp table by populating a dataset with only a single data table using OracleDataAdapter:


Sample Code
  1. Imports Oracle.DataAccess.Client
  2. Public Class Form6
  3.  
  4.   Private Sub btnGetEmployees_Click(ByVal sender As  
  5.   System.Object, ByVal e As System.EventArgs) Handles  
  6.   btnGetEmployees.Click
  7.     'create connection to db
  8.     Dim cn As New OracleConnection("Data Source=xe; _
  9.                           User Id=scott;Password=tiger")
  10.     Try
  11.       Dim SQL As String
  12.       'build the SELECT statement
  13.       SQL = String.Format("SELECT empno, ename, job,  
  14.              mgr, hiredate, sal, comm, deptno FROM emp")
  15.       'create the dataadapter object
  16.       Dim adp As New OracleDataAdapter(SQL, cn)
  17.       'create the offline datatable
  18.       Dim ds As New DataSet
  19.       'fill the data set with a data table named emp
  20.       adp.Fill(ds, "emp")
  21.       'clear up the resources and work offline
  22.       adp.Dispose()
  23.       'check if it has any rows
  24.       If ds.Tables("emp").Rows.Count > 0 Then
  25.         'simply bind datatable to grid
  26.         Me.DataGridView1.DataSource = ds.Tables("emp")
  27.       Else
  28.         'display message if no rows found
  29.         MessageBox.Show("Not found")
  30.         Me.DataGridView1.Rows.Clear()
  31.       End If
  32.     Catch ex As Exception
  33.       'display if any error occurs
  34.       MessageBox.Show("Error: " & ex.Message)
  35.       'close the connection if it is still open
  36.       If cn.State = ConnectionState.Open Then
  37.         cn.Close()
  38.       End If
  39.     End Try
  40.   End Sub
  41. End Class
Copyright exforsys.com



If you can observe the highlighted code in the above script, we are creating a new DataSet object, populating it with a DataTable named "emp" (which contains all the rows) and finally assigning the same DataTable to the grid. The output for the above code would look similar to the figure in the section Retrieving Multiple Rows into a Data Table Using OracleDataAdapter.



Read Next: ODP.NET - Populating a Dataset with Multiple Data Tables



 

 

Comments



Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2010 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape