BUILDING A MANAGED CODE COMPACT DATABASE APPLICATION

With SQL Compact you again use the same application as previously used. Here you change the filename used to have an .sdf extension and change the read and write functions to use the database. The WriteData function creates the database file if it doesn’t already exist, so there is no need to create the file in preparation. The code is essentially the same except the classes are for SQL Compact and the connection is to the database file.

1. Remove the System.Data.SqlServer reference and add a reference to System.Data.SqlServerCe as in Figure 20-6 or via the Add Reference .NET tab.

2. Comment out the SQL Remote using reference at the top of the program and uncomment the SQL Compact using reference.

3. Remove the SQL connection string code.

Writing Tracks to a Compact Database File

In this function you do the following:

a. Create a connection to the database.

b. If the database file doesn’t exist, create it and add the Tracks table.

c. Clear any tracks from the table.

d. Write each track in the Tracks list to the table.

Note again the importance of capturing errors with the database actions.

1. Replace the WriteData function in the PlaylistConsole application with the following (or copy it from compactsqlReadWrite.cs.txt) It includes the database filename:

image
public static string dbFilename = "\MyMusic.sdf"; public static void ...

Get Professional Windows® Embedded Compact 7 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.