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

Chapter 18 - Data sources and datasets 1 Outline How to create a data source How to use a data source How to use Query Builder to build a simple query.
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 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.
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.
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.
VB.NET Database Access ISYS 812. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
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.
VB.NET Database Tools ISYS 573. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
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.
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.
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
From VS C# 2010 Programming, John Allwork 1 VS2010 C# Programming - DB intro 1 Topics – Database Relational - linked tables SQL ADO.NET objects Referencing.
VB.NET Database Access ISYS546. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
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.
Overview of Data Access MacDonald Ch. 15 MIS 324 Professor Sandvig.
CIS 451: Using ASP.NET Objects with SQL Dr. Ralph D. Westfall February, 2009.
CHAPTER EIGHT Accessing Data Processing Databases.
ASP.NET Rina Zviel-Girshin Lecture 5
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.
Christopher M. Pascucci.NET Programming: Databases & ADO.NET.
Module 4: Building DataSets. Overview Working in a Disconnected Environment Building DataSets and DataTables Binding and Saving a DataSet Defining Data.
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.
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.
1 Data Classes- DataView DataGridView Control. Objectives 2  Data Class  DataAdapter  DataReader  DataSet  DataTable  DataView  DataGridView Control.
Command Object’s ExecuteNonQuery Method ISYS 512.
1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query.
Introduction to ADO.Net and VS Database Tools and Data Binding ISYS 350.
Databases and ADO.NET Programming Right from the Start with Visual Basic.NET 1/e 11.
2314 – Programming Language Concepts Introduction to ADO.NET.
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.
DataGridView. Displaying data in a tabular format is a task you are likely to perform frequently. The DataGridView control is designed to be a complete.
Introduction to ADO.Net and Visual Studio Database Tools ISYS 350.
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.
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.
Coding ADO.NET Objects: Connection, Command, DataReader.
Distributed Database Systems
ASP.NET and ADO.NET.
VB.NET Using Database.
Browser (Client Side) 瀏覽器 (客戶端)
Coding ADO.NET Objects: Connection, Command, DataReader
Database Handling Class and Service
Array ISYS 350.
ASP.NET and ADO.NET.
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 string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\SalesDB2011.accdb"; OleDbConnection objConn = new OleDbConnection(strConn); DataSet objDataSet = new DataSet(); string strSQL= "select * from customer;"; OleDbDataAdapter objAdapter=new OleDbDataAdapter(strSQL, objConn); objAdapter.Fill(objDataSet, "Customer");

