SQLServerCE(SQLCE)是一款运行在WindowsCE设备环境下的轻量级本地数据库引擎,为移动设备上偶尔连接的数据访问提供了解决方案。例如,工商管理人员在执行巡查任务前,可以用WinCE移动设备把台式计算机中的数据库(SQLServer)拷到WinCE的数据库(SQLCE)上;在执行任务时,可以在移动设备上修改记录;完成任务后,可以把移动设备上 本文来自文秘114 http://www.wenmi114.com,转载请保留此标记。 的数据库上传到台式计算机中的数据库,使这两个数据库之前的数据保持同步。RemoteDataAccess(RDA)方式为WinCE移动设备上SQLCE与SQLServer数据库之间的同步提供了快速而简便的解决方案。本文将通过一个实例演示如何使用RDA方式同步数据库。
2开发环境
SQLServer2000必须安装SP3补丁,SQLServerMobileEdition2005的安装文件在MicrosoftVisualStudio8/SmartDevices/SDK/SQLServer/Mobile/v3.0目录下,文件名为Sqlce30setupcn.msi。安装SQLServerMobileEdition2005后,使用该程序菜单中的“配置Web同步向导”创建一个别名为“sqlce3”的虚拟目录,访问方式为“匿名”。注意:创建虚拟目录后,还必须打开IIS,把虚拟目录“sqlce3”的访问权限增加“写入”权限,否则RDA将同步失败
3程序的实现
// The database file.
private string strDBFile = @"/NandFlash/Northwind.sdf";
// The local connection string.
private string strConnLocal = "Data Source=" + @"/NandFlash/Northwind.sdf";
// The remote connection string.
private string strConnRemote = "Provider=sqloledb; "
+ @"Data Source=CXI;"
+ "Initial Catalog=Northwind; "
+ "User Id=sa;Password=123";
// The URL
private string strURL = "http://192.168.0.112/sqlce3/sqlcesa30.dll";
private void buttonPull_Click(object sender, EventArgs e)
{
// Initialize RDA Object
//
SqlCeRemoteDataAccess rda = null;
try
{
// Try the Pull Operation
//
rda = new SqlCeRemoteDataAccess(strURL,strConnLocal);
rda.Pull("TestTable", "SELECT ID,Name FROM TestTable", strConnRemote,
RdaTrackOption.TrackingOnWithIndexes, "ErrorTable");
// or, try one of these overloads:
//
// rda.Pull("Employees", "SELECT * FROM DimEmployee", rdaOleDbConnectString,
// RdaTrackOption.TrackingOnWithIndexes);..................................................
//
// rda.Pull("Employees", "SELECT * FROM DimEmployee", rdaOleDbConnectString);
}
catch (SqlCeException ex)
{
// Handle errors here
MessageBox.Show(ex.ToString());
}
finally
{
// Dispose of the RDA object
//
rda.Dispose();
}
}