How i could check if i have connection to an SQL Server with the Connection String known in C#?
+4
A:
using (var connection = new SqlConnection("connectionString"))
{
try
{
connection.Open();
Console.WriteLine("Connection Ok");
}
catch (SqlException)
{
Console.WriteLine("Connection Not Ok");
}
}
František Žiačik
2010-06-16 13:53:08
+1
A:
You can also test it outside of your code by making an UDL file (a text file with extension .UDL). Then right-click it, get the properties and enter the connectionstring details.
Press the "Test Connection" button and then you'll know.
Sam
2010-06-16 13:55:56
Interesting approach, thanx
2010-06-16 14:07:40
+3
A:
I"m not sure if you are asking how to validate a connection string or check to see if a current connection is open If you are trying to check if a current connection is open you can use.
connection.State
Gratzy
2010-06-16 13:59:47