How to: Use SQL Server 2005 Compact Edition 3.1 in Managed Projects (Devices)
This topic explains how to use SQL Server 2005 Compact Edition 3.1 in managed smart device projects. Developers sometimes use this version of SQL Server because it is built into the ROM of Windows Mobile 6 devices and emulators.
Important Note: |
---|
You must complete Walkthrough: Creating Windows Forms Applications for a Device, before you begin. |
To add a reference to a managed data provider
Download and install the Microsoft SQL Server 2005 Compact Edition Developer Software Development Kit.
Open the device project that you created by completing Walkthrough: Creating Windows Forms Applications for a Device.
In Solution Explorer, right-click the project and then click Add Reference.
The Add Reference dialog box appears.
Click the Browse tab, find the file drive:\Program Files\Microsoft SQL Server Compact Edition\v3.1\SDK\bin\platform\System.Data.SqlServerCe.dll, and then click OK.
(Visual Basic only) Add a reference to System.Data.
To add a SQL Server 2005 Compact Edition 3.1 database to the project
In Solution Explorer, right-click the project, point to Add, and then click Existing Item.
The Add Existing Item dialog box appears.
In the Object of type drop-down, select All Files (*.*).
Find the file drive:\Program Files\Microsoft SQL Server Compact Edition\v3.1\SDK\Samples\Northwind.sdf and then click Add.
Click Cancel in the Upgrade to SQL Server Compact 3.5 Database and Data Source Configuration Wizard dialog boxes.
The database file appears in Solution Explorer.
To connect to the database
From the Toolbox, drag a Button control onto the form.
If you cannot see the Toolbox in the integrated development environment (IDE), click Toolbox on the View menu. If you cannot see the Device Controls tab in the Toolbox, right-click the Toolbox, and then click Show All.
Right-click the Button on the form, and then click Properties.
In the Properties window, type Connect, and press ENTER to set the Text property.
Double-click the Button on the form.
The Code Editor opens with the cursor positioned in the event handler.
Insert the following Visual Basic code:
Dim conn As New System.Data.SqlServerCe.SqlCeConnection("data source='\Program Files\DeviceSample\Northwind.sdf';") conn.Open()
—or—
Insert the following C# code:
System.Data.SqlServerCe.SqlCeConnection conn = new System.Data.SqlServerCe.SqlCeConnection("data source='\\Program Files\\DeviceSample\\Northwind.sdf';"); conn.Open();
Note
You may need to modify the connection string, depending on the deployment folder of the project.
When you debug or deploy this application, Visual Studio copies the Northwind.sdf sample database and deploys the application to the device. The application connects to the database but does not perform any operations.
See Also
Tasks
How to: Upgrade Managed Projects to SQL Server Compact 3.5 (Devices)
Concepts
Using SQL Server 2005 Compact Edition 3.1 Databases (Devices)