1 / 32

Coding ADO.Net DataSet Objects

Coding ADO.Net DataSet Objects. DataSet Object. A DataSet object can hold several tables and relationships between tables.

muncel
Download Presentation

Coding ADO.Net DataSet Objects

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Coding ADO.Net DataSet Objects

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

  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 DataView DataAdapter Commands Connection DataSource

  5. Structure of a Dataset Dataset Tables Data table Rows Data Row Columns Data Column Constraints Relations Constraint Data Relation

  6. 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");

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

  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:\\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]["Rating"].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:\\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?

  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:\\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. Move Next and Move Previous Procedures 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(); }

  13. 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();

  14. DataView ObjectConvenient 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

  15. 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;

  16. 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'"

  17. Example: Display customers with the selected rating

  18. 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; }

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

  20. 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");

  21. 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");

  22. 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);

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

  24. Creating a Parent/Child Form

  25. 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";

  26. 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; }

  27. 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;

  28. Parent/Child form with Listbox

  29. 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; }

  30. 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");

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

  32. 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(); }

More Related