Udostępnij za pośrednictwem


How to: Manage Local Data Files in Your Project

A local database file can be included as a file in a project. The first time you connect your application to a local database file, you can choose between creating a copy of the database in your project or connecting to the existing database file in its current location. If you choose to connect to the existing file, then a connection is created just as if you were connecting to any remote database, and the database file is left in its original location. If you choose to copy the database into your project, Visual Studio creates a copy of the database file, adds it to your project, and modifies the connection so that it now points to the database in your project as opposed to the original location of the database file.

Note

Existing data connections in Server Explorer/Database Explorer are modified to point to the database file in the project as well (the database file in the project's root folder).

When you build a project, the database file might be copied from the root project folder into the output (bin) folder. (Select Show All Files in Solution Explorer to view the bin folder.) This behavior is due to the Copy to Output Directory property of the file. The default setting of the Copy to Output Directory property is dependant on the type of database file you are using.

Note

The behavior of the Copy to Output Directory property does not apply to Web or C++ projects.

The database file in your root project folder is changed only when you edit the database schema or data by using Server Explorer/Database Explorer or other Visual Database Tools.

The database file behavior is dependent on the Copy to Output Directory property setting, which is detailed in the following table.

During application development, any changes made to the data (during run time within your application) are being made to the database in the bin folder. For example, when you press F5 to debug your application, you are connected to the database in the bin folder.

Copy to Output Directory setting

Behavior

Copy if newer (default for .sdf files)

The database file is copied from the project directory to the bin directory the first time the project is built. Every subsequent time you build the project, the Date Modified property of the files is compared. If the file in the project folder is newer, it is copied to the bin folder, replacing the file that is currently there. If the file in the bin folder is newer, no files are copied. This setting persists any changes made to the data during run time, meaning that every time you run your application and save changes to the data, those changes are visible the next time you run your application.

Caution noteCaution
This option is not recommended for .mdb or .mdf files. The database file can change even when no changes are made to the data. Simply opening a connection (for example, expanding the Tables node in Server Explorer) on a data file can mark it as newer. Because of this unpredictable behavior, we recommended that you do not use this option for .mdb or .mdf files.

Copy always (default for .mdf and .mdb files)

The database file is copied from the project directory to the bin directory every time you build your application. Every time you build your application and save changes to the data, those changes are overwritten when the original file is copied to the bin directory, replacing the copy that you just changed. You do not see the updated data the next time you run your application. Any changes made to the data file in the output folder will be overwritten the next time you run the application.

Do not copy

The file is never copied or overwritten by the project system. Because your application creates a dynamic connection string that points to the database file in the output directory, this setting works only for local database files when you manually copy the file yourself. You must manually copy the file to the output directory after setting to Do not copy.

Procedure

To respond to the Local database file dialog box

  • Click Yes if you want Visual Studio to copy the database file into your project and modify the connection to point to the copy in your project. For more information on working with database files in your project, see Local Data Overview.

  • Click No if you do not want Visual Studio to copy the database file into your project. Instead, the connection points to the file in the original location and the database file is not added as a file to the project.

See Also

Tasks

Walkthrough: Connecting to Data in a SQL Server Express Database (Windows Forms)

Walkthrough: Connecting to Data in an Access Database (Windows Forms)

Walkthrough: Creating a SQL Server Compact 3.5 Database

Other Resources

Using SQL Server Compact 3.5 (Visual Studio)