问题
I have an SQL database set up in which I would like to use a vbscript to read from it.
Here is the code I am trying to use.
SQL_Command_String = "SELECT * FROM Table1 JOIN Table2 WHERE Table1.ID = '5' AND Table2.Date LIKE '2011'"
Set Rs = SQLConnection.Execute(SQL_Command_String)
DO WHILE NOT Rs.EOF
file1="./seminars/"&seminar_type&"/"&seminar_year&"/"&Rs("date")&"-"&Rs("year")&"_"&Rs("last")&".pdf"
seminar_year and seminar_type are constants I have initialized and declared to be seminar_year = 2011 and seminar_type = section1.
I believe I have the SQL_Command_String messed up with the LIKE statement. I'm not sure if it is being used properly. I just want it to find cells that include 2011 in the date but i'm not sure if I should include wildcards for the month and day portions of the date and if so what wildcards?
When I comment out the declaration (Set) of Rs the page loads fine (except that it hasn't filled out the table). The error that is displayed when included is "An error occurred on the server when processing the URL. Please contact the system administrator. If you are the system administrator please click here to find out more about this error. "
The here simply takes you to a microsoft page about running os IIS 7 which I have already accounted for.
I'm really stumped on this one my thoughts are I have the SQL command incorrect, there is a confliction with the new framework that is out, or I am not declaring the reader correctly or perhaps i'm not using the correct type of reader.
I would appreciate any insights the SO community has. Thanks and please know I am very new at VBscripts
回答1:
Try removing the AND Table2.Date LIKE '2011' part and see if it works. I'm guessing it's because your date column is a date or datetime data type as opposed to char type. I think LIKE will only work for char data types, so you will need to do an actual date comparison e.g. Table2.Date >= '1/1/2011' AND Table2.Date <= '12/31/2011' or something similar, depending on the database your using.
回答2:
I found the offending code.
Set Rs = SQLConnection.Execute(SQL_Command_String)
The Set keyword cannot be used when using a reader. I have found no documentation backing this up but removing the Set resolved the issue.
The rest of the connection Code is correct. Thank you all for your input.
来源:https://stackoverflow.com/questions/11109076/using-vbscript-to-read-from-an-sql-database