Link to home
Start Free TrialLog in
Avatar of rivkamak
rivkamakFlag for United States of America

asked on

problem connecting to sql

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

Avatar of Big Monty
Big Monty
Flag of United States of America image

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
Avatar of rivkamak

ASKER

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.
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

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?
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?
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
ASKER CERTIFIED SOLUTION
Avatar of Big Monty
Big Monty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial