Solved

problem connecting to sql

Posted on 2013-01-07
7
210 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

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.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

732 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