|
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:
Imports Oracle.DataAccess.Client Public Class Form6 Private Sub btnGetEmployees_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetEmployees.Click 'create connection to db Dim cn As New OracleConnection("Data Source=xe; _ User Id=scott;Password=tiger") Try Dim SQL As String 'build the SELECT statement SQL = String.Format("SELECT empno, ename, job, mgr, hiredate, sal, comm, deptno FROM emp") 'create the dataadapter object Dim adp As New OracleDataAdapter(SQL, cn) 'create the offline datatable Dim ds As New DataSet 'fill the data set with a data table named emp adp.Fill(ds, "emp") 'clear up the resources and work offline adp.Dispose() 'check if it has any rows If ds.Tables("emp").Rows.Count > 0 Then 'simply bind datatable to grid Me.DataGridView1.DataSource = ds.Tables("emp") Else 'display message if no rows found MessageBox.Show("Not found") Me.DataGridView1.Rows.Clear() End If Catch ex As Exception 'display if any error occurs MessageBox.Show("Error: " & ex.Message) 'close the connection if it is still open If cn.State = ConnectionState.Open Then cn.Close() End If End Try End Sub End Class
Imports%20Oracle.DataAccess.Client%20%0APublic%20Class%20Form6%20%0A%0A%20%20%20Private%20Sub%20btnGetEmployees_Click%28ByVal%20sender%20As%20%0A%20%20%20System.Object%2C%20ByVal%20e%20As%20System.EventArgs%29%20Handles%20%0A%20%20%20btnGetEmployees.Click%20%0A%20%20%20%20%20%20%20%27create%20connection%20to%20db%20%0A%20%20%20%20%20%20%20Dim%20cn%20As%20New%20OracleConnection%28%22Data%20Source%3Dxe%3B%20_%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20User%20Id%3Dscott%3BPassword%3Dtiger%22%29%20%0A%20%20%20%20%20%20%20Try%20%0A%20%20%20%20%20%20%20%20%20%20%20%20Dim%20SQL%20As%20String%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%27build%20the%20SELECT%20statement%20%0A%20%20%20%20%20%20%20%20%20%20%20%20SQL%20%3D%20String.Format%28%22SELECT%20empno%2C%20ename%2C%20job%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mgr%2C%20hiredate%2C%20sal%2C%20comm%2C%20deptno%20FROM%20emp%22%29%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%27create%20the%20dataadapter%20object%20%0A%20%20%20%20%20%20%20%20%20%20%20%20Dim%20adp%20As%20New%20OracleDataAdapter%28SQL%2C%20cn%29%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%27create%20the%20offline%20datatable%20%0A%20%20%20%20%20%20%20%20%20%20%20%20Dim%20ds%20As%20New%20DataSet%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%27fill%20the%20data%20set%20with%20a%20data%20table%20named%20emp%20%0A%20%20%20%20%20%20%20%20%20%20%20%20adp.Fill%28ds%2C%20%22emp%22%29%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%27clear%20up%20the%20resources%20and%20work%20offline%20%0A%20%20%20%20%20%20%20%20%20%20%20%20adp.Dispose%28%29%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%27check%20if%20it%20has%20any%20rows%20%0A%20%20%20%20%20%20%20%20%20%20%20%20If%20ds.Tables%28%22emp%22%29.Rows.Count%20%3E%200%20Then%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%27simply%20bind%20datatable%20to%20grid%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Me.DataGridView1.DataSource%20%3D%20ds.Tables%28%22emp%22%29%0A%20%20%20%20%20%20%20%20%20%20%20%20Else%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%27display%20message%20if%20no%20rows%20found%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20MessageBox.Show%28%22Not%20found%22%29%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Me.DataGridView1.Rows.Clear%28%29%20%0A%20%20%20%20%20%20%20%20%20%20%20%20End%20If%20%0A%20%20%20%20%20%20%20Catch%20ex%20As%20Exception%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%27display%20if%20any%20error%20occurs%20%0A%20%20%20%20%20%20%20%20%20%20%20%20MessageBox.Show%28%22Error%3A%20%22%20%26amp%3B%20ex.Message%29%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%27close%20the%20connection%20if%20it%20is%20still%20open%20%0A%20%20%20%20%20%20%20If%20cn.State%20%3D%20ConnectionState.Open%20Then%20%0A%20%20%20%20%20%20%20%20%20%20%20%20cn.Close%28%29%20%0A%20%20%20%20%20%20%20End%20If%20%0A%20%20%20%20End%20Try%20%0A%20%20End%20Sub%20%0AEnd%20Class
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.
Trackback(0)
|