The disconnected data access model of ADO.NET is centered on DataSets. DataSets are the core of ADO.NET architecture and represent an in memory representation of the database. They can be used with a wide variety of data sources and contain one or more DataTable objects. These DataTable objects in turn comprise of one or more DataRows and DataColumns. While the DataTable object represents every table within a DataSet; the DataColumn and the DataRow objects represent the columns and rows within a DataTable. The DataSet class provides a seamless support for XML. Using XML, we can use the DataSet class in ADO.NET to perform the CRUD (Create, Read, Update and Delete) operations without an underlying database to store the data. This article discusses how we can use DataSets and XML - considered by many as a perfect combination to perform the CRUD operations devoid of any underlying database to hold data.
What are DataSets?
A DataSet is in effect an in-memory representation of cached, disconnected data that is available as a as a collection of tables, relationships, constraints, etc. In other words, a DataSet is actually an in-memory representation of the entire database along with its tables and relationships. The DataSet class is contained in the System.Data namespace in ADO.NET. The DataSet class has the ability to represent itself as XML upon request or when you pass it through the tiers or components of an application.
The primary advantage in using DataSets in applications is its disconnected approach that enables data to be transferred across application boundaries. The primary disadvantage of using DataSets however that is it consumes large amount of memory that is particularly a nightmare if your DataSet contains high volume of data. Hence, it becomes detrimental to the application’s performance when the DataSet contains large amount of data due to excessive consumption of memory.
DataSet and XML – A perfect match
Before we delve deep any further, let us have a recap of XML. What is XML and why is it so useful? Extensible Markup Language (XML) is a simple, platform independent, flexible meta-markup language that provides a format for storing structured data and is great for efficient exchange of data across the internet. XML has rapidly gained wide popularity and the enterprise applications the world over are using XML as the primary format for data exchange across the globe.
The salient features of this language are:--
- Robust
- Support for UNICODE
- Hierarchical structure
- Platform Independent
- Present in human readable format
- Can support even complex data structures
Now that we have had a quick tour of XML, let us discuss the support for XML provided by the DataSet class in ADO.NET.
The DataSet class in ADO.NET contains a lot of methods that provide XML support. The following are some of the most important methods of this class:--
- The ReadXml Method
- The ReadXmlSchema Method
- The GetXml Method
- The GetXmlSchema Method
- The WriteXml Method
- The WriteXmlSchema Method
While the ReadXML, GetXML and WriteXML methods deal with the data as XML representation devoid of any schema, the ReadXMlSchema, WriteXMLSchema and the GetXMLSchema methods represent the schema only. These methods would become much clear as we move on to the next section where working (for CRUD operations) with DataSet instances and XML has been illustrated.
Working with DataSet and XML
This section discusses how we can perform the CRUD operations using DataSet and XML as the database to store the data.
Storing a DataSet instance to a XML File
The code sample below demonstrates how we can store data from a DataSet object directly to a disk file in XML format. The resultant XML document has been named as employee.xml
string connectionString = …; //Some connection string
SqlConnection sqlConnection =
new SqlConnection(connectionString);
SqlDataAdapter sqlDataAdapter =
new SqlDataAdapter(
"select name,id,joindate from employee", sqlConnection);
sqlConnection.Open();
DataSet dataSet = new DataSet();
sqlDataAdapter.Fill(dataSet);
dataSet.WriteXmlSchema(Server.MapPath("employee.xml"));
dataSet.WriteXml(Server.MapPath("employee.xml"));
Read Data from a XML file using a DataSet
The code that follows illustrates how we can search data from a XML document using a DataSet. The name of the method used is SearchData that returns a signed integer value of -1 if the record being searched (employee ID in our case is the key on which the search operation is being performed) is not found.
private int SearchData(int empID)
{
string filePath = …; //Some path indicating where the xml file // is stored
DataSet dataSetEmployee = GetEmployeeDataSet(); //Retrieve the // employee DataSet instance
DataTable dataTableEmployee = dataSetEmployee.Products.Tables[0];
DataView dataViewEmployee = new DataView();
dataViewEmployee = dataTableEmployee.DefaultView;
dataViewEmployee.Sort = "Emp_ID";
int rowIndex = dataViewEmployee.Find("empID");
return rowIndex;
}
Note that once the SearchData method returns the value of the row index (a value that is not equal to -1), an employee DataSet instance can be easily populated with the data representing the particular employee identified by the unique employee ID.
Add data to a XML file using a DataSet
The code sample below illustrates how we can append data to the xml file created above using a DataSet instance.
DataSet dataSetEmployee = GetEmployeeDataSet(); // The
// GetEmployeeDataSet method populates a DataSet object with
// employee data and schema information and returns the instance
DataRow employeeRecord = dataSetEmployee.Tables[0].NewRow();
employeeRecord[0] = 1;
employeeRecord[1] = “Joydip Kanjilal”;
dataSetEmployee.Tables[0].Rows.Add(employeeRecord);
string filePath = …; //Some path where the file is stored
dataSetEmployee.WriteXml(filePath,XmlWriteMode.WriteSchema);
Update Data in a XML file using DataSet
The code that follows updates the xml document with the most recent data in the DataSet instance. It works the same way as the above method (DeleteData) in the sense that it accepts the employee ID representing the employee record to be updated. It searches the same and updates the employee record if one exists.
private void UpdateData(int empID)
{
string filePath = …; //Some path indicating where the xml
//file is stored
DataSet dataSetEmployee = GetEmployeeDataSet();
DataTable dataTableEmployee = dataSetEmployee.Products.Tables[0];
DataView dataViewEmployee = new DataView();
dataViewEmployee = dataTableEmployee.DefaultView;
dataViewEmployee.Sort = "Emp_ID";
int rowIndex =dvProducts.Find("empID");
if (rowIndex == - 1)
Response.Write("This employee code does not exist");
else
{
dataViewEmployee.RowFilter = " Emp_ID =" + empID;
dataViewEmployee[0][“Emp_Name”] = “Joydip”;
dataSetEmployee.WriteXml(filePath,XmlWriteMode.WriteSchema);
}
}
Delete Data from XML File using a DataSet
The following code illustrates how we can delete data from an XML file using a DataSet. The method DeleteData accepts the employee ID as the parameter searches the employee record in the DataSet instance and deletes the record if one exists.
private void DeleteData(int empID)
{
string filePath = …; //Some path indicating where the xml file is stored
DataSet dataSetEmployee = GetEmployeeDataSet();
DataTable dataTableEmployee = dataSetEmployee.Products.Tables[0];
DataView dataViewEmployee = new DataView();
dataViewEmployee = dataTableEmployee.DefaultView;
dataViewEmployee.Sort = "Emp_ID";
int rowIndex = dataViewEmployee.Find("empID");
if (rowIndex == - 1)
Response.Write("This employee code does not exist");
else
{
dataViewEmployee.RowFilter = " Emp_ID =" + empID;
dataViewEmployee.Delete(0);
dataSetEmployee.WriteXml(filePath,XmlWriteMode.WriteSchema);
}
}
Note that in both of these above methods (UpdateData & DeleteData) you can alternately call the SearchData method to find whether the employee record exists prior to updating or deleting the record. This would minimize a lot of code redundancy.
References
Please refer to the following links for further reference on this topic:--DataGrids, DataSets, and XML Strings
Using XML in a DataSet
DataSets and Xml Representation
Smarten Up Your DataSets with XML Schema
Points to be noted
Note that if the XML is generated originally from a DataSet, using DataSet.WriteXml method, you can directly import it with DataSet.ReadXml method. On the other hand, if the XML data is in some other format, you need to read the Xml into an XmlDocument object and then write the necessary code to import the records one after the other, or use XSLT to transform the XML data into a format that can be understood by the DataSet class.Note that when writing data to a XML file you can choose if you want to write the Schema only, Schema and Records, or just the records. When you read the XML data you can also decide if the tables and columns should be created based on the schema, or a new schema should be created and then the data XML read, ignoring the fields that are not present in the dataset or the datatable instance.
The DataSet class is generic in the sense that it is independent of the source of the data that is populating it. This is why, unlike a DataReader, we have only one type of DataSet. Note that we have DataReaders for each type of Data Providers, i.e., each type of databases.
A point of advice: Avoid using DataSets if you only require using it as a data container, i.e., a container of data. I would recommend using a Custom Business Entity class instead that would boost the performance to a large extent due to avoidance of un-necessary boxing and un-boxing issues. Refer to my article at Passing Data through Layers that illustrates how we can build and implement a custom business entity class in lieu of a DataSet to pass data through the layers of an application.
No comments:
Post a Comment