Connecting to an .mdf database

Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2008
Posts: 31
Reputation: BluePaper is an unknown quantity at this point 
Solved Threads: 0
BluePaper's Avatar
BluePaper BluePaper is offline Offline
Light Poster

Connecting to an .mdf database

 
0
  #1
Mar 25th, 2008
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

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.
Stone In Focus - Caffeine Group - CoffeePHP - Open Notes - Onyx - Redpoint Network - Espresso
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 12
Reputation: green2go is an unknown quantity at this point 
Solved Threads: 2
green2go green2go is offline Offline
Newbie Poster

Re: Connecting to an .mdf database

 
0
  #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

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:

  1. Try
  2.  
  3. SQLConnection.Open()
  4.  
  5. Catch Ex As System.Exception
  6.  
  7. MsgBox(ex.Message, MsgBoxStyle.Critical, "Error Information")
  8.  
  9. End Try

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

  1. If sqlConnection.State = ConnectionState.Closed Then
  2.  
  3. sqlConnection.Open()
  4.  
  5. End If

Hope this helps.

Green2Go
Last edited by green2go; Mar 25th, 2008 at 7:02 pm. Reason: Typo :P
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 31
Reputation: BluePaper is an unknown quantity at this point 
Solved Threads: 0
BluePaper's Avatar
BluePaper BluePaper is offline Offline
Light Poster

Re: Connecting to an .mdf database

 
0
  #3
Mar 25th, 2008
If I can't directly open the .mdf file how can I access it? It's just something I'm looking into since I've no idea what it is compared to an actual database on a SQL server :/

Thank you so much with the Try/Catch method, I had seen it once or twice before in PHP but it's not used much by the people I speak to but it seems to come in useful in VB.net thanks and the second part will be useful to, saving me opening up multiple unneeded connections. thanks
Stone In Focus - Caffeine Group - CoffeePHP - Open Notes - Onyx - Redpoint Network - Espresso
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 12
Reputation: green2go is an unknown quantity at this point 
Solved Threads: 2
green2go green2go is offline Offline
Newbie Poster

Re: Connecting to an .mdf database

 
1
  #4
Mar 25th, 2008
No problems for previous comment. You need to have an instance of SQL server express (http://msdn2.microsoft.com/en-gb/express/bb410792.aspx) free from Microsoft running on your local machine if you can't access an SQL server already setup on a network and/or are planning to deploy the application to locations without access to a server.

You then use a connection string to connect to the SQL server instance and read, write and query the data as you are already. See this link for connection strings for SQL Server 2005(http://www.connectionstrings.com/?carrier=sqlserver2005)
You can mount database files (.MDF) and other handy stuff using these commands.

Other than using a full blown server you could use an Access database to store the data, if there isn't lots of it, all that it requires is the correct drivers which I think are standard on most windows based machines anyway. You would have to convert the data from the SQL Server MDF file into an Access MDB file.

Green2Go
Last edited by green2go; Mar 25th, 2008 at 8:13 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 31
Reputation: BluePaper is an unknown quantity at this point 
Solved Threads: 0
BluePaper's Avatar
BluePaper BluePaper is offline Offline
Light Poster

Re: Connecting to an .mdf database

 
0
  #5
Mar 26th, 2008
Thank you for your time and help, after some fiddling and with that site I managed to open up the database file, now I'm going to test whether I can run SQL queries on it and such. Thanks once again, I'll keep that site bookmarked
Stone In Focus - Caffeine Group - CoffeePHP - Open Notes - Onyx - Redpoint Network - Espresso
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 4
Reputation: dkerr is an unknown quantity at this point 
Solved Threads: 0
dkerr dkerr is offline Offline
Newbie Poster

Re: Connecting to an .mdf database

 
0
  #6
Apr 1st, 2008
Can I query a SQL Server 2000 mydatabase.mdf file directly via SQL? If so, how?
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 2
Reputation: quangvo is an unknown quantity at this point 
Solved Threads: 0
quangvo quangvo is offline Offline
Newbie Poster

Re: Connecting to an .mdf database

 
0
  #7
Jan 22nd, 2009
Originally Posted by dkerr View Post
Can I query a SQL Server 2000 mydatabase.mdf file directly via SQL? If so, how?
No sure if you can do this in sql server 2000. But you can do it in 2005.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for VB.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC