Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

Come verificare se la stringa di connessione è valida?

Puoi consentire a SqlConnectionStringBuilder costruttore controllalo:

bool isValidConnectionString = true;
try{
    var con = new SqlConnectionStringBuilder("ABC");
}catch(Exception)
{
    // can be KeyNotFoundException, FormatException, ArgumentException
    isValidConnectionString = false;
}

Ecco una panoramica dei ConnectionStringBuilders per i diversi fornitori di dati:

Provider                   ConnectionStringBuilder 
System.Data.SqlClient      System.Data.SqlClient.SqlConnectionStringBuilder
System.Data.OleDb          System.Data.OleDb.OleDbConnectionStringBuilder
System.Data.Odbc           System.Data.Odbc.OdbcConnectionStringBuilder
System.Data.OracleClient   System.Data.OracleClient.OracleConnectionStringBuilder