Link to home
Start Free TrialLog in
Avatar of davecocks
davecocks

asked on

Open .MDF Sql sever connection VB.Net

HI,

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  )

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Sreedhar Vengala
Sreedhar Vengala
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of davecocks
davecocks

ASKER

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:
            cn.Open()
        End If

PS. the database was called 'internal.mdf'