Solved

problem connecting to sql

Posted on 2013-01-07
7
204 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 32

Expert Comment

by:Big Monty
Comment Utility
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
Comment Utility
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 32

Expert Comment

by:Big Monty
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:rivkamak
Comment Utility
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 32

Expert Comment

by:Big Monty
Comment Utility
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
Comment Utility
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 32

Accepted Solution

by:
Big Monty earned 500 total points
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

763 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now