Asp.net database data conversion to xml files

Source: Internet
Author: User

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.