User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the VB.NET section within the Software Development category of DaniWeb, a massive community of 401,646 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,714 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.

Connecting to an .mdf database

Join Date: Mar 2008
Posts: 10
Reputation: green2go is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
green2go green2go is offline Offline
Newbie Poster

Re: Connecting to an .mdf database

  #2  
Mar 25th, 2008
Originally Posted by BluePaper View Post
Through using Visual Studio to learn more about ASP.net and VB.net I would like to connect to a database. I create an .mdf database file but I've no idea how to connect to it (well, I have some code below...) that's not my only problem but I'll ask the next question in a moment. The code I'm using is shown below, am I missing something? Or using the wrong connection string?

  1.  
  2. Private Function sqlDBConnection() As Boolean
  3. 'Set the variables
  4. Dim SQLConnectionString As String
  5. Dim SQLConnection As New SqlConnection()
  6. 'Create the Connection String
  7. SQLConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=\App_Data\Overland.mdf;Initial Catalog=Overland;Integrated Security=True;User Instance=True"
  8. 'Set the connection string into the SQL connection
  9. SQLConnection.ConnectionString = SQLConnectionString
  10. 'Open up the connection
  11. SQLConnection.Open()
  12. End Function
  13.  

A similar connection string works ( "Data Source=LAMBDA\SQLEXPRESS;Initial Catalog=General;Integrated Security=True" ) with an actual database server (SQL Express 2005) but modifying it a bit doesn't work for an SQL Server file (.mdf in this case) so if you have any idea please help

Secondly as you can see I've got the function set as a Boolean, however I don't return anything and the .Open() function doesn't return anything. Therefore how can I check to see whether a connection is succesful or not before it breaks out into an error? I'd like to be able to catch it just incase.

Thanks for your time.


You cannot directly 'Open' and .mdf file with Vb.NEt. You need to you an SQL server or MSDE. The second connection string you mention appears to connect to an SQL server, hence why it works.
As to your second question, If you are looking to catch exceptions, use the Try Catch method as demontrated below:

Try

SQLConnection.Open()

Catch Ex As System.Exception

MsgBox(ex.Message, MsgBoxStyle.Critical, "Error Information")

End Try

If you are looking to obtain the state of an SQL connection before you open it you can use this if statement:

If sqlConnection.State = ConnectionState.Closed Then

        sqlConnection.Open()

End If

Hope this helps.

Green2Go
Last edited by green2go : Mar 25th, 2008 at 6:02 pm. Reason: Typo :P
Reply With Quote  
All times are GMT -4. The time now is 6:24 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC