?
Solved

problem connecting to sql

Posted on 2013-01-07
7
Medium Priority
?
217 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

764 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