上一篇总结了WinCE通过WebService进行数据通讯的交互方式,今天整理个SqlCE数据同步方式的内容。先说下软件环境:终端平台使用WinCE5.0+SqlCE2.0,服务器使用Windows server 2003+Sql Server2000 sp4,Dot Net Framwork 使用的是2.0版本。

SQL Server CE与SQL Server之间通过RDA合并复制进行数据同步。Remote data access(RDA)主要由三部分组成:SQL Server CE Database Engine、SQL Server CE Client Agent和SQL Server CE DataBase Agent。SQL Server CE数据库引擎负责写入和读取SQL Server CE数据库中的数据;SQL Server CE客户端代理是RDA在移动设备上的主要组件,它实现了RDA的主要功能,我们可以通过程序调用它提供的接口以控制RDA;SQL Server CE服务器端代理位于服务器端,它与SQL Server CE客户端代理通过HTTP协议进行通信,接收并处理SQL Server CE客户端代理的命令。其通讯原理结构如下图:

数据同步的环境配置工作也分为:终端SqlCE配置、IIS代理配置。

终端SqlCE配置:安装终端与PC的同步程序MicrosoftActiveSync,找到VS2005安装目录D:\Program Files\Microsoft Visual Studio 8\SmartDevices\SDK\SQL Server\Mobile\v2.0\wce400\armv4下的sqlce20.dev.ppc.wce4.armv4.CAB和sqlce20.ppc.wce4.armv4.CAB,拷贝至终端WinCE系统中,在终端安装这两个包,安装后在终端的Windows目录下将增加了三个文件:Ssce20.dll、Ssceca20.dll和Sscemw20.dll。在Windows目录下增加了一个目录SqlCE 2.0,其中的isqlw20.exe即是在WinCE下运行的类似SqlServer2000的查询分析器程序,也可以在烧制WinCE操作系统时把SqlCE烧制进系统中。

IIS代理配置:IIS代理服务器若要进行GPRS通讯,最好能有静态IP。代理服务器上先安装好IIS和SqlServer2000,在IIS代理服务器上安装Sql Server CE2.0,安装过程中会出现配置选项

这里输入虚拟目录的别名和SqlCE的安装目录,我输入的虚拟目录名是SqlCE20,因为我还配置了一个SqlCE3.0的版本 :-) 继续

选择匿名就可以了,安全要求高的可以设置密码访问,做个匿名的先。完成安装后,在IE上输入http://localhost/sqlCE20/sscesa20.dll ,若显示“SQL Server CE Server Agent”则说明安装配置IIS成功。这里安装要注意的问题是sqlServer2000的补丁要和sqlCE2.0的补丁版本一致。

开始编写代码,新建项目智能设备Windows CE5.0应用程序,添加sqlCE2.0引用,注意VS2005默认的sqlce引用是3.0版本的,所以这里要添加浏览引用,位置是D:\Program Files\Microsoft Visual Studio 8\SmartDevices\SDK\SQL Server\Mobile\v2.0\System.Data.SqlServerCe.dll。添加代码引用:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Data.SqlServerCe;
using System.Reflection; 设置数据连接及相关连接属性 private string AppPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase.ToString());//程序运行目录
private SqlCeCommand cmd = new SqlCeCommand();
private SqlCeConnection con = new SqlCeConnection();
private string dataBaseName = "test.sdf";
private string dbFile = string.Empty;
private string internetURL = "http://192.168.1.187/sqlce20/sscesa20.dll; //IIS代理服务器
private string remoteConStr = "Provider=sqloledb;Data Source=(local);Initial Catalog=test;User Id=sa; Password=sa;";//IIS代理服务器上能访问到的数据库配置
private SqlCeEngine se = new SqlCeEngine();
private SqlCeRemoteDataAccess rda = new SqlCeRemoteDataAccess(); 窗体加载项 private void Form1_Load(object sender, EventArgs e)
{
this.dbFile = this.AppPath + @"\" + this.dataBaseName;//数据库
string str = "DataSource=" + this.dbFile;
this.con.ConnectionString = str;//终端数据库配置
this.rda.InternetLogin = "";//访问IIS代理服务器的用户
this.rda.InternetPassword = "";//访问IIS代理服务器的密码
this.rda.InternetUrl = this.internetURL;//服务器数据库配置
this.rda.LocalConnectionString = str;//终端数据库配置
} 下载 private void btnPull_Click(object sender, EventArgs e)
{
try
{
this.ExecuteSql("drop table gdinfos");//先要删除本地数据表如果存在的话,否则会报错
int tickCount = Environment.TickCount;
this.rda.Pull("gdinfos", "select * from gdinfos", this.remoteConStr, RdaTrackOption.TrackingOnWithIndexes);
int num2 = Environment.TickCount - tickCount;
MessageBox.Show(string.Format("下载成功\n用时{0}毫秒", num2));
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
}
} 上传 private void btnPush_Click(object sender, EventArgs e)
{
try
{
int tickCount = Environment.TickCount;
this.rda.Push("gdinfos", this.remoteConStr);
int num2 = Environment.TickCount - tickCount;
MessageBox.Show(string.Format("上传成功\n用时{0}毫秒", num2));
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
}
}

最新文章

  1. SQLServer 游标 (A)
  2. Linux编程(3) MakeFile
  3. 3D Touch集成过程整理
  4. 解决关于打开plist文件乱码问题,plist转换为xml文件的txt文件!
  5. Coding.net 代码管理快速入门
  6. 你必须知道的ADO.NET
  7. [hankerrank]Counter game
  8. Delphi 使用串口模拟工具进行串口程序开发调试
  9. HTTP和HTTPS详解
  10. PHP学习笔记十四【面向对象】
  11. Unity3d 打包时报错 CommandInvokationFailure: Unable to convert classes into dex format. See the Console for details.
  12. sql server中关于批处理与脚本的简单介绍
  13. Vmware虚拟机安装win7系统教程
  14. LeetCode-391. 完美矩形(使用C语言编译,详解)
  15. Sql Server2014数据库清理日志
  16. Winform 加载datagridview
  17. Android录制音频的三种方式
  18. Linux连不上校园网怎么办?
  19. isA,小赋值大
  20. libevent简述

热门文章

  1. SpringBoot-基于Maven工程使用SpringBoot
  2. Sails 框架学习资料
  3. linux内核介绍
  4. SQL Server 2008 sa用户可以登录,Windows身份验证无法登录
  5. 细说PHP的FPM
  6. oracle 查看隐藏参数
  7. HALCON里面的一维测量。
  8. 网页图片jpg,gif,png对比。
  9. kafka的javaapi生产者生产消息,消费者获取不到
  10. Hive的静态分区和动态分区