• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 332
  • Last Modified:

Server down redirect

Hey folks,
  I'm using SQL Database as my datasource.  What is the best method to look at a different server if server one is down.

I need to do this in VB code behind inthe Master file I guess.
  • 2
  • 2
1 Solution
Daniel WilsonCommented:
The best solution is a SQL Server cluster so that your app doesn't ahve to deal with this.  But ... doing this in the code-behind shouldn't be too hard -- as long as you're just trying to read.

Somewhere ... maybe actually in a page from which your content pages inherit ... you could put a Connection property.

This property would:
* Instantiate a SqlConnection, if not already done
* Attempt to connect to the primary DB server
* If that fails, attempt to connect to the second DB server
* Return the connection

Think that will work for you?

lrbristerAuthor Commented:
Yes...that would work.  You have an example?
Daniel WilsonCommented:
Try something like this:

you will need a module-level
Private _GoodCon as SqlConnection

Public ReadOnly Property GoodConnection() as SqlConnection
   if _GoodCon is nothing then
     set _GoodCon = new SqlConnection(ConnString1)
   end if
   if not TestCon(_GoodCon) then
     if _GoodCon.ConnectionString = ConnString1 then 
        _GoodCon.ConnectionString = ConnString2
        _GoodCon.ConnectionString = ConnString1
     if not TestCon(_GoodCon) then
       throw new exception ("Can't connect to either server!!!")
     end if
   end if
   return _GoodCon
 End Get
End Property
Private Function TestCon(C as SqlConnection) as Boolean
  dim cmd as new SqlCommand("Select 1", C)
   if C.State = Closed then
   end if
  catch ex as exception
   return false
  end try
  return true
end function

Open in new window

lrbristerAuthor Commented:
Thanks.  This put me on the track I needed.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now