Binding DataGridView with Code private void Form6_Load(object sender, EventArgs e) { string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\SalesDB2011.accdb"; OleDbConnection objConn = new OleDbConnection(strConn); DataSet objDataSet = new DataSet(); string strSQL= "select * from customer;"; OleDbDataAdapter objAdapter=new OleDbDataAdapter(strSQL, objConn); objAdapter.Fill(objDataSet, "Customer"); dataGridView1.DataSource = objDataSet; dataGridView1.DataMember = "Customer"; } Note: Adapter’s Fill method is able to open the connection.

Binding a ListBox with Code private void Form7_Load(object sender, EventArgs e) { string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\SalesDB2011.accdb"; OleDbConnection objConn = new OleDbConnection(strConn); DataSet objDataSet = new DataSet(); string strSQL = "select * from customer;"; OleDbDataAdapter objAdapter = new OleDbDataAdapter(strSQL, objConn); objAdapter.Fill(objDataSet, "Customer"); listBox1.DataSource = objDataSet.Tables["customer"]; listBox1.DisplayMember = "CID"; listBox1.ValueMember = "Cname"; } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { textBox1.Text = listBox1.SelectedValue.ToString(); } Note: How to access other fields?

Navigate a dataset to access a column of a row in a table DataSet: has a collection of tables –Tables: each member is a DataTable object Table: has a collection of rows –Rows: each member is a DataRow object »Row: has a collection of fields Example: the Rating field of the first row of the Customer table: –objDataSet.Tables["customer"].Rows[0]["Ratin g"].ToString();

Binding a ListBox and display selected record in textboxes DataSet objDataSet = new DataSet(); private void Form7_Load(object sender, EventArgs e) { string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\SalesDB2011.accdb"; OleDbConnection objConn = new OleDbConnection(strConn); string strSQL = "select * from customer;"; OleDbDataAdapter objAdapter = new OleDbDataAdapter(strSQL, objConn); objAdapter.Fill(objDataSet, "Customer"); listBox1.DataSource = objDataSet.Tables["customer"]; listBox1.DisplayMember = "CID"; listBox1.ValueMember = "Cname"; } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { textBox1.Text = listBox1.SelectedValue.ToString(); textBox2.Text = objDataSet.Tables["customer"].Rows[listBox1.SelectedIndex]["Rating"].ToString(); } Note: Different from previous example, objDataSet is defined as a form level object. Why?

Navigate Records in a Table DataSet objDataSet = new DataSet(); int rowIndex = 0; private void Form8_Load(object sender, EventArgs e) { string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\SalesDB2011.accdb"; OleDbConnection objConn = new OleDbConnection(strConn); string strSQL = "select * from customer;"; OleDbDataAdapter objAdapter = new OleDbDataAdapter(strSQL, objConn); objAdapter.Fill(objDataSet, "Customer"); textBox1.Text = objDataSet.Tables["customer"].Rows[rowIndex]["cid"].ToString(); textBox2.Text = objDataSet.Tables["customer"].Rows[rowIndex]["cname"].ToString(); textBox3.Text = objDataSet.Tables["customer"].Rows[rowIndex]["city"].ToString(); }

private void button1_Click(object sender, EventArgs e) { rowIndex += 1; if (rowIndex > objDataSet.Tables["customer"].Rows.Count - 1) rowIndex = objDataSet.Tables["customer"].Rows.Count - 1; textBox1.Text = objDataSet.Tables["customer"].Rows[rowIndex]["cid"].ToString(); textBox2.Text = objDataSet.Tables["customer"].Rows[rowIndex]["cname"].ToString(); textBox3.Text = objDataSet.Tables["customer"].Rows[rowIndex]["city"].ToString(); } private void button2_Click(object sender, EventArgs e) { rowIndex -= 1; if (rowIndex <0) rowIndex = 0; textBox1.Text = objDataSet.Tables["customer"].Rows[rowIndex]["cid"].ToString(); textBox2.Text = objDataSet.Tables["customer"].Rows[rowIndex]["cname"].ToString(); textBox3.Text = objDataSet.Tables["customer"].Rows[rowIndex]["city"].ToString(); } Move Next and Move Previous Procedures

Use foreach loop to access records in table’s Rows Collection string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\SalesDB2011.accdb"; OleDbConnection objConn = new OleDbConnection(strConn); string strSQL = "select * from customer;"; OleDbDataAdapter objAdapter = new OleDbDataAdapter(strSQL, objConn); DataSet objDataSet = new DataSet(); objAdapter.Fill(objDataSet, "Customer"); string strOutput=""; foreach (DataRow objRow in objDataSet.Tables["customer"].Rows) strOutput = strOutput+ objRow["cid"].ToString() + objRow["cname"].ToString() + objRow["city"].ToString() + Environment.NewLine; textBox1.Text = strOutput.ToString();

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. To create a DataView, use a Table’s DefaultView property: –DataView objDataView = new DataView(); –objDataView = objDataSet.Tables["customer"].DefaultView; DataView can be used as a DataSource in data binding. Other useful methods: –Find, Sort

Data Binding with DataView Object string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\SalesDB2011.accdb"; OleDbConnection objConn = new OleDbConnection(strConn); string strSQL = "select * from customer;"; OleDbDataAdapter objAdapter = new OleDbDataAdapter(strSQL, objConn); DataSet objDataSet = new DataSet(); objAdapter.Fill(objDataSet, "Customer"); DataView objDataView = new DataView(); objDataView = objDataSet.Tables["customer"].DefaultView; dataGridView1.DataSource = objDataView;

Selecting a Subset of Records with DataView’s RowFilter Property objDataView.RowFilter = criteria; Criteria is a string of condition Examples of criteria: –“Rating=‘A’” –“City = ‘SF’” The criteria can be complex condition. Use logical operators AND, OR to write a complex condition. Examples of complex condition: –“City = ‘SF’ AND Rating=‘A’” –"Rating='A' and City='SF' or rating='C'"

Example: Display customers with the selected rating

DataView Example private void radioButton1_CheckedChanged(object sender, EventArgs e) { if (radioButton1.Checked) objDataView.RowFilter = "Rating='A'"; else if (radioButton2.Checked) objDataView.RowFilter = "Rating='B'"; else objDataView.RowFilter = "Rating='C'"; dataGridView1.DataSource = objDataView; }

DataView with Sort objDataView.Sort = "Cname"; dataGridView1.DataSource = objDataView;

Creating multiple tables: One Adapter, One Table string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\SalesDB2011.accdb"; OleDbConnection objConn = new OleDbConnection(strConn); string strSQL = "select * from customer;"; OleDbDataAdapter objAdapter = new OleDbDataAdapter(strSQL, objConn); DataSet objDataSet = new DataSet(); objAdapter.Fill(objDataSet, "Customer"); string strSQL2 = "select * from orders;"; OleDbDataAdapter objAdapter2 = new OleDbDataAdapter(strSQL2, objConn); objAdapter2.Fill(objDataSet, "orders");

Creating Multiple Tables: One Adapter, Many Tables string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\SalesDB2011.accdb"; OleDbConnection objConn = new OleDbConnection(strConn); string strSQL = "select * from customer;"; OleDbDataAdapter objAdapter = new OleDbDataAdapter(strSQL, objConn); DataSet objDataSet = new DataSet(); objAdapter.Fill(objDataSet, "Customer"); string strSQL2 = "select * from orders;"; objAdapter.SelectCommand.CommandText = strSQL2; objAdapter.Fill(objDataSet, "orders");

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) DataRelation objRel; 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 DataRelation objRel; objRel = new DataRelation("custOrder", objDataSet.Tables["Customer"].Columns["cid"], objDataSet.Tables["orders"].Columns["cid"]); objDataSet.Relations.Add(objRel);

Creating a Parent/Child Form

Binding Textbox and Binding GridView to a Relation textBox1.DataBindings.Add("text", objDataSet, "customer.cid"); textBox2.DataBindings.Add("text", objDataSet, "customer.cname"); dataGridView1.DataSource = objDataSet; dataGridView1.DataMember = "Customer.custOrder";

Parent/Child Form DataSet objDataSet = new DataSet(); private void Form11_Load(object sender, EventArgs e) { string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\SalesDB2011.accdb"; OleDbConnection objConn = new OleDbConnection(strConn); string strSQL = "select * from customer;"; OleDbDataAdapter objAdapter = new OleDbDataAdapter(strSQL, objConn); objAdapter.Fill(objDataSet, "Customer"); string strSQL2 = "select * from orders;"; objAdapter.SelectCommand.CommandText = strSQL2; objAdapter.Fill(objDataSet, "orders"); DataRelation objRel; 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"; } private void button1_Click(object sender, EventArgs e) { this.BindingContext[objDataSet, "Customer"].Position += 1; } private void button2_Click(object sender, EventArgs e) { this.BindingContext[objDataSet, "Customer"].Position -= 1; }

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 DataView objDataView = new DataView(); objDataView = objDataSet.Tables["orders"].DefaultView; objDataView.RowFilter = "CID='" + listBox1.SelectedValue.ToString() + "'"; dataGridView1.DataSource = objDataView;

Parent/Child form with Listbox

DataSet objDataSet = new DataSet(); private void Form12_Load(object sender, EventArgs e) { string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\SalesDB2011.accdb"; OleDbConnection objConn = new OleDbConnection(strConn); string strSQL = "select * from customer;"; OleDbDataAdapter objAdapter = new OleDbDataAdapter(strSQL, objConn); objAdapter.Fill(objDataSet, "Customer"); string strSQL2 = "select * from orders;"; objAdapter.SelectCommand.CommandText = strSQL2; objAdapter.Fill(objDataSet, "orders"); listBox1.DataSource=objDataSet.Tables["Customer"]; listBox1.DisplayMember = "CID"; listBox1.ValueMember = "CID"; } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { textBox1.Text = objDataSet.Tables["customer"].Rows[listBox1.SelectedIndex]["Cname"].ToString(); textBox2.Text = objDataSet.Tables["customer"].Rows[listBox1.SelectedIndex]["Rating"].ToString(); DataView objDataView = new DataView(); objDataView = objDataSet.Tables["orders"].DefaultView; objDataView.RowFilter = "CID='" + listBox1.SelectedValue.ToString() + "'"; dataGridView1.DataSource = objDataView; }

Save a Copy of DataSet - DataSet.WriteXml string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\SalesDB2011.accdb"; OleDbConnection objConn = new OleDbConnection(strConn); string strSQL = "select * from customer;"; OleDbDataAdapter objAdapter = new OleDbDataAdapter(strSQL, objConn); objAdapter.Fill(objDataSet, "Customer"); string strSQL2 = "select * from orders;"; objAdapter.SelectCommand.CommandText = strSQL2; DataSet objDataSet = new DataSet(); objAdapter.Fill(objDataSet, "orders"); objDataSet.WriteXml("C:\\CustOrderDS.xml");

ReadXML DataSet objDataSet=new DataSet(); objDataSet.ReadXml("c:\\CustOrderDS.xml"); dataGridView1.DataSource = objDataSet; dataGridView1.DataMember = "Customer";

Using BindingSouce for navigation DataSet objDataSet = new DataSet(); BindingSource objBS = new BindingSource(); private void Form9_Load(object sender, EventArgs e) { string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\SalesDB2011.accdb"; OleDbConnection objConn = new OleDbConnection(strConn); string strSQL = "select * from customer;"; OleDbDataAdapter objAdapter = new OleDbDataAdapter(strSQL, objConn); objAdapter.Fill(objDataSet, "Customer"); objBS.DataSource = objDataSet; objBS.DataMember = "Customer"; textBox1.DataBindings.Add("text", objBS, "CID"); textBox2.DataBindings.Add("text", objBS, "Cname"); } private void button1_Click(object sender, EventArgs e) { objBS.MoveNext(); } private void button2_Click(object sender, EventArgs e) { objBS.MovePrevious(); }