I am trying to set up a connection to my SQL server inside of a VBScript.
Whenever I have a connection to my SQL server there is an error on the webpage that says "There was a problem processing the URL" and to contact the administrator.
I know my connection string is correct because it works on all the others pages.
Here is the code I am using so far to set up the SQL connection
Const SQL_Connection_String = "Provider=sqloledb;SERVER=SQLPROD;DATABASE=MyDataBase;UID=MyUsername;PWD=MyPassword;"
Set SQLConnection = CreateObject("ADODB.Connection")
SQLConnection.provider = "ADODB.Connection"
Set SQLConnectionRecordSet = CreateObject("ADODB.Recordset")
From what I know this should be a valid way of setting up the connection in a VBscript. I have not really found any good documentation on configuring these connections.
What I would like to know is: am I setting up the connection correctly, and Is there some strategy I could use to troubleshoot this? Meaning I'd like to be able to have a print statement that returns true or false if the connection was successful. I would also like to somehow print the contents of my reader to see if it has read from the database and if it read what I wanted it to read.
Thanks for any help. I'm using VBscript and ASP.net for the first time so I don't know any of the subtlties.
Your
SQLConnection.provider = "ADODB.Connection"
is definitely wrong. As you specify the provider in the connection string, there is a (small) chance that deleting that line, will make your script 'work'.
UPDATE
(1) If you have problems connecting to your database, use this excellent source to get started on theory and samples, create an empty file (e.g. constr.udl), start it, and use the GUI to specify/test your connection. Leaving the dialog will save the connection string in the file, from where you can pick it up via a decent (Unicode enabled) editor.
(2) If you have too many errors on your ASP page, extract the (simplified) database code to a commandline .vbs script; that way you can focus on one foe (and deal with second one when you are sure, your SELECTs return the desired data).
(3) If you have problems with specific SQL statements/features/expressions, study the docs; in this case look into your favorite SQL book or follow Google to LIKE (according to the horse). JOIN may be a candidate too. Then use (2) to start from simple statements, proceed to more complex ones after you have mastered the seemingly boring. ("Table2.Date LIKE '2011'" may work or not, depending on the type and content of the Date column (and isn't Date a reserved/key word?)
(While Derek will surely read an answer to another of his question, other poeple starting here will probably profit from reading this too.)
Here's some example code for scripting ADO:
Set SQLConnection = CreateObject("ADODB.Connection")
SQLConnection.Open ConnectionString
Set rs = CreateObject("ADODB.RecordSet")
rs.Open "SELECT SYSTEM_USER AS Username, session_id, auth_scheme, net_transport, client_net_address FROM sys.dm_exec_connections WHERE (@@SPID = session_id)", SQLConnection
' Loop through each record until there are no more
While NOT rs.EOF
' Loop through each field
For Each field In rs.Fields
output.Write field.Value & " "
next
rs.MoveNext
output.WriteLine
Wend
来源:https://stackoverflow.com/questions/11105466/trying-to-display-data-from-an-sql-database-using-vbscript