Windows Mobile 6.5 Database Connection and Data Access


In this article we will be talking about a major subject for mobile application development : Database Connection

If you want to develop Windows Mobile applications you should install Visual Studio 2008 and these SDKs:
After installing these SDKs we open Visual Studio 2008 and we choose File->New->Project->Smart Device->Smart Device Project.

1.gif
 
After that we see the screen below and we choose

from Target Platforms-> Windows Mobile 6 Professional SDK 
from .NET Compact Framework version->3.5 
from Templates-> Device Application as you can see.

2.gif
 
After making these settings we see our emulator on the screen and everything is ready to start coding.

3.gif
 
To develop database applications in mobile devices we need to install SQL Compact Edition (SQLCE).We installed it when we install our SDKs at the beginning. So we can add a database to our project and try to get data from database.

To see Server Explorer in our project we choose Server Explorer from View menu. In Solution Explorer we right click to project and choose Add->New Item.We choose from Data tab Database File.

4.gif
 
Now it is time to create a new table. It is exactly same as the SQL Server, we just right click on Tables under Databases and choose Create New Table.

5.gif
 
We save our table as tbl1 and as you can see below we create two column named id and name. We mark id as primary key.

6.gif 

Now, to see our table's data we right click on tbl1 and choose show table data.

7.gif

As you guess our table is empty because we did not add anything to table.We add our data in table save it. 

8.gif
 
Now it is time to acces database on our project and get the data. To do this first we have to add SqlServerCe to our project as a reference. We right click to our project and choose Add Reference. We choose System.Data.SqlServerCe from reference list as shown below. From now on Visual Studio will recognize SqlCe commands in our project.

9.gif
 
We turn back to our application and drag a textbox and a buton to our form.Our scenario is that : We want to see on the textbox the name of the person whose id=1.So we expect to see on textbox "duygu".

10.gif
 
We open applications code behind and add namespace System.Data.SqlServerCe.

After that we double click the buton and write these codes:

11.gif
 
First 3 lines as you can guess we create our connection string. On 4th line we create our connection and 5th line we write the query for our scenario. 7th line we run our command cmd and get data from database and show it on textbox. As you can see all steps are same as SQL Server. Now we can run our project we pres F5 and see a screen like that:

12.gif
 
We make settings as above and click deploy. It could take some time to run we should wait and finally we can see our application as shown below.

13.gif
 
We just click the buton and see the result of our application.

14.gif
 
Also we see the information we want. 

We talked about connecting database and getting data from database on Mobile applications and we saw there is no difference between Sql Server and SQL Compact Edition on these steps.

You can download project source code.


Similar Articles