• WinCE数据通讯之SqlCE数据同步篇


    上一篇总结了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);
               }
           }

  • 相关阅读:
    百度文库文档下载分析
    旅游公司招聘Java工程师
    C# 在Repeater 的ItemDataBound 如何转换e.Item.DataItem 的类型
    IOS多线程GCD
    vDSP加速的应用
    ios获取内核数目
    Struts06---通配符的使用
    Struts05---动态查询
    Struts04---命名空间的查询顺序以及默认执行的Action
    Struts03---参数传递
  • 原文地址:https://www.cnblogs.com/jshchg/p/2400095.html
Copyright © 2020-2023  润新知