First, use sqlconnection to open the database tutorial link, use sqlcommand to open the Employees data table, and save the data to the DataSet object. Create an XmlDocument object using the DataSet object as the data source, and Save the data to the specified XML file using the Save method of the XmlDocuemnt object.
<% @ Import Namespace = "System. Data" %>
<% @ Import Namespace = "System. Data. OleDb" %>
<Script language = "c #" runat = "server">
Void Page_Load (Object sender, EventArgs e)
{
String strConnection, strSQL;
DataSet objDataSet = new DataSet ();
OleDbConnection objConnection = null;
OleDbDataAdapter objAdapter = null;
StrConnection = "Provider = Microsoft. Jet. OLEDB.4.0 ;";
StrConnection + = "Data Source =" + Server. MapPath ("Northwind. mdb ");
StrSQL = "SELECT FirstName, LastName FROM Employees ;";
ObjConnection = new OleDbConnection (strConnection );
ObjAdapter = new OleDbDataAdapter (strSQL, objConnection );
ObjAdapter. Fill (objDataSet, "Employees ");
ObjDataSet. WriteXml (Server. MapPath ("Employees2.xml "));
Response. Write ("<a href = 'ployees2. xml'> View xml file </a> ");
}
</Script>
Save data from database query results to XML files
<% @ Import Namespace = "System. Data" %>
<% @ Import Namespace = "System. Data. OleDb" %>
<Script language = "VB" runat = "server">
Sub Page_Load (Sender As Object, E As EventArgs)
Dim strConnection As String
Dim strSQL As String
Dim objDataSet As New DataSet ()
Dim objConnection As OleDbConnection
Dim objAdapter As OleDbDataAdapter
StrConnection = "Provider = Microsoft. Jet. OLEDB.4.0 ;"&_
"Data Source =" + MapPath ("EmployeeDatabase. mdb ")
StrSQL = "SELECT FirstName, LastName FROM Employee ;"
ObjConnection = New OledbConnection (strConnection)
ObjAdapter = New OledbDataAdapter (strSQL, objConnection)
ObjAdapter. Fill (objDataSet, "Employees ")
ObjDataSet. WriteXml (Server. MapPath ("Employee2.xml "))
Response. Write ("XML File Generated ")
End Sub
</Script>
C # practices
<% @ Import Namespace = "System. Data" %>
<% @ Import Namespace = "System. Data. OleDb" %>
<Script language = "c #" runat = "server">
Void Page_Load (Object sender, EventArgs e)
{
String strConnection, strSQL;
DataSet objDataSet = new DataSet ();
OleDbConnection objConnection = null;
OleDbDataAdapter objAdapter = null;
StrConnection = "Provider = Microsoft. Jet. OLEDB.4.0 ;";
StrConnection + = "Data Source =" + Server. MapPath ("Northwind. mdb ");
StrSQL = "SELECT FirstName, LastName FROM Employees ;";
ObjConnection = new OleDbConnection (strConnection );
ObjAdapter = new OleDbDataAdapter (strSQL, objConnection );
ObjAdapter. Fill (objDataSet, "Employees ");
ObjDataSet. WriteXml (Server. MapPath ("Employees2.xml "));
Response. Write ("<a href = 'ployees2. xml'> View xml file </a> ");
}
</Script>