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.

Slides:



Advertisements
Similar presentations
Coding ADO.Net DataSet Objects ISYS 512. DataSet Object A DataSet object can hold several tables and relationships between tables. A DataSet is a set.
Advertisements

VB.NET Database Access ISYS546. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
Creating Custom Controls ISYS 512/812. Inheritance The process in which a new class can be based on an existing class, and will inherit that class’s interface.
Authenticating Users in an ASP.NET Application. Web Site Administration Tool From VS 2008, click Website/ ASP.Net Configuration to open Web Site Administration.
Introduction to ADO.NET Programming. ADO.NET Objects Data Set.NET Applications Data Reader Command Object Connection Object Managed Data Provider (OLEDB)
ASP.NET and ADO.NET. ASP.NET Server Controls Intrinsic Controls: These controls correspond to their HTML counterparts. –Ex. Textbox, listbox, button,
VB.NET Database Tools ISYS Net Applications OLE DB Provider OLE DB Data Source OLE DB Provider ODBC Data Source SQL Server Data Source SQL Server.Net.
Coding ADO.NET Objects: Connection, Command, DataReader.
ASP.Net AJAX. AJAX Asynchronous JavaScript and XML: – JavaScript, Document Object Model, Cascade Style Sheet, XML, server-side script such as.Net, etc.
Introduction to Data Adapter. A Simplified View of ADO.Net Objects Ado.Net Data Provider Connection Adapter Command Reader Dataset Data Consumer WinForm.
ASP.NET and ADO.NET. ADO.NET Objects Data Set.NET Applications Data Reader Command Object Connection Object Managed Data Provider (OLEDB) Database.
Introduction to ADO.NET. ADO.NET Objects Data Set.NET Applications Data Reader Command Object Connection Object Managed Data Provider (OLEDB) Database.
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.
Web Site Security ISYS 512/812. Authentication Authentication is the process that determines the identity of a user. Web.config file – node Options: –Windows:
Coding ADO.NET Objects: Connection, Command, DataReader.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
Working with Session and Application Objects. Postback and Variables Variables declared in a web page including ADO.Net objects may be reinitialized and.
Concurrency Control. R/RR/W W/W User 2 ReadWrite User 1 Read Write R/W: Inconsistent Read problem. W/W: Lost Update problem.
VB.NET Database Access ISYS 812. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
VB.NET Database Tools ISYS 573. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
Introduction to Web Application Development with.Net and Web Service ISYS 350.
ASP.NET and ADO.NET. Bind the DataReader to a DataGrid Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" Dim objConn.
Introduction to ADO.NET. ADO.NET Objects Data Set.NET Applications Data Reader Command Object Connection Object Managed Data Provider (OLEDB) Database.
Updating Database. Two Approaches to Update Database 1. Using the DataSet object, the updates are first applied to the DataSet then applied to the database.
Coding ADO.NET Objects: Connection, Command, DataReader.
Introduction to ADO.Net and Visual Studio Database Tools ISYS 512.
Working with Session. Postback and Variables Variables declared in a web page including ADO.Net objects may be reinitialized and lose their values.
VB.NET Database Access ISYS546. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
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.
Shopping Cart. Search and display product information Add item to cart View cart contents Check out.
Introduction to Web Service ISYS 512. Web Service XML Web Service Web services are classes that are stored on the web which can instantiate and use in.
Introduction to ADO.NET. ADO.NET Objects Data Set.NET Applications Data Reader Command Object Connection Object Managed Data Provider (OLEDB) Database.
ASP and HTML. Anchor Tag testAnchorTag Demo: ASPNet/ASPNETProdListSelf.ASPX.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS546.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
Introduction to ADO.Net and Visual Studio Database Tools
VB.NET Database Access ISYS546. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
Chapter 11 Introduction to Database Processing. Class 11: Database Processing Use a Visual Studio Wizard to establish a database connection used to load.
Programming with Visual Basic.NET An Object-Oriented Approach  Chapter 8 Introduction to Database Processing.
Introduction to ADO.Net and Visual Studio Database Tools ISYS 512.
CIS 451: Using ASP.NET Objects with SQL Dr. Ralph D. Westfall February, 2009.
Session 8: ADO.NET. Overview Overview of ADO.NET What is ADO.NET? Using Namespaces The ADO.NET Object Model What is a DataSet? Accessing Data with ADO.NET.
Module 4: Building DataSets. Overview Working in a Disconnected Environment Building DataSets and DataTables Binding and Saving a DataSet Defining Data.
Using Adapter Wizard ISYS 512. Data Adapter Wizard – 2 nd Level of Using ADO.Net Configure Data Adapter and generating a dataset: –From the Data tab of.
ASP.Net and HTML. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load dim strConn as string ="Provider=Microsoft.Jet.OLEDB.4.0;Data.
ADO.NET Objects – Data Providers Dr. Ron Eaglin. Requirements Visual Studio 2005 Microsoft SQL Server 2000 or 2005 –Adventure Works Database Installed.
1 Data Classes- DataView DataGridView Control. Objectives 2  Data Class  DataAdapter  DataReader  DataSet  DataTable  DataView  DataGridView Control.
Introduction to ADO.Net and VS Database Tools and Data Binding ISYS 350.
Created by Alia Al-Abdulkarim 2008 Visual Basic Vs. Java.
Coding ADO.NET Objects: Connection, Command, DataReader.
ADO.NET Objects Data Adapters Dr. Ron Eaglin. Agenda Builds on Information in Part I Should have working knowledge of creating a database connection Continuation.
Module 5 Data Classes DataView – DataGridView Control 1.
C# Classes ISYS 512. Introduction to Classes A class is the blueprint for an object. –It describes a particular type of object. –It specifies the properties.
The Windows Forms DataGrid A Complex-Bound.NET GUI Control Mike FITZSIMON SYSTEMSARCHITECT F ITZSIMON IT C ONSULTING PTY LTD.
Coding ADO.Net DataSet Objects ISYS 512. DataSet Object A DataSet object can hold several tables and relationships between tables. A DataSet is a set.
Directory and File. Access Files in a Directory Name space: System.IO The Directory and File classes contain only shared methods that set or return information.
Shopping Cart Demo. Shopping Cart Search and display product information Add item to cart View cart contents Check out.
Coding ADO.NET Objects: Connection, Command, DataReader.
Coding ADO.NET Objects: Connection, Command, DataReader
ASP.NET and ADO.NET.
1.الدوال Function 2.الاجراءاتSub Procedure 3.وحده نمطيه Add Module
Coding ADO.NET Objects: Connection, Command, DataReader
Database Handling Class and Service
ASP.NET and ADO.NET.
VB Classes ISYS 512.
Working with Session and Application Objects
Introduction to ADO.NET
Command Object’s ExecuteNonQuery Method
Introduction to ADO.Net and Visual Studio Database Tools.
Presentation transcript:

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"