Here i am using Connection Timeout=1 to check the server connected or not.
In my aspx.cs
try
{
using (SqlConnection connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings[ServerConnection].ConnectionString))
{
connection.Open();
// My Process Here from server
}
}
catch (Exception ex)
{
using (SqlConnection connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings[ClientConnection].ConnectionString))
{
connection.Open();
// My Process Here from server
}
}
<div class="post-text" itemprop="text">
Is it correct way?
From this it takes long time to check server is connected or not. If it is not connected then catch part will execute the client machine sql.
It is very slow to check sql connection. any other way to check server connection?
Another option could be perhaps to fetch the correct connection string based on the host name or the machine name (this is not a web app ?) or just configure the app with the connection you need.
On my side my very first impression is that you were trying to implement a kind of retry pattern. It's likeky best to be always as direct as possible ie here :
you don't want to connect to a first machine and connect to another if it fails
you want to connect to a different machine depending on where your code run
Member
138 Points
357 Posts
How to check SQL Client-server Connectivity
Mar 16, 2018 01:17 PM|Hisanth|LINK
I am using client server system.
I have one server and 7 client machine.
my connection string in client app.config
Here i am using Connection Timeout=1 to check the server connected or not.
In my aspx.cs
<div class="post-text" itemprop="text">
Is it correct way?
From this it takes long time to check server is connected or not. If it is not connected then catch part will execute the client machine sql.
It is very slow to check sql connection. any other way to check server connection?
All-Star
52091 Points
23216 Posts
Re: How to check SQL Client-server Connectivity
Mar 16, 2018 01:27 PM|mgebhard|LINK
I would add a appSetting element that defines the current location rather than relying on a failed SQL connection.
Member
138 Points
357 Posts
Re: How to check SQL Client-server Connectivity
Mar 16, 2018 01:32 PM|Hisanth|LINK
How to do? any code snippets?
All-Star
52091 Points
23216 Posts
Re: How to check SQL Client-server Connectivity
Mar 16, 2018 01:38 PM|mgebhard|LINK
Simply read an appSetting node for the connection name.
https://odetocode.com/articles/345.aspx
This is pretty basic stuff. Can you explain the problem yo are trying to solve?
In web apps the connection string is driven by the environment.
https://msdn.microsoft.com/en-us/library/dd465326(v=vs.110).aspx
All-Star
48270 Points
17981 Posts
Re: How to check SQL Client-server Connectivity
Mar 16, 2018 01:50 PM|PatriceSc|LINK
Hi,
Another option could be perhaps to fetch the correct connection string based on the host name or the machine name (this is not a web app ?) or just configure the app with the connection you need.
On my side my very first impression is that you were trying to implement a kind of retry pattern. It's likeky best to be always as direct as possible ie here :
So try to have your code doing exactly that.