Development Environment
Visual Studio 2005 SP1
SQL Server 2005 Enterprise Edition
Windws Mobile 6.0 professional SDK and Chinese Image
ActiveSync 4.5
Target Platform
Windws Mobile 6.0 professional
About Databases
Windows Mobile devices can directly access the SQL Server server over the network. However, due to network environment, application performance, and other reasons, devices generally do not directly access the SQL Server server, you can deploy SQL Server mobile (SQL Server CE) on the device to directly operate the database on the device. To perform remote data operations, You Can remot dataaccess (pull and push) and merge and copy to synchronize data. This is why data operations can be performed on devices offline. Data Synchronization will be discussed later.
Demo
This demo has two forms: one is to access the SQL Server mobile database on the device, and the other is to access the SQL Server database on the server for simple data query.
Create a project
Create an SQL Server mobile database, right-click the project name/Add/new item/database file in solution, and a new SDF database file will be created in the project.
In server resource management, create a table and enter data.
Design view: Put A DataGrid in the form, two buttons, one page switch, and one exit.
BackendCode
Code
Public Partial Class Form1: Form
{
Public Form1 ()
{
Initializecomponent ();
}
Private Void Form1_load ( Object Sender, eventargs E)
{
String Spath = System. Io. Path. getdirectoryname (system. reflection. Assembly. getexecutingassembly (). getname (). codebase. tostring ());
Sqlceconnection con = New Sqlceconnection ( " Data Source = " + Spath + " \ Appdatabase1.sdf " );
Dataset DS = New Dataset ();
Sqlcedataadapter da = New Sqlcedataadapter ( " Select * From ebook " , Con );
Da. Fill (DS );
Datagrid1.datasource = DS. Tables [ 0 ];
}
Private Void Menuitem2_click ( Object Sender, eventargs E)
{
This. Close ();
}
Private Void Menuitem1_click ( Object Sender, eventargs E)
{
SQL= NewSQL ();
SQL. Show ();
This. Hide ();
}
}
Generate a project, connect to the simulator, select the target platform, and click "connect to device"
CTRL + F5 run. The deployment dialog box is displayed. Select the target platform and click "deploy"
ProgramStart deployment and run. The result is as follows:
You can see the deployed file in the device's resource manager. In Program Files \ Project name \ an EXE file and An SDF file. If you exit, you can run the EXE file in the resource manager to start the program again, but if you change the code, re-deploy and execute it.
Create SQL. CS on the second page
Code
Public Partial Class SQL: Form
{
Public SQL ()
{
Initializecomponent ();
}
Private Void Menuitem2_click ( Object Sender, eventargs E)
{
This. Close ();
}
Private Void Menuitem1_click ( Object Sender, eventargs E)
{
Form1 F1= NewForm1 ();
F1.show ();
This. Hide ();
}
Private Void SQL _load ( Object Sender, eventargs E)
{
Datatable dt = New Datatable ();
Sqlconnection con = New Sqlconnection ( " Server = 192.168.1.81; database = ez4phonedb; uid = sa; Pwd = 123; " );
Sqldataadapter da = New Sqldataadapter ( " Select * From ressort " , Con );
Da. Fill (DT );
Datagrid1.datasource = DT;
}
}
Note: The preceding server address must be a real network IP address. It cannot be localhost or 127.0.0.1.
Generate and connect to the simulator. Because you want to access the server, the device must connect to the computer and above through ActiveSync. Tools/set the simulation Manager/
In this case, ActiveSync is activated because it only connects to the network for testing and does not perform data synchronization. Click Cancel.
After the above settings, if the simulator cannot access the Internet (use Internet Explorer to test), you may need to set a network connection.
Start/set/connection/advanced/select Network/select unit settings
Run the program. The result is as follows. The data comes from the SQL Server server.