Solved

problem connecting to sql

Posted on 2013-01-07
7
207 Views
Last Modified: 2013-01-12
I have 2 sql servers and I have to make a test connection to see which connection string to use.
then I take the connection string that worked and I try to connect again and select my database records

When I response.write the connection string and put it in my script, I am able to connect. If I use the If statement string variable then I am unable to connect and get an error.
I'll paste my script and see if someone can figure out what I am doing wrong.

  openConnection = false
MM_donorSelect_app1 = "DRIVER={SQL Server};Server=******.193;Database=oorahContests;Uid=tenflights;Pwd=800spring;"
MM_donorSelect_app2 = "DRIVER={SQL Server};Server=******.219;Database=oorahContests;Uid=tenflights;Pwd=800spring;"

Set myConn = Server.CreateObject("ADODB.Connection")
On Error Resume Next
myConn.open MM_donorSelect_app1
If err.Number = 0 Then

	If myConn.State = 1 then 
On Error GoTo 0

		stringUse = MM_donorSelect_app1
		openConnection = true

	End If
End If

'try connection 2
if openConnection = false then

On Error Resume Next
myConn.open MM_donorSelect_app2
If err.Number = 0 Then

	If myConn.State = 1 then 
On Error GoTo 0

		stringUse = MM_donorSelect_app2
		openConnection = true
	End If

End If
end if ' check connection

    Set MM_editCmd = Server.CreateObject("ADODB.Command")
    MM_editCmd.ActiveConnection = stringUse
   MM_editCmd.CommandText = query 
  MM_editCmd.Execute
   MM_editCmd.ActiveConnection.Close

Open in new window

if I response.write(stringUse), I get without quotes.
DRIVER={SQL Server};Server=******.193;Database=oorahContests;Uid=tenflights;Pwd=800spring;

Yet this works fine
    Set MM_editCmd = Server.CreateObject("ADODB.Command")
    MM_editCmd.ActiveConnection = stringUse
   MM_editCmd.CommandText =  "DRIVER={SQL Server};Server=******.193;Database=oorahContests;Uid=tenflights;Pwd=800spring;"
  MM_editCmd.Execute
   MM_editCmd.ActiveConnection.Close

Open in new window

0
Comment
Question by:rivkamak
  • 4
  • 3
7 Comments
 
LVL 33

Expert Comment

by:Big Monty
ID: 38751989
may I suggest a different approach? I assume you're trying to differentiate from a test server from a production server, as this is a common issue developers come across. Instead of checking the connection, check the server name you're on, then build the appropriate connection string:

      dim bIsProductionServer
    if InStr( LCase( Request.ServerVariables("SERVER_NAME") ), "www.testServerName.com" ) > 0 then
        bIsProductionServer = false
    else
        bIsProductionServer = true
    end if

    if not bIsProductionServer then
            connectionString = "connection Info Here for Test Server"
    else
            connectionString = "connection Info Here for Production Server"
     end if
0
 

Author Comment

by:rivkamak
ID: 38752002
No. We have 2 servers as load balancers. we can only have 1 sql server running at a time.
If we are doing maintenance on the sql server, we shut app1 down and use app2 sql server, but the people might still be on either server box.
I can't check server, i have to check the actual database connection.
0
 
LVL 33

Expert Comment

by:Big Monty
ID: 38752093
gotcha, then lets try this:

  openConnection = true
MM_donorSelect_app1 = "DRIVER={SQL Server};Server=******.193;Database=oorahContests;Uid=tenflights;Pwd=800spring;"
MM_donorSelect_app2 = "DRIVER={SQL Server};Server=******.219;Database=oorahContests;Uid=tenflights;Pwd=800spring;"

Set myConn = Server.CreateObject("ADODB.Connection")
On Error Resume Next

stringUse = MM_donorSelect_app1
myConn.open stringUse

If err.Number <> 0 Then    '-- error occured

    '-- try connection 2
    if myConn.State = 0 then '-- make sure another error didn't occur and that we are connected
         myConn.open MM_donorSelect_app2

        If err.Number = 0 Then   '-- successful connection
	      If myConn.State = 1 then
		  stringUse = MM_donorSelect_app2
	 	  openConnection = true
               else
                   openConnection = false
 	      End If
         else
              openConnection = false
         end if
  
End If

On Error GoTo 0

if openConnection then
    Set MM_editCmd = Server.CreateObject("ADODB.Command")
    MM_editCmd.ActiveConnection = stringUse
   MM_editCmd.CommandText = query 
  MM_editCmd.Execute
   MM_editCmd.ActiveConnection.Close
else
     Response.Write "could not connect to DB
end if

Open in new window

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:rivkamak
ID: 38752125
Let me change the question a bit.
a. I know the first string is supposed to work right now.
b. when i load the page I get an unable to connect with that username. SO it's obviously reading the right line of code because it tells me the name of the database. Then I hit f5 and it works.

Now why would that be so? what is the refresh doing that is allowing me to connect the second time around?
0
 
LVL 33

Expert Comment

by:Big Monty
ID: 38752183
sounds like the issue could be related to the load balancer. if the code fails during one page load but then works on the second page load, the issue isn't the code, but rather the server access. i'm not sure how you would go about troubleshooting that though.

are both servers suppose to be accessible at all times? does the webserver's firewall have access to both servers? whats the error you get when it fails?
0
 

Author Comment

by:rivkamak
ID: 38755631
are both servers suppose to be accessible at all times?
 Yes, they are behind a load balancer
does the webserver's firewall have access to both servers?
Both servers are behind the same physical firewall.  Windows firewall is turned off for both
whats the error you get when it fails?
Login failure
0
 
LVL 33

Accepted Solution

by:
Big Monty earned 500 total points
ID: 38756318
is the sql server set up to allow both windows authentication AND sql server authentication? if so, it could be an issue of the IIS user (usually IUSR_MachineName) not having access to the databases.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Viewers will learn how the fundamental information of how to create a table.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question