Coding ADO.Net DataSet Objects
DataSet Object A DataSet object can hold several tables and relationships between tables. A DataSet is a set of disconnedted data. Data is extracted from the database and stored in the DataSet object. Updates to the DataSet must copy back to the database to make the changes permanent.
DataSet and Related Objects DataSet: Can contain multiple tables and relationships. DataTable object: Represents a table in the dataset. DataAdapter: This the object used to pass data between the database and the dataset. The Fill method copies the data into the dataset, and the Update method copies the updates back into the database. DataView: This represents a specific view of the DataTables held in the dataset.
DataSet and Related Objects DataSet Tables DataAdapter Commands Connection DataView DataSource
Structure of a Dataset Dataset Tables Data table Rows Data Row Columns Data Column Constraints Constraint Relations Data Relation
Reading Data into a Table Dim strConn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\SalesDB2007.accdb" Dim objConn As New OleDbConnection(strConn) Dim objDataSet As New DataSet() Dim strSQL As String = "select * from customer;" Dim objAdapter As New OleDbDataAdapter(strSQL, objConn) objAdapter.Fill(objDataSet, "Customer")
Binding DataGridView with Code Imports System.Data.OleDb Public Class Form4 Inherits System.Windows.Forms.Form Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim strConn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\SalesDB2007.accdb" Dim objConn As New OleDbConnection(strConn) Dim objDataSet As New DataSet() Dim strSQL As String = "select * from customer;" Dim objAdapter As New OleDbDataAdapter(strSQL, objConn) objAdapter.Fill(objDataSet, "Customer") DataGridView1.DataSource = objDataSet DataGridView1.DataMember = "Customer" End Sub Note: Adapter’s Fill method is able to open the connection.
Binding a ListBox with Code Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim strConn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\SalesDB2007.accdb" Dim objConn As New OleDbConnection(strConn) Dim objDataSet As New DataSet() Dim strSQL As String = "select * from customer;" Dim objAdapter As New OleDbDataAdapter(strSQL, objConn) objAdapter.Fill(objDataSet, "Customer") ListBox1.DataSource = objDataSet.Tables("Customer") ListBox1.DisplayMember = "CID" End Sub Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged TextBox1.Text = ListBox1.SelectedItem.item("cname") End Sub
DataTable’s Rows Property This is a collection of all the records in a table, a collection of DataRow objects. DataRow object’s properties and methods.
Accessing a Record with Index objAdapter.Fill(objDataSet, "Customer") TextBox1.Text = objDataSet.Tables("Customer").Rows(rowIndex).Item(0) TextBox2.Text = objDataSet.Tables("Customer").Rows(rowIndex).Item(1)
Access Rows in a DataRow Collection dim objTable as DataTable = objDataset.Tables("Customer") dim objRow as DataRow For each objRow in objTable.Rows strResult=strResult+“ " & objRow("cid") & " " & objRow("cname") & vbCrLf Next
DataView Object Convenient for Binding and Display Records Meeting Criteria The DataView object exposes a complete table or a subset of the records from a table. Table’s DefaultView property: –Dim ObjDataView As New DataView() –ObjDataView.Table = objDataSet.Tables("Cust") –Or: ObjDataView = objDataSet.Tables("Cust").DefaultView DataView can be used as a DataSource in data binding. Other useful methods: –Find, Sort
Data Binding with DataView Object Dim strConn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\SalesDB2007.accdb" Dim objConn As New OleDbConnection(strConn) Dim objDataSet As New DataSet() Dim strSQL As String = "select * from customer;" Dim objAdapter As New OleDbDataAdapter(strSQL, objConn) objAdapter.Fill(objDataSet, "Cust") Dim ObjDataView As New DataView() ObjDataView = objDataSet.Tables("Cust").DefaultView DataGridView1.DataSource = ObjDataView
Selecting a Subset of Records with DataView’s RowFilter Property objDataView.RowFilter = criteria Note: The criteria can be a simple or complex condition.
DataView Example Dim ObjDataView As New DataView() Dim newRating As String If RadioButton1.Checked = True Then newRating = "A" ElseIf RadioButton2.Checked Then newRating = "B" Else newRating = "C" End If ObjDataView.Table = objDataSet.Tables("Cust") ObjDataView.RowFilter = "rating='" & newRating & "'" DataGridView1.Visible = True DataGridView1.DataSource = ObjDataView
Creating multiple tables: One Adapter, One Table Dim strConn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\SalesDB2007.accdb" Dim objConn As New OleDbConnection(strConn) Dim strSQLCustomer As String = "select * from customer;" Dim strSQLOrders As String = "select * from orders" Dim objAdapterCust As New OleDbDataAdapter(strSQLCustomer, objConn) Dim objAdapterOrd As New OleDbDataAdapter(strSQLOrders, objConn) Dim objDataset As New DataSet objAdapterCust.Fill(objDataset, "Customer") objAdapterOrd.Fill(objDataset, "orders")
Creating Multiple Tables: One Adapter, Many Tables Dim strConn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\SalesDB2007.accdb" Dim objConn As New OleDbConnection(strConn) Dim strSQLCustomer As String = "select * from customer;" Dim strSQLOrders As String = "select * from orders" Dim objAdapter As New OleDbDataAdapter(strSQLCustomer, objConn) Dim objDataset As New DataSet objAdapter.Fill(objDataset, "Customer") objAdapter.SelectCommand.CommandText = strSQLOrders objAdapter.Fill(objDataset, "orders") DataGrid1.DataSource = objDataset DataGridView1.DataMember = "customer"
Adding Relationship to a Dataset The Dataset object has a Relations property. It is a collection of DataRelations. We can use a relationship to enforce the referential integrity. To define a DataRelation: –DataRelObj=DataRelation(RelationName, ParentTable Field, ChildTableField) Dim objRel As DataRelation objRel = New DataRelation("custOrder", objDataset.tables("customer").columns("cid"), objDataset.tables("orders").columns("cid")) Adding a relation to the dataset: –objDataSet.Relations.Add(objRel)
Code to Define and Add a Relation Dim objRel As DataRelation objRel = New DataRelation("custOrder", objDataSet.Tables("customer").Columns("cid"), objDataSet.Tables("orders").Columns("cid")) objDataSet.Relations.Add(objRel)
Parent/Child Form Dim objDataset As New DataSet Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim strConn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\SalesDB2007.accdb" Dim objConn As New OleDbConnection(strConn) Dim strSQLCustomer As String = "select * from customer;" Dim strSQLOrders As String = "select * from orders" Dim objAdapter As New OleDbDataAdapter(strSQLCustomer, objConn) objAdapter.Fill(objDataset, "Customer") Dim objRel As DataRelation objAdapter.SelectCommand.CommandText = strSQLOrders objAdapter.Fill(objDataset, "orders") objRel = New DataRelation("custOrder", objDataset.Tables("customer").Columns("cid"), objDataset.Tables("orders").Columns("cid")) objDataset.Relations.Add(objRel) TextBox1.DataBindings.Add("text", objDataset, "customer.cid") TextBox2.DataBindings.Add("text", objDataset, "customer.cname") DataGridView1.DataSource = objDataset DataGridView1.DataMember = "Customer.custOrder" End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Me.BindingContext(objDataset, "customer").Position += 1 End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Me.BindingContext(objDataset, "customer").Position -= 1 End Sub
Parent/Child Form without a Relation This form lets users to select a CID from a listbox, then displays parent information in textboxes and child records in a datagrid by defining a dataview on Orders table based on selected CID. –Use View’s Filter Dim ObjDataView As New DataView() ObjDataView.Table = objDataSet.Tables(“Orders") ObjDataView.RowFilter = “cid='" & ListBox1.SelectedItem.item("cid") & "'" DataGrid1.DataSource = ObjDataView
Private Sub MainSubForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\salesDB.mdb" Dim objConn As New OleDbConnection(strConn) Dim strSQLCustomer As String = "select * from customer;" Dim strSQLOrders As String = "select * from orders" Dim objAdapter As New OleDbDataAdapter(strSQLCustomer, objConn) objAdapter.Fill(objDataSet, "Customer") objAdapter.SelectCommand.CommandText = strSQLOrders objAdapter.Fill(objDataSet, "orders") lstCID.DataSource = objDataSet.Tables("customer") lstCID.DisplayMember = "CID" End Sub Private Sub lstCID_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstCID.SelectedIndexChanged Dim ObjDataView As New DataView ObjDataView.Table = objDataSet.Tables("Orders") ObjDataView.RowFilter = "cid='" & lstCID.SelectedItem.item("cid") & "'" txtCname.Text = lstCID.SelectedItem.item("Cname") txtCity.Text = lstCID.SelectedItem.item("City") txtRating.Text = lstCID.SelectedItem.item("rating") txtOrdCount.Text = ObjDataView.Count.ToString dgOrders.DataSource = ObjDataView End Sub
Save a Copy of DataSet - DataSet.WriteXml Dim strConn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\SalesDB2007.accdb" Dim objConn As New OleDbConnection(strConn) Dim strSQLCustomer As String = "select * from customer;" Dim strSQLOrders As String = "select * from orders" Dim objAdapter As New OleDbDataAdapter(strSQLCustomer, objConn) Dim objDataset As New DataSet objAdapter.Fill(objDataset, "Customer") objAdapter.SelectCommand.CommandText = strSQLOrders objAdapter.Fill(objDataset, "orders") objDataset.WriteXml("C:\CustOrderDS.xml")
ReadXML Dim objDataset As New DataSet objDataset.ReadXml("C:\custorderDS.xml") DataGridView1.DataSource = objDataset DataGridView1.DataMember = "customer"