Example of querying database records written to XML files in ASP

Source: Internet
Author: User
Set the followingCodeSave it as asp_xml.asp and run it:

< %

' By Dicky 21:52:18 am QQ: 25941 E-mail: AppleBBS@GMail.Com

Const Issql =   1 ' Define 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   ' Query data records in read-only mode

If Rs. EOF Then

Response. Write " Sorry, no record! "   ' If no record exists

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.doc umentelement = Objrootnode


Do   While   Not Rs. EOF ' Loop all records

' Response. Write RS ("productname") & "<br>"

Set Objrownode = Objxmldom. createelement ( " Row " ) ' Create parent node

Set Objnode = Objxmldom. createelement ( " Productname " ) ' Create a subnode

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 ends



Objxmldom. Save " D: \ myxmldoc. xml "   ' Writing XML files allows you to use variables to customize file names on pages.

Response. Write " <SCRIPT> alert ('Congratulations, the XML file is successfully written! '); </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 () ' Close database connection



Function Openconn (conn) ' Open Database Connection

Dim Connstr

If Issql =   1   Then   ' For SQL Server databases

' SQL Server database connection parameters: User Name, user password, database name, connection name (local, IP address used in other places)

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 ' For access databases

Dim DB

' For the first use, modify the address of the local database and change the database name accordingly. For example, change Dicky. mdb to Dicky. asp to prevent malicious download of the 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. Check the connection string. "

Response. End

End   If

Set RS = Server. Createobject ( " ADODB. recordset " )

End Function


Function Closeconn () ' Close database connection

Conn. Close

Set Conn =   Nothing

End Function

% >

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.