Question

I have this simple code to test that a DB is ready:

Function testlocalcon() As Boolean
    Dim constr As String = _clconstr

    Try
        Using t As New SqlConnection()
            constr = constr & " ; Connect Timeout=1"

            If Not t.State = Data.ConnectionState.Open Then
                t.ConnectionString = constr

                t.Open()
                If t.State = Data.ConnectionState.Open Then
                    Return True
                Else
                    Return False
                End If
            Else
                Return True
            End If
        End Using
    Catch ex As Exception
        Return False
    End Try
End Function

I do not want to execute a query, just to check the connection, but no matter what the time out parameter is ignored. I search here (Stackoverflow) and internet and found nothing in how to fix this.

Any one else have this problem? Or, are there any other ideas on how to let the application know that the DB is ready?

Was it helpful?

Solution

I appologize for the code differenc but this is C# and I have used it in the past. Its pretty simple and should be readable.

private SQLServerConnection
private SqlConnection _SQLServerConnection;
public SqlConnection SQLServerConnection
    {
        get
        {
            return _SQLServerConnection;
        }
        set
        {
            _SQLServerConnection = value;
        }
    }

private void SetSQLServerConnectionString (string sqlServerName, string databaseName, string saPassword, int connectTimeout)
{
    SQLServerConnection = new SqlConnection("Password=" + saPassword + ";Persist Security Info=True;User ID=sa;Initial Catalog=" + databaseName + ";Data Source=" + sqlServerName + ";connection timeout=" + connectTimeout.ToString(CultureInfo.InvariantCulture));
}

internal bool TestSQLServerConnection(string sqlServerName, string databaseName, string saPassword, int connectTimeout)
{
    try
    {
        SetSQLServerConnectionString(sqlServerName, databaseName, saPassword, connectTimeout);
        SQLServerConnection.Open();
        return true;
    }
    catch (SqlException e)
    {

        return false;
    }
    finally
    {
        SQLServerConnection.Close();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top