Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Coding ADO.Net DataSet Objects ISYS 512

2 DataSet Object A DataSet object can hold several tables and relationships between tables. A DataSet is a set of disconnected 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.

3 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.

4 DataSet and Related Objects DataSet Tables Relations DataAdapter Commands Connection DataView DataSource

5 Structure of a Dataset Dataset Tables Data table Rows Data Row Columns Data Column DataView Relations Data Relation

6 Defining and Reading Data into a DataSet string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\CSharpexamples\\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");

7 Binding DataGridView with Code private void Form6_Load(object sender, EventArgs e) { string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\CSharpexamples\\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: Adapters Fill method is able to open the connection.

8 Binding a ListBox with Code private void Form7_Load(object sender, EventArgs e) { string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\CSharpexamples\\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?

9 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();

10 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:\\CSharpexamples\\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: objDataSet is defined as a form level object.

11 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:\\CSharpexamples\\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(); }

12 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

13 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:\\CSharpexamples\\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(); }

14 Using BindingSource for Listbox DataSet objDataSet = new DataSet(); BindingSource objBS = new BindingSource(); private void Form16_Load(object sender, EventArgs e) { string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\CSharpexamples\\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"; listBox1.DataSource = objBS; listBox1.DisplayMember = "CID"; textBox1.DataBindings.Add("text", objBS, "CID"); textBox2.DataBindings.Add("text", objBS, "Cname"); }

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

16 Data Binding with DataView Object string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\CSharpexamples\\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;

17 Selecting a Subset of Records with DataViews 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'"

18 Example: Display customers with the selected rating

19 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; }

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

21 Creating multiple tables: One Adapter, One Table string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\CSharpexamples\\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");

22 Creating Multiple Tables: One Adapter, Many Tables string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\CSharpexamples\\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");

23 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);

24 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);

25 Creating a Parent/Child Form

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

27 Using BindingSouce for Parent/Child Form 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:\\CSharpexamples\\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); objBS.DataSource = objDataSet; objBS.DataMember = "Customer"; textBox1.DataBindings.Add("text", objBS, "cid"); textBox2.DataBindings.Add("text", objBS, "cname"); dataGridView1.DataSource = objBS; dataGridView1.DataMember = "custOrder"; }

28 Navigation Buttons private void button1_Click(object sender, EventArgs e) { objBS.MoveNext(); } private void button2_Click(object sender, EventArgs e) { objBS.MovePrevious(); }

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

30 Parent/Child form with Listbox

31 DataSet objDataSet = new DataSet(); private void Form12_Load(object sender, EventArgs e) { string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\CSharpexamples\\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; }

32 Save a Copy of DataSet - DataSet.WriteXml DataSet objDataSet = new DataSet(); string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\CSharpexamples\\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"); objDataSet.WriteXml("C:\\CustOrderDS.xml");

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

Download ppt "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."

Similar presentations


Ads by Google