Microsoft Corporation
January 2007
Applies to:
Microsoft Visual Studio 2005
Microsoft SQL Server 2005 Compact Edition
Summary: In this tutorial, you will learn about two different deployment scenarios available for deploying SQL Server 2005 Compact Edition with your applications: centrally installing SQL Server 2005 Compact Edition and privately installing SQL Server 2005 Compact Edition as part of your application. You will be using Visual C#.NET in this tutorial. There is another tutorial that shows how to use Visual Basic.NET to perform the same tasks. (22 printed pages)
Introduction
Tutorial Objective
Conclusion
The following applications are required to run this tutorial:
This tutorial requires Visual Studio 2005 Standard, Professional, or Team System Edition. It will not work with any of the Express Editions. If you do not have the correct edition of Visual Studio 2005, find out how you can acquire it from the Visual Studio 2005 Developer Center.
None
200
20 minutes
In this tutorial, you will learn about two different deployment scenarios available for deploying SQL Server 2005 Compact Edition with your applications: centrally installing SQL Server 2005 Compact Edition and privately installing SQL Server 2005 Compact Edition as part of your application.
When you centrally install SQL Server 2005 Compact Edition, all applications that use SQL Server 2005 Compact Edition use the same SQL Server 2005 Compact Edition files. The centrally installed SQL Server 2005 Compact Edition files are automatically updated and serviced by Microsoft Update. However, any user that performs the application installation must have administrative rights.
When you privately install SQL Server 2005 Compact Edition, as part of your application, the SQL Server 2005 Compact Edition files are installed directly into the application directory. The privately installed SQL Server 2005 Compact Edition files do not affect any other applications; therefore, the application installation can be performed by virtually any user because administrative rights are not required. However, updating and servicing the privately installed SQL Server 2005 Compact Edition files is the responsibility of the application author because the private SQL Server 2005 Compact Edition files are not updated or serviced by Microsoft Update.
You will be using Visual C#.NET in this tutorial. There is another tutorial that shows how to use Visual Basic.NET to perform the same tasks.
In this tutorial, you will learn about two different options for deploying SQL Server 2005 Compact Edition with your applications: SQL Server 2005 Compact Edition central installation and SQL Server 2005 Compact Edition private application installation. At the completion of this tutorial, you will know how to perform both central installations and private installations of SQL Server 2005 Compact Edition and be able to identify the advantages and disadvantages of each SQL Server 2005 Compact Edition installation technique.
In this tutorial, you will perform the following exercises:
In this exercise, you will learn how to perform application deployment using central (or shared) deployment of SQL Server 2005 Compact Edition. When using a central install of SQL Server 2005 Compact Edition, all applications that use SQL Server 2005 Compact Edition will use the same SQL Server 2005 Compact Edition files.
The main benefits of centrally installing SQL Server 2005 Compact Edition are that all applications on the computer use a common SQL Server 2005 Compact Edition installation and Microsoft Update automatically updates and services the SQL Server 2005 Compact Edition installation. The main concern when centrally installing SQL Server 2005 Compact Edition is that the user performing the application installation must have administrative rights on the computer to successfully complete the install.
Note The project that you use in this tutorial is a slightly modified version of the Visual Studio project created in the tutorial entitled "Updatable Cursor Programming with ResultSet (C#)". The only modifications to the project are the addition of two text boxes located at the bottom of the application form: one displays the location of the application directory and the other displays the location of the data directory.
Visual Studio opens the solution.
Note As listed in the Tutorial Requirements near the beginning of this manual, you must have Visual Studio 2005 Service Pack 1 installed to successfully complete this tutorial.
.gif)
Figure 1. Properties listed in the Solution Explorer
Visual Studio displays the Project Designer, which you use to configure the available project settings. The configuration setting values are grouped into panes, with each pane accessible by way of the tabs listed on the left edge of the Project Designer.
Figure 2. The publication settings pane (Click on the picture for a larger image)
The Prerequisites dialog box is displayed, as shown in Figure 3.
Figure 3. The Prerequisites dialog box
Note If SQL Server 2005 Compact Edition was not already checked when you opened the Prerequisites dialog box, your computer probably doesn't have Visual Studio 2005 Service Pack 1 installed. With Visual Studio 2005 Service Pack 1 installed, Visual Studio automatically makes SQL Server 2005 Compact Edition a prerequisite to the project when a SQL Server Compact Edition database file is added to the project. In the case of this project, Visual Studio made SQL Server 2005 Compact Edition a project prerequisite when the Northwind.sdf file was added to the project. If you do not have Visual Studio 2005 Service Pack 1 installed, you might encounter difficulty in completing this tutorial.
Note Choosing Download prerequisites from the same location as my application places the setup files for SQL Server Compact Edition on your Web site in addition to the setup files for your application. At the time that this tutorial is being written you must choose to download the SQL Server Compact Edition files from the same location as your application because SQL Server Compact Edition is not yet a released product; therefore, the SQL Server Compact Edition setup files referenced by ClickOnce are not yet available from the vendor's (Microsoft) Web site. If you are doing this tutorial after the public release of SQL Server Compact Edition, you might want to try selecting Download prerequisites from the component vendor's Web site. Configuring the ClickOnce package to download SQL Server Compact Edition from the vendor's Web site reduces the download burden on your Web site and ensures that the user gets the most up-to-date version of SQL Server Compact Edition.
Visual Studio displays a window containing the code associated with the form.
_conn = new SqlCeConnection(@"Data Source = |DataDirectory|\Northwind.sdf");
At run time, the DataDirectory substitution string is replaced with the appropriate directory path for the application's current execution environment. Without the DataDirectory substitution string, your application would have to programmatically determine the location of the database file based on the current execution environment and then dynamically build the path used in the connection string.
.gif)
Figure 4. Selecting Publish on the SSCEDeploymentCSharp context menu
The Publish Wizard appears.
.gif)
Figure 5. Specifying the location to publish the application
After the wizard builds and publishes the application and verifies the publication, the wizard opens Internet Explorer with the Web page that it has published, as shown in Figure 6. It might take a few moments before the Web page is displayed.
.gif)
Figure 6. The generated publication to be used to install the SSCEDeploymentCSharp application
Notice the two links on the generated page. The Launch link launches the application without performing any prerequisites checks. The Install link installs any of the prerequisites that are not already installed and then installs and launches the application. You are now ready to test installing your application.
For the purposes of this tutorial, if you already have SQL Server 2005 Compact Edition installed on your computer, you will uninstall it. With SQL Server Compact Edition removed from your computer, you can test the ClickOnce installation behavior as an end user who does not have SQL Server 2005 Compact Edition already installed experiences the ClickOnce installation.
This is necessary because Visual Studio is using a reference to SQL Server 2005 Compact Edition and will therefore interfere with the SQL Server Compact Edition uninstall.
You have successfully removed SQL Server Compact Edition from your computer.
.gif)
Figure 7. The File Download security warning
.gif)
Figure 8. The Setup security warning
The setup program executes and as part of the prerequisites check determines that SQL Server 2005 Compact Edition is not installed. The setup program then displays an installation dialog box, as shown in Figure 9. The setup program automatically combines the End User License Agreement (EULA) for each of the required prerequisites and displays the combined EULA on the setup dialog box. In this exercise, the setup dialog box displays only the SQL Server Compact Edition EULA because SQL Server Compact Edition is the only prerequisite that the setup program is installing.
.gif)
Figure 9. SSCEDeploymentCSharp Setup, indicating that the SQL Server 2005 Compact Edition prerequisite is about to be installed
The SSCEDeploymentCSharp Setup installs SQL Server 2005 Compact Edition.
.gif)
Figure 10. The Application Install security warning
After installation, the application appears, as shown in Figure 11.
.gif)
Figure 11. The deployed application
The application is now fully installed on your local computer. In addition to installing your application, the ClickOnce installation process has centrally installed SQL Server Compact Edition; therefore, SQL Server Compact Edition is available for use by all applications on your computer.
All aspects of your program are now locally installed on your computer including the SQL Server Compact Edition database file. All changes that you make to the data in the application are stored to the local database. Notice that your program is now available under Start | All Programs | Microsoft | SSCEDeploymentCSharp. You can exit the application then launch it from the Start menu and see that any changes that you make to the application data are maintained between program executions. You can also use Add or Remove Programs in the Windows Control Panel to uninstall the application.
The Application Directory and Data Directory values at the bottom of the application form show the current locations of the application directory and data directory. You'll recall that when you ran the program directly from the executable file created by Visual Studio that the SQL Server Compact Edition database file was located in the same directory as the application. When you install the application by way of ClickOnce, the application directory and data directory are now in different locations. This change in the location of the SQL Server Compact Edition database file relative to the application directory is why it is crucial to use the DataDirectory substitution string when specifying the path to the data source instead of using a fixed path.
In this exercise, you will learn how to perform application deployment using private (or local) deployment of SQL Server 2005 Compact Edition. When using a private install, the SQL Server 2005 Compact Edition files are deployed with the application and placed in the application directory. A private SQL Server 2005 Compact Edition installation makes SQL Server 2005 Compact Edition only available to the program with which it is installed; no other programs are affected by the installation.
The main benefits of privately installing SQL Server 2005 Compact Edition are that the user performing the application installation can do so without having administrative rights and when privately installing SQL Server 2005 Compact Edition, the application author has control over the updates applied to the SQL Server 2005 Compact Edition files used by the application. The main concern when privately installing SQL Server 2005 Compact Edition is that the application author assumes the responsibility of updating and servicing the SQL Server 2005 Compact Edition files; the privately installed SQL Server 2005 Compact Edition files are not updated or serviced by Microsoft Update.
Visual Studio opens the project you used in Exercise 1.
.gif)
Figure 12. Properties listed in the Solution Explorer
Visual Studio displays the project designer tabs to allow you to configure project settings.
Figure 13. The publication settings pane (Click on the picture for a larger image)
The Prerequisites dialog box is displayed.
You remove SQL Server Compact Edition as a project prerequisite because you are copying the SQL Server Compact Edition files as part of your application. With SQL Server Compact Edition marked as a prerequisite, the ClickOnce setup utility will perform a central install of SQL Server Compact Edition on the target machine as it did in Exercise 1 of this tutorial.
.gif)
Figure 14. The Prerequisites dialog box
To privately install the SQL Server Compact Edition with your application, you must include the SQL Server Compact Edition DLL files with your application and copy the DLL files to the application directory.
.gif)
Figure 15. Selecting SQL Server 2005 Compact Edition DLLs
The selected DLL files are added to the project and are now visible in the Solution Explorer pane, as shown in Figure 16.
.gif)
Figure 16. SQL Server 2005 Compact Edition DLLs in the Solution Explorer
With the Copy to Output Directory property set to Copy if newer, Visual Studio copies the SQL Server Compact Edition DLL files to the application directory when building or publishing the project. Visual Studio copies the SQL Server Compact Edition DLL files to the application directory if the DLL files do not exist in the application directory or if the SQL Server Compact Edition DLL files in the project directory are newer than the existing DLL files in the application directory.
With the Copy Local property set to True, Visual Studio copies the System.Data.SqlServerCe assembly to the application directory when building or publishing the project. When you centrally install SQL Server Compact Edition as you did in Exercise 1, the installation process installs the System.Data.SqlServerCe assembly in the GAC. In the case of a private SQL Server Compact Edition installation, the System.Data.SqlServerCe assembly might not be available from the GAC; therefore, your application needs the assembly to be in the application directory. When you privately install SQL Server Compact Edition with your application, the application always uses the copy of the System.Data.SqlServerCe assembly in the application directory even if SQL Server Compact Edition is centrally installed on the computer running your application because the .NET assembly loader always prefers a local copy of an assembly over a copy installed in the GAC.
Notice that Visual Studio copies all of the specified SQL Server Compact Edition DLL files, including the System.Data.SqlServerCe assembly, to the output directory, as shown in Figure 17.
Note If you do not see the DLL files and the assembly listed under the bin\Debug directories, refresh the Solution Explorer view by either clicking the Refresh button on the Solution Explorer or selecting View | Refresh from the Visual Studio menu.
.gif)
Figure 17. Viewing output files in the Solution Explorer pane
The Application Files dialog box is displayed, as shown in Figure 18. The ApplicationFiles dialog box lists all of the files that are installed with your application including whether each file is installed in the application directory or the data directory.
.gif)
Figure 18. The Application Files dialog box, displaying a list of files to be installed with the application
.gif)
Figure 19. Clicking Publish on the SSCEDeploymentCSharp context menu
.gif)
Figure 20. Specifying the location to publish the application
After the wizard builds and publishes the application, the wizard opens Internet Explorer displaying the application installation Web page, as shown in Figure 21. It might take a few moments before the Web page is displayed.
.gif)
Figure 21. The generated publication to be used to install the SSCEDeploymentCSharp application
Notice that the application installation page that Visual Studio generates in this case is simpler than the application installation page from Exercise 1 of this tutorial. By converting the application to install SQL Server Compact Edition locally rather then installing SQL Server Compact Edition centrally, you have eliminated the need for application prerequisite installs; therefore, the application installation page need only provide a way to install the actual application.
After installation, the application appears, as shown in Figure 22.
.gif)
Figure 22. The deployed application
The application is now fully installed on your local computer and is completely self-contained with all of the SQL Server Compact Edition DLL files and assemblies located in the application directory. The ClickOnce private installation of SQL Server Compact Edition makes SQL Server Compact Edition fully available to your application but does not centrally install SQL Server Compact Edition on the local computer and does not impact any other applications on the local computer. By configuring the application installation to privately install SQL Server Compact Edition, any user can install the application because the user performing the install can do so without being required to have administrator rights.
Other then installing the SQL Server Compact Edition DLL files and assembly in the application directory, all aspects of your program are identical to when you performed the ClickOnce install in Exercise 1 of this tutorial. The ClickOnce install installs the SQL Server Compact Edition database file on the local computer in the appropriate data directory and all changes that you make to the data in the application are stored to the local database. Just as in Exercise 1, your program is now available under Start | All Programs | Microsoft | SSCEDeploymentCSharp and you can exit the application then launch it from the Start menu and see that any changes that you make to the application data are maintained between program executions. You can also use Add or Remove Programs in the Windows Control Panel to uninstall the application.
In this window, you can see that the application, the SQL Server Compact Edition DLL files, and the System.Data.SqlServerCe assembly are installed into the application directory.
.gif)
Figure 23. The contents of the application directory
.gif)
Figure 24. The contents of the data directory
In this tutorial, you performed the following exercises:
In this tutorial, you learned about two different deployment scenarios available for SQL Server 2005 Compact Edition applications. You learned how to perform both a central installation and a private installation of SQL Server 2005 Compact Edition with your application.