Example of querying database records writing XML files in ASP

Source: Internet
Author: User
Tags object end modify sql access database access
xml| Sample | data | database Save the following code as a asp_xml.asp run:
<%
' By Dicky 2005-03-22 21:52:18 AM qq:25941 e-mail:applebbs@gmail.com
Const issql = 1 ' defines the database type as SQL Server
Call Openconn (Conn) ' Open database connection

Dim Rs,sql
Set Rs = Server.CreateObject ("ADODB.") RecordSet ")
SQL = ' SELECT * from ' products ' ORDER by ProductName
Rs.Open sql,conn,1,1 ' querying data records in read-only mode
If rs.eof Then
Response.Write "Sorry,no record!" ' If there is no record
Else
Dim objxmldom, Objrootnode, Objnode
Set objxmldom = Server.CreateObject ("MSXML2. DOMDocument ")" Create an XML Document object
Set Objrootnode = objxmldom.createelement ("xml") ' Create root node
Objxmldom.documentelement = Objrootnode

Do, not rs.eof ' loops out all records
' Response.Write Rs ("ProductName") & "<br>"
Set Objrownode = objxmldom.createelement ("row") ' Create parent node
Set objnode = objxmldom.createelement ("ProductName") ' Creates child nodes
Objnode.text = Rs ("ProductName")
Objrownode.appendchild (Objnode)

Set objnode = objxmldom.createelement ("UnitPrice")
Objnode.text = Rs ("UnitPrice")
Objrownode.appendchild (Objnode)

Set objnode = objxmldom.createelement ("UnitsInStock")
Objnode.text = Rs ("UnitsInStock")
Objrownode.appendchild (Objnode)

Objrootnode.appendchild (Objrownode)
Rs.MoveNext:Loop ' Loop End

Objxmldom.save "D:\MyXMLDoc.xml" writes XML file can use variables to allow users to customize file names on the page
Response.Write "<script>alert (' Congratulations, write XML file successfully!") ');</script> "
Set objnode = Nothing ' Destroy object
Set Objrownode = Nothing ' Destroy object
Set Objrootnode = Nothing ' Destroy object
End If
Rs.close
Set Rs = Nothing

Call Closeconn () ' Shut Down database connection

Function openconn (Conn) ' Open database connection
Dim ConnStr
If Issql = 1 Then ' If it is a SQL Server database
' SQL Server database connection parameters: User name, user password, database name, connection name (local locally, field IP)
Dim Sqlusername,sqlpassword,sqldatabasename,sqllocalname
sqlusername = "sa"
SQLPassword = ""
Sqldatabasename = "Northwind"
Sqllocalname = "(local)"
ConnStr = "Provider = SQLOLEDB; User ID = "& Sqlusername &"; Password = "& SQLPassword &"; Initial Catalog = "& Sqldatabasename &"; Data Source = "& Sqllocalname &";
Else ' If it is an Access database
Dim Db
' First use please modify our database address and modify the database name accordingly, such as modifying Dicky.mdb to dicky.asp (prevent malicious downloading of an Access database)
Db = "Dicky.mdb"
ConnStr = "Provider = Microsoft.jet.oledb.4.0;data Source =" & Server.MapPath (DB)
End If
On Error Resume Next
Set Conn = Server.CreateObject ("ADODB. Connection ")
Conn.Open ConnStr
If ERR Then
' Err.Clear
Set Conn = Nothing
Response.Write "Database connection error, please check the connection string. "
Response.End
End If
Set Rs = Server.CreateObject ("Adodb.recordset")
End Function

Function closeconn () ' Closes the database connection
Conn.close
Set Conn = Nothing
End Function
%>


Related Article

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.