Share via


How to: Add a SQL Server Compact Database to a Project

There are two ways to add a SQL Server Compact database to a Visual Studio project. You can add an existing database to a project, or you can create a new database by using Visual Database Tools and then include it in a project.

Creating a New SQL Server Compact Database

You create new databases using the Add Connection dialog box accessible through Server Explorer/Database Explorer.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings.

To create a new SQL Server Compact database

  1. To open Server Explorer/Database Explorer, on the View menu, click Server Explorer/Database Explorer.

  2. Right-click the Data Connections node and click Add Connection.

  3. If the Add Connection dialog box opens, verify that the Data Source is Microsoft SQL Server Compact (.NET Framework Data Provider for SQL Server Compact). If it is not, click the Change button, click the Microsoft SQL Server Compact data source, and click OK.

  4. Leave the default selection of My Computer in the Data Source area.

  5. Click the Create button in the Connection Properties area.

  6. In the Create New SQL Server Compact Database dialog box, provide the file path, database file name, and password of the new database. You can also choose to encrypt the data.

  7. Click OK to return to the Add Connection dialog box. Note that the Database box now contains the path to the new database.

    Note

    At this point the database has been created. Clicking Cancel in the dialog box will not delete the newly created file.

  8. Click Test Connection to verify connectivity to the new database.

  9. Click OK to create the data connection in Server Explorer/Database Explorer.

    Now you can expand the connection and right-click the Tables node to start creating new tables and designing the schema that defines the database.

  10. To add this database to your project, follow the directions for adding existing databases below.

Adding an Existing Database to a Project

You can use the Add Existing Item dialog box to browse to an existing SQL Server Compact database file (.sdf) and add it to a project. You need to have an existing project loaded into the integrated development environment (IDE). After you add the database to your project, the Data Source Configuration Wizard automatically opens. You can then select the database objects (only tables in the case of SQL Server Compact) to add to the generated dataset that the wizard creates. After you complete the wizard, the tables are added to the Data Sources window, ready to be dropped onto forms to create data-bound controls.

To add an existing SQL Server Compact database to a project

  1. On the Project menu, click Add Existing Item.

  2. In the Add Existing Item dialog box, browse to the location of the SQL Server Compact database file (.sdf) you want to use in the project, and then click Add.

    After you add the database file, the Data Source Configuration Wizard opens to the Choose your Database Objects page, and you can select the tables to use in the application.

    Note

    As an alternative to using the Add Existing Item dialog box, you can drag the .sdf file from File Explorer and drop it into your project in Solution Explorer.

  3. Expand the Tables node and select the tables you want to use in your application.

  4. Click Finish. The database is added to the project along with a typed dataset that includes the tables selected in the previous step. Additionally, the dataset appears in the Data Sources Window.

See Also

Tasks

How to: Deploy a SQL Server Compact 4.0 Database with an Application

Walkthrough: Adding a SQL Server Compact Database to an Application and Deploying it

Concepts

SQL Server Compact 4.0 and Visual Studio