What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleOpen a database in the project's main directory during development in VB .NET
Keywordsdatabase, ADO.NET, VB .NET
CategoriesDatabase, VB.NET, Software Engineering
Build a connection object as usual. For example:

  1. Open the form designer. On the Toolbox, click the Data tab. Double click the OldDbConnection tool.
  2. Select the new connection object. In the Properties window, click on the ConnectionString property. Click the dropdown to the right and select . Use the resulting Data Link Properties dialog to specify the data provider and database.

Now open the form and find the statement that sets the connection object's ConnectionString property. Copy it into the form's Load event handler. Add code to locate the project directory st run time. Add the database's name and replace the database's name in the ConnectionString with this value.

The following code shows how the example program connects to the database Users.mdb in the project directory.

' Get the database file name.
Dim db_name As String = Application.ExecutablePath
db_name = db_name.Substring(0, db_name.LastIndexOf("\") + 1)
If db_name.EndsWith("\bin\") Then
    db_name = db_name.Substring(0, db_name.Length - 4)
End If
db_name &= "Users.mdb"

' Build the database connect string.
Me.connUsers.ConnectionString = _
    "Jet OLEDB:Global Partial Bulk Ops=2;" & _
    "Jet OLEDB:Registry Path=;" & _
    "Jet OLEDB:Database Locking Mode=1;" & _
    "Data Source=""" & db_name & """;" & _
    "Mode=Share Deny None;Jet OLEDB:Engine Type=5;" & _
    "Provider=""Microsoft.Jet.OLEDB.4.0"";" & _
    "Jet OLEDB:System database=;Jet OLEDB:SFP=False;" & _
    "persist security info=False;Extended Properties=;" & _
    "Jet OLEDB:Compact Without Replica Repair=False;" & _
    "Jet OLEDB:Encrypt Database=False;" & _
    "Jet OLEDB:Create System Database=False;" & _
    "Jet OLEDB:Don't Copy Locale on Compact=False;" & _
    "User ID=Admin;Jet OLEDB:Global Bulk Transactions=1"
For more information on using databases with VB .NET, see my book Visual Basic .NET Database Programming.
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.