Visual Studio and local databases

Working with local databases

Overview

In Visual Studio 2005 we added a number of features to help developers build and deploy applications that need a local data store. Here’s a quick peek at how it works.

In order to work with a local database file, you can simply add the file to your project (e.g. using the Project/Add Existing Item… menu). We currently support adding SQL Server data files (.mdf), Jet (Access) data files (.mdb) and SQL Mobile data files (.sdf). Note that in order to be able to use .mdf files, you need to have installed SQL Server Express. SQL Express is available on the VS CD or at http://go.microsoft.com/fwlink/?LinkId=49251. With SQL Server Express installed, you will also be able to create new databases through ‘Project/Add New Item…/Database’.

Once the database file is in the project, VS will do a few things:

      1. It will automatically add a connection in the Database Explorer so you can edit the database schema or the data.

      2. It will make sure that the connection strings are serialized using a relative path (more on this below).

      3. The first time the file is added, VS will also launch the Data Source wizard to create a new typed dataset.

Full path vs relative path

One of the reasons why it was hard to work with database files before is that the full path to the database was serialized in different places. This made it harder to share a project and also to deploy the application. In this version, the .NET runtime added support for what we call the DataDirectory macro. This allows Visual Studio to put a special variable in the connection string that will be expanded at run-time. So instead of having a connection string like this:

      “Data Source=.\SQLExpress;AttachDbFileName=c:\program files\app\data.mdf”

You can have a connection string like this:

      “Data Source=.\SQLExpress;AttachDbFileName=|DataDirectory|\data.mdf”

This connection string syntax is supported by the SqlClient and OleDb managed providers.

By default, the |DataDirectory| variable will be expanded as follow:

      – For applications placed in a directory on the user machine, this will be the app’s (.exe) folder.
– For apps running under ClickOnce, this will be a special
data folder created by ClickOnce
– For Web apps, this will be the App_Data folder

Under the hood, the value for |DataDirectory| simply comes from a property on the app domain. It is possible to change that value and override the default behavior by doing this:

      AppDomain.CurrentDomain.SetData(“DataDirectory”, newpath)

For customizing the connection string at runtime, please see our team blog at: http://blogs.msdn.com/smartclientdata/archive/2005/07/25/443034.aspx

Where is my data? — Understanding the file copy for desktop projects

One of the things to know when working with local database files is that they are treated as any other content files. For desktop projects, it means that by default, the database file will be copied to the output folder (aka bin) each time the project is built. After F5, here’s what it would look like on disk

      MyProject\Data.mdf

      MyProject\MyApp.vb

      MyProject\Bin\Debug\Data.mdf

      MyProject\Bin\Debug\MyApp.exe

At design-time, MyProject\Data.mdf is used by the data tools. At run-time, the app will be using the database under the output folder. As a result of the copy, many people have the impression that the app did not save the data to the database file. In fact, this is simply because there are two copies of the data file involved. Same applies when looking at the schema/data through the database explorer. The tools are using the copy in the project, not the one in the bin folder.

If this copy behavior is not what you want, there are few ways to work around it:

1. If you select local database file in the Solution Explorer window, you will see a property called ‘Copy to Output’ in the Properties window. By default it is set to ‘Copy Always’ which means that on each build, the data files in the project folder will be copied to the output folder overwriting the existing data files if any. You can set this property to Copy Never and then manually put a copy of the data file in the output folder. This way, on subsequent builds, the project system will leave the datafile in the output folder and not try to overwrite it with the one from the project. The downside is that you still have two copies so after you modify the database file using the app, if you want to work on those changes in the project, you need to copy it to the project manually and vise-versa.

2. You can leave the data file outside the project and create a connection to it in Database Explorer. When the IDE asks you to bring the file into the project, just say no. This way, both the design-time and the run-time will be using the same data file but the downside is that the path in the connection string will be hard coded and therefore it’ll be harder to share the project and deploy the app. Before deploying the app, just make sure to replace the full path in the settings with a relative path.

 

Antoine
Software Design Engineer
Visual Studio Data Design-time

 

Published Friday, August 26, 2005 9:24 PM by SmartClientData Filed under: ,

Advertisements

About arnulfo

veterano del ciberespacio
This entry was posted in Uncategorized and tagged . Bookmark the permalink.

10 Responses to Visual Studio and local databases

  1. shine says:

    Hi…

    Do u know how to connect vc++2005 with Oracle9i?If u know about this please send a reply or tutorial to my mail.

  2. arnulfo says:

    to connect to oracle need to install the .net data provider for oracle from
    http://www.microsoft.com/downloads/details.aspx?familyid=4F55D429-17DC-45EA-BFB3-076D1C052524&displaylang=en

    then you can add a connection to you project from the server explorer on visual studio

    add connection

    select oracle database from the data source combobox on the wizard

  3. Idetrorce says:

    very interesting, but I don’t agree with you
    Idetrorce

  4. Alexey says:

    But it’s not working on CompactFramework!!!!!!

    AppDomain.CurrentDomain Have no “SetData” method!!!!!

  5. Richard Clarke says:

    This is a great tutorial. I’d spend two days wondering why my data wasnt updating. Its all working now that I have put the database outside the app! If I could rate this out of 10 I would give it 20!

    Richard

  6. Pingback: 2010 in review | The grokking eagle

  7. Exactly how much time did it require you to publish “Visual Studio and local databases | The grokking eagle”?
    It boasts quite a lot of fine details. Many thanks -Brad

  8. This page definitely has all the info I wanted concerning this subject and didn’t know who to ask.

  9. Helpful information. Lucky me I found your web site by accident, and I’m stunned why this coincidence didn’t happened in advance!
    I bookmarked it.

  10. baner says:

    I really need help, everything works great when im debbugin and even works great when a install the deployed project in my laptop (in which im developing the app), but when i install the app in another pc, cant find database. the is still looking for full path, im usin vs 2010 express.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s