Site sponsored by: Idera Try Idera’s new SQL admin toolset
SQL Server Performance

  • Home
  • Articles
  • Forums
  • Tips
  • Quiz
  • FAQ's
  • Blogs
  • Software
  • Books
  • About Us
RSS Feeds
Sign in | Join


Article Topics

All Articles
Performance Tuning
Audit
Business Intelligence
Clustering
Reporting Services
Developer
General DBA
ASP.NET / ADO.NET

Write for Us

Share you SQL Server knowledge with others and raise your profile in the community More...
Latest Articles

Capture DDL Changes using Change Data Capture with SQL Server 2008 ...
Business Intelligence in Collaborative Planning, Forecasting and Replenishment
Inside SQL Server Cluster Setup and Troubleshooting Techniques - Part I ...
Configure and Manage Policy Based Management in SQL Server 2008 ...

More     
 
Latest FAQ's

Cannot Start SQL Server Service
Users are able to connect to report manager but not able ...
Errors when SQL Server Snapshot Replication is Running
How to Display Server Name or IP Address in a Reporting ...

More     
   
Latest Software Reviews

Spotlight on ApexSQL Doc 2008
ApexSQL Enforce
Embarcadero Change Manager
SQL Server DBA Dashboard

More     

articles >> asp.net / ado.net >> DataSets and XML – A Perfect Combination ...

DataSets and XML – A Perfect Combination

By : Joydip Kanjilal
Jan 22, 2007

Page 2 / 3


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


<< Prev Page     Next Page>>    








Home | Peformance Articles | Audit Articles | Business Intelligence Articles | Clustering Articles | Developer Articles | Reporting Services Articles | DBA Articles | ASP.NET / ADO.NET Articles | DBA FAQ's | Developer Peformance FAQ's | DBA Peformance FAQ's | Developer FAQ's | Clustering FAQ's | Error Messages | Audit Tool Reviews | Backup Tool Reviews | Coding Tool Reviews | Compare Tool Reviews | Documentation Tool Reviews | Design Tool Reviews | Monitoring Tool Reviews | Log Tool Reviews | Reporting Tool Reviews | Clustering Tool Reviews | Security Tool Reviews | Change Management Tool Reviews | Remote Access Tool Reviews | Book Reviews | Security Tool Reviews | QDPMA Performance Tuning | ADO.NET / ASP.NET | Administration | Analysis/OLAP Services | Application Development | Configuration | Components | ETL | Hardware | High Availability | Hints | Index | Misc | Operating Systems | Performance Tuning | Replication | T-SQL | Views


              © 1999-2008 by T10 Media. All rights reserved