Classic ASP Script to Create XML
I am in need of using classic ASP to create an XML file from a SQL2005 express database. I am receving a 500 error. I believe my problem is the connection string, I was wondering if I could get a fresh set of eyes on this to possibly provide me a new avenue. Here is the code:
<%
Dim objConn, strConnect, strSQL, rs, tb, objFSO, xmlFile, objWrite
xmlFile = Server.MapPath("inventory.xml")
tb = chr(9)
set objFSO = Server.CreateObject( "Scripting.FileSystemObject" )
Set objConn = Server.C开发者_运维百科reateObject( "ADODB.Connection" )
objConn.Open "Provider=SQLOLEDB;Data Source=mybox.com;Initial Catalog=myDB;UserId=myID;Password=myPW;"
If Not objFSO.FileExists( xmlFile ) Then objFSO.CreateTextFile( xmlFile )
set objWrite = objFSO.OpenTextFile( xmlFile, 2 )
objWrite.WriteLine("<?xml version=""1.0"" encoding=""ISO-8859-1""?>")
objWrite.WriteLine("<data>")
strSQL = "SELECT * FROM table1"
Set rs = objConn.Execute(StrSQL)
Do While not rs.EOF
objWrite.WriteLine(tb & "<marker>")
objWrite.WriteLine(tb & tb & "<name>" & rs("name") & "</name>")
objWrite.WriteLine(tb & tb & "<address>" & replace(rs("address"),"&","&") & "</address>")
objWrite.WriteLine(tb & tb & "<city>" & rs("city") & "</city>")
objWrite.WriteLine(tb & tb & "<state>" & rs("size") & "</state>")
objWrite.WriteLine(tb & tb & "<zipcode>" & rs("zipcode") & "</zipcode>")
objWrite.WriteLine(tb & tb & "<lat>" & rs("lat") & "</lat>")
objWrite.WriteLine(tb & tb & "<lng>" & rs("lng") & "</lng>")
objWrite.WriteLine(tb & "</marker>")
rs.MoveNext
Loop
objWrite.WriteLine("</data>")
objWrite.Close()
%>
I appreciate any fresh perspective anyone can share. Thanks, --Matt
The thing that stands out most to me is your connection string. I'm not sure how it handles the extra spaces in "Initial Catalog"
, but I can say that mybox.com
is almost certainly the wrong value for the data source. You should use an internal name there, and not expose your database to the public internet.
Also, it's been a long time since I've used classic asp, but IIRC you need to call .MoveNext()
before accessing any records — at the top of the loop rather than the bottom.
Finally, don't forget to close your connection, and use good error handling there to make sure it will reach the code that closes both the connection and the file, even if an error occurs earlier in the code. Otherwise you'll end up locking the file or database on yourself.
精彩评论