I've created a small method to test the connection to a database:
public bool TestConnection(string provider, string serverName, string initialCatalog, string userId, string password, bool integratedSecurity)
{
var canConnect = false;
var connectionString = integratedSecurity ? string.Format("Provider={0};Data Source={1};Initial Catalog={2};Integrated Security=SSPI;", provider, serverName, initialCatalog)
: string.Format("Provider={0};Data Source={1};Initial Catalog={2};User ID={3};Password={4};", provider, serverName, initialCatalog, userId, password);
var connection = new OleDbConnection(connectionString);
try
{
using (connection)
{
connection.Open();
canConnect = true;
}
}
catch (Exception exception)
{
}
finally
{
connection.Close();
}
return canConnect;
}
Despite the method works it doesn t seems right to me. Is any way to test the connection without having to catch the exception? Is it possible to achieve the same result in a different way?
OleDbConnectionStringBuilder
– SLaks Nov 9 '11 at 22:57