Walkthrough: Adding a SQL Server Compact Database to an Application and Deploying it
[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]
This walkthrough provides step-by-step instructions for incorporating a SQL Server Compact database into a Windows-based application.
This walkthrough illustrates the following tasks:
The first step is to create a Windows-based application project.
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 Customizing Development Settings.
To create a new Windows-based application
On the File menu, choose New, Project.
Name the project SQLCompactWalkthrough.
Choose Windows Application and then choose the OK button. For more information, see Developing Client Applications with the .NET Framework.
The SQLCompactWalkthrough project is created and added to Solution Explorer.
The next step after creating the project is to add the SQL Server Compact database to the project and create a new data source based on tables in the database. The new data source will be used for drag-and-drop data binding from the Data Sources window in the next section.
In this procedure, you will create a data source based on the Customers table in the Northwind sample database by using the Data Source Configuration Wizard. The Data Source Configuration Wizard automatically starts when a SQL Server Compact database file is added to a project.
The Northwind sample database for SQL Server Compact 4.0 is called Northwind.sdf and is located at C:\Program Files\Microsoft SQL Server Compact Edition\v4.0\Samples or at C:\Program Files (x86)\Microsoft SQL Server Compact Edition\v4.0\Samples.
To create a data source
On the Project menu, choose Add Existing Item.
In the Add Existing Item dialog box, browse to the location of the Northwind.sdf file, and then choose the Add button.
In the Add Existing Item dialog box, you have to choose Data Files in the Objects of type list to make the .sdf file visible.
The Data Source Configuration Wizard opens.
On the Choose a Database Model page, select Dataset, and then choose the Next button.
On the Choose Your Database Objects page, expand the Tables node and select the Customers table check box.
Choose the Finish button and the database (Northwind.sdf) file is added to the project, together with a typed dataset (NorthwindDataset).
Now that the data source is created and available in the Data Sources window, you can drag the table onto a Windows Form to create a data-bound grid.
To add a data-bound grid to the Windows Form
From the Data Sources window, drag the Customers node onto Form1.
Choose F5 to run the application.
The form appears with the customer data loaded into the grid.
Assuming the application is complete, you are now ready to deploy it. For this walkthrough, you will use ClickOnce deployment.
Creating a ClickOnce deployment for an application that has a SQL Server Compact database consists of configuring the appropriate publish information in the Project Designer.
Adding a SQL Server Compact database file to a project automatically configures the appropriate publish information for ClickOnce deployment. However, in this walkthrough you will inspect and verify each setting to understand how deployment works.
To configure ClickOnce deployment for an application that has a SQL Server Compact database
To open the Project Designer, in Solution Explorer, choose My Project if you are working on a Visual Basic project, or Properties if you are working on a C# project.
On the Publish tab, choose the Application Files button, and then set the Publish Status of Northwind.sdf to Data File (Auto).
This setting notifies the installer to put this file in the Data Directory.
Choose the OK button.
Choose Prerequisites and then select the SQL Server Compact check box.
This setting notifies the installer to check whether the SQL Server Compact runtime exists, and to install it from the Internet if it is not found.
Now that the publish information is configured to deploy the SQL Server Compact database together with the application, it is time to create the installer.
To create the installer
In the Publishing Location area, choose the ellipsis button (...) to open the Open Web Site dialog box.
Select File System.
Create a new folder to deploy the installer to, and then select it.
You can create the folder anywhere. Just remember where it is so that you can locate it later.
Choose the Open button.
Choose Publish Now to create the installer.