65.9K
CodeProject 正在变化。 阅读更多。
Home

通过 RDA 使用 SQL Server CE 移动版与 SQL Server 通信

2006年11月16日

CPOL

1分钟阅读

viewsIcon

63063

downloadIcon

581

本文介绍通过远程数据访问对象与服务器通信的方法。

Sample Image - DBManager.jpg

引言

在本文中,我将讨论 Visual Studio .NET 移动开发以及使用 RDA(远程数据访问)对象访问数据和与 PC 通信的方法。我将展示如何将您的设备与服务器同步并共享数据。我将重点介绍 RDA 和复制。我为我的 HP Ipaq HW6500 系列 Pocket PC 开发了这个程序。因此,屏幕尺寸可能不匹配。但您可以更改它以适应您的移动设备的屏幕尺寸。

使用代码

只需运行代码即可。它将被安装到您的设备上。代码需要安装 SQL Server CE,并且需要配置 sscesa20.dll。代码使用 SQL Server 2000(2005 的方式相同),带有 SP3 和 Visual Studio 2003。您可以在 此处 找到所需的信息。

下面给出了源代码的一个示例。这用于提取一个表。

SqlCeConnection cn = null;
string rdaOleDbConnectString = 
    "Provider=sqloledb; Data Source=localhost;" + 
    "Initial Catalog=" + DbManager.glbConn.DbName + "; " +
    "User Id=" + DbManager.glbConn.DbUserId + 
    ";Password=" + DbManager.glbConn.DbPassword ;
SqlCeRemoteDataAccess rda = null;
try 
{
    rda = new SqlCeRemoteDataAccess();
    rda.InternetLogin          = DbManager.glbConn.WebUserId;
    rda.InternetPassword       = DbManager.glbConn.WebPassword;
    rda.InternetUrl            = http://+DbManager.glbConn.ServerIp+
                                 "/"+DbManager.glbConn.WebDir+
                                 "/sscesa20.dll";
    rda.LocalConnectionString ="Data Source=\\Program Files\\"+
                               this.txtLocalDb.Text + ".sdf";
    if (this.chkDropTable.Checked)
    { 
        cn = new SqlCeConnection(rda.LocalConnectionString);
        cn.Open();
        SqlCeCommand cmd = cn.CreateCommand();
        cmd.CommandText = "DROP TABLE " + this.txtLocalTable.Text;
        cmd.ExecuteNonQuery();
        if (cn.State != ConnectionState.Closed)
            cn.Close();
    }
    if (this.cmbTrackOption.Text=="TrackingOff")
        rda.Pull(this.txtLocalTable.Text, 
          this.txtQuery.Text, 
          rdaOleDbConnectString,
          RdaTrackOption.TrackingOff,
          "ErrorTable");
    if (this.cmbTrackOption.Text=="TrackingOn")
        rda.Pull(this.txtLocalTable.Text, 
          this.txtQuery.Text, 
          rdaOleDbConnectString,
          RdaTrackOption.TrackingOn,
          "ErrorTable");
    if (this.cmbTrackOption.Text=="TrackingOffWithIndexes")
        rda.Pull(this.txtLocalTable.Text, 
          this.txtQuery.Text, 
          rdaOleDbConnectString,
          RdaTrackOption.TrackingOffWithIndexes,
          "ErrorTable"); ...

要点

除非您正确输入服务器参数的名称和类型,否则将无法使用 DA 对象,因此请务必小心。

请注意,为了高效利用内存,在 DataGrid 中使用 DataReader,而不是使用 DataSet

此外,您必须确保 sscesa20.dll 已配置并正常工作。确保路径正确。

最后…

随着我开发更多内容,我会提出其他的移动示例。

© . All rights reserved.