2

I need to validate the ConnectionString property on a DataContext object to ensure that LINQ can get a connection to the database. I have tried the two methods below but, they lock up the application if the connection string is invalid. Is there another way in LINQ to do this?

    public static bool TestDBConnection(connectionString)
    {
        bool result = true;
        DomainClassesDataContext db = new DomainClassesDataContext(connectionString);

        try
        {
            // Hangs if connectionString is invalid rather than throw an exception
            db.Connection.Open();

            // Initially, I was just trying to call DatabaseExists but, this hangs as well if the conn string is invalid
            if (!db.DatabaseExists())
            {
                result = false;
            }
        }
        catch (Exception ex)
        {
            result = false;
            logger.Fatal(ex.Message);
            logger.Fatal(ex.StackTrace);
        }
        finally
        {
            db.Dispose();
        }

        return result;
    }
4

1 に答える 1

1

Connection Timeout接続文字列の を 5 秒などの低い値に設定します。

開きが足りない。古い、プールされた接続を取得する可能性があります。クエリを実行します: SELECT NULL.

于 2013-01-24T23:05:44.460 に答える