Successfully reported this slideshow.

Disconnected data


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Disconnected data

  1. 1.  DataTable DataColumn DataRow DataView DataSet
  2. 2.  The DataTable object represents tabular data as rows, columns, and constraints. Use the DataTable object to hold data in memory while performing disconnected data operations. Can get a DataTable object by connecting to the database and returning table data.
  3. 3.  Can be explicitly created by instantiating the DataTable class You then add DataColumn objects to the class to define the type of data to be held. The DataColumn objects also contain constraints. Once the DataTable is defined with columns, you can add DataRow objects that contain data for the table.
  4. 4. private DataTable GetDataTable(){ DataTable employee = new DataTable("Employee"); DataColumn eid = new DataColumn("Eid"); eid.DataType = typeof(string); eid.MaxLength = 10; eid.Unique = true; eid.AllowDBNull = false; eid.Caption = "EID"; employee.Columns.Add(eid); return employee;}
  5. 5.  DataType MaxLength Unique AllowDBNull Caption
  6. 6.  The primary key of a DataTable object consists of one or more columns that define data that represent a unique identity for each row in the data.employee.PrimaryKey = new DataColumn[] {eid};
  7. 7.  The DataTable object contains a Rows collection, which contains a collection of DataRow objects. You can insert data into the Rows collection by using  Add method on the Rows collection  Load method on the DataTable object.
  8. 8. DataRow newemployee = employee.NewRow();newemployee["Eid"] = "123456789A";newemployee["FirstName"] = "Nancy";newemployee["LastName"] = "Davolio";newemployee["Salary"] = 10.00m;employee.Rows.Add(newemployee);Oremployee.Rows.Add("987654321X", "Andrew", "Ful ler", 15.00m);
  9. 9. employee.LoadDataRow( new object[]{ "987654321X", "Janet", "Leverling", 20.00m },LoadOption.OverwriteChanges); LoadOption enumeration value that has one of the following values:  OverwriteChanges  PreserveChanges  Upsert
  10. 10.  Detached : DataRow is created but not added to a DataTable. Added : DataRow is added to a DataTable. Unchanged : DataRow has not changed since the last call to the AcceptChanges method. The DataRow changes to this state when the AcceptChanges method is called. Modified : DataRow has been modified since the last time the AcceptChanges method was called. Deleted : DataRow is deleted using the Delete method of the DataRow.
  11. 11.  The AcceptChanges method is used to reset the DataRow state to Unchanged. After data has been loaded from the database, the RowState property of the loaded rows is set to Added. Calling AcceptChanges on the DataTable resets the RowState of all of the DataRow objects to Unchanged. If you modify the DataRow objects, their RowState changes to Modified. After the changes have been successfully sent to the data store by calling the AcceptChanges method the RowState changes to Unchanged.
  12. 12.  The Delete method on the DataRow is used to set the RowState of the DataRow to Deleted. There are many scenarios where you need to undelete a DataRow. The DataRow object doesn’t have an undelete method, but you can use the RejectChanges method to perform an undelete that may satisfy some scenarios.
  13. 13.  The DataRow object can hold up to three versions, or copies, of the data: Original, Current, and Proposed. When the DataRow is created, it contains a single copy of the data, which is the Current version. When the DataRow is placed into edit mode by executing its BeginEdit method, changes to the data are placed in a second version of the data, called the Proposed version. When the EndEdit method is executed, the Current version becomes the Original version, the Proposed version becomes the Current version, and the Proposed version no longer exists. After EndEdit has completed its execution, there are two versions of the DataRow data: Original and Current.
  14. 14.  You often need to create a full copy of a DataTable in your application. For example, you might want to assign a DataTable object to a GridView control to allow a user to edit the data, but you also might want to provide a cancel button that aborts all changes on the Web page. A simple way to implement this functionality is to create a copy of your DataTable object and use the copy for editing. DataTable copy = employee.Copy();
  15. 15.  You often require a copy of the DataTable schema without the data. You can accomplish this by invoking the Clone method on the DataTable. Use this method when an empty copy of the DataTable is required and to which DataRow objects will be added at a later time. DataTable clone = employee.Clone(); clone.ImportRow(employee.Rows[0]); The ImportRow method on the DataTable object copies a DataRow from a DataTable that has the same schema.
  16. 16.  The DataView object provides a window into a DataTable that can be sorted and filtered using the Sort, RowFilter, and RowStateFilter properties. A DataTable can have many DataView objects assigned to it, allowing the data to be viewed in many different ways without requiring the data to be reread from the database. The DataView object also contains the AllowDelete, AllowEdit, and AllowNew properties to constrain user input as needed.
  17. 17. protected void Button7_Click(object sender, EventArgs e){//get datatableDataTable employee = GetDataTable();//sort and displayDataView view = new DataView(employee);view.Sort = "LastName ASC, FirstName ASC, Salary DESC";GridView1.DataSource = view;GridView1.DataBind();}Also RowFilter can be set to a SQL WHERE clause without the word “WHERE.”view.RowFilter = "LastName like A% and Salary > 15";
  18. 18.  The DataSet is a memory-based relational representation of data and the primary disconnected data object. The DataSet contains a collection of DataTable and DataRelation objects.
  19. 19. private DataSet GetDataSet(){DataSet companyData = new DataSet("CompanyList");DataTable company = companyData.Tables.Add("company");company.Columns.Add("Id", typeof(Guid));company.Columns.Add("CompanyName", typeof(string));company.PrimaryKey = new DataColumn[] { company.Columns["Id"] };DataTable employee = companyData.Tables.Add("employee");employee.Columns.Add("Id", typeof(Guid));employee.Columns.Add("companyId", typeof(Guid));employee.Columns.Add("LastName", typeof(string));employee.Columns.Add("FirstName", typeof(string));employee.Columns.Add("Salary", typeof(decimal));employee.PrimaryKey = new DataColumn[] { employee.Columns["Id"] };companyData.Relations.Add("Company_Employee",company.Columns["Id"],employee.Columns["CompanyId"]);return companyData;
  20. 20. Label lbl = GetLabel(275, 20);DataSet companyList = GetDataSet();DataRelation dr = companyList.Relations["Company_Employee"];DataRow companyParent = companyList.Tables["company"].Rows[1];lbl.Text = companyParent["CompanyName"] + "<br />";foreach (DataRow employeeChild in companyParent.GetChildRows(dr)){lbl.Text += "&nbsp;&nbsp;&nbsp;" + employeeChild["Id"] + " "+ employeeChild["LastName"] + " "+ employeeChild["FirstName"] + " "+ string.Format("{0:C}", employeeChild["Salary"]) + "<br />";}lbl.Text += "<br /><br />";DataRow employeeParent = companyList.Tables["employee"].Rows[1];lbl.Text += employeeParent["Id"] + " "+ employeeParent["LastName"] + " "+ employeeParent["FirstName"] + " "+ string.Format("{0:C}", employeeParent["Salary"]) + "<br />";DataRow companyChild = employeeParent.GetParentRow(dr);lbl.Text += "&nbsp;&nbsp;&nbsp;" + companyChild["CompanyName"] + "<br />";
  21. 21.  A DataSet can be serialized as XML or as binary data to a stream or file. The DataSet can also be deserialized from XML or binary data from a stream or file. The serialized data can be transferred across a network via many protocols, including HTTP.