Open .MDF Sql sever connection VB.Net

Posted on 2009-04-27
Last Modified: 2013-11-26

I'm building a windows forms application  in Visual Studio 2008.  I've set-up a SQL server 2008 .MDF database and attached it to my project. All the data is stored, used, shown etc and seems to work fine.

However, when I debug the project (for the first time!) or publish it I get a' timeout expired' error.

I think it is becuase I haven't explicity opened the connection to the database in code.

If I check the .MDF properties before and after I manually click the database in the Server Explorer,  I can see that the database properies 'State' changes from closed to open and then I don't get the error. However, I need to fix for publishing.

How do I open this connection in VB.Net?     (  .Net 3.5  )

Question by:davecocks
    LVL 9

    Accepted Solution

    If you are looking to obtain the state of an SQL connection before you open it you can use this if statement:
    you can do :
    If sqlConnection.State = ConnectionState.Closed Then
    End IfIf sqlConnection.State = ConnectionState.Closed Then
    End If

    Open in new window

    LVL 46

    Assisted Solution

    Hi dave,

    Have a look at the following:

    Good Luck!

    LVL 1

    Author Closing Comment

    Hi thanks both of you for your help. I've split the points and I hope everyone is ok with the split. I used both the code from sree ven and the reference from Gary to track down a soltuion:

    'Opens SQL connection
            'Prevents the timeout exception error
            'I put it on the load of each form
            Dim cn As New SqlClient.SqlConnection(My.Settings.internalConnectionString)
            If cn.State = ConnectionState.Broken OrElse cn.State = ConnectionState.Closed Then
                'reopen your connection:
            End If

    PS. the database was called 'internal.mdf'

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Article by: Martin
    Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    Via a live example, show how to shrink a transaction log file down to a reasonable size.
    Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now