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

SQL 2000 或 SQL 2005 与 SQLCE 之间的合并复制

starIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

1.00/5 (1投票)

2007年8月20日

CPOL

3分钟阅读

viewsIcon

38496

downloadIcon

299

使用 SQL 2000 或 2005 和 Compact Framework 进行合并复制

引言

本文将提供一个关于如何使用 SQL Server 2000、2005 和 SQL CE 配置合并复制的详细分步指南。

背景

如果您想了解如何创建复制,请阅读 SQL 2000 环境下合并复制的陷阱。如果您还想学习如何发布,请阅读 SQL Server 2000 - 合并复制分步过程

合并复制设置分步过程

您可以在下载的文件中看到附带图片的步骤。

第一步

  1. 创建新用户
  2. 转到 开始 -> 控制面板 -> 管理工具 -> 计算机管理
  3. 本地用户和组 -> 右键单击 -> 选择 新用户
  4. 点击 创建

第二步

  1. 转到 C 盘目录并创建一个新文件夹,如下所示
  2. 将其命名为 snapshot。
  3. 右键单击文件夹并选择 共享和安全
  4. 选择 共享此文件夹,并输入 PDA 作为名称
  5. 点击 权限,授予完全控制权限。点击 应用,然后点击 确定

第三步

  1. 通过单击此网站从 Microsoft 下载 SQL SERVER 2005 复制组件
  2. http://www.microsoft.com/downloads/details.aspx?FamilyID=6ed0fb7e-7c05-4f59-879a-8fb619e36612&displaylang=en
  3. 在 C:\ 中单击 Sqlce30setupen.msi
  4. 单击“下一步”
  5. 选择 我接受许可协议中的条款
  6. 点击 安装

  7. 我们将看到类似的窗口
  8. 点击 完成

第四步

  1. 转到 开始 -> 程序 -> sql 2005 mobile edition -> 配置 Web 同步向导
  2. 单击“下一步”
  3. 选择 SQL mobile edition,然后单击 下一步
  4. 选择 创建新的虚拟目录,然后选择 默认网站,然后单击 下一步
  5. 输入 PDA,然后选择快照文件夹(此处为 C:\snapshot)(如步骤…中所述的快照文件夹的共享名称)
  6. 如果您是第一次运行,它将显示一个消息框,提示 “文件夹不存在,您要创建该文件夹吗?” 点击
  7. 另一个消息框将弹出,提示该文件夹
  8. “不包含 SQL Mobile Server Agent 的副本。您要复制并注册 SQL Mobile Server Agent 吗?” 点击
  9. 安全通信选择 不要求安全通道
  10. 客户端身份验证
  11. 选择 客户端将匿名连接
  12. 匿名访问
  13. 点击 更改
  14. 点击 高级,然后点击 立即查找
  15. 从列表中选择 您在第一步中创建的用户,然后点击 确定
  16. 快照共享访问
  17. 点击 下一步,如果出现警告“快照文件夹为空。您要继续吗?”,请点击 是
  18. 点击 完成
  19. 最后一个屏幕

第五步

  1. 打开 Internet Explorer 并输入 https:///PDA/sqlcesa30.dll
  2. 如果您在 Internet Explorer 中看到相同的内容,则表示 IIS 已成功配置。

第六步

  1. 转到 安全
  2. 从下面的列表中选择 sqldbalocal,然后点击 添加,然后按 确定
  3. 在 服务器角色 选项卡下
  4. 在 数据库访问 选项卡中,选择与下方所示相同的设置

第八步

  1. 配置复制
  2. 单击“下一步”
  3. 点击 下一步,然后为警告消息点击 确定
  4. 按 确定
  5. 按 确定
  6. 点击 确定,然后输入快照文件夹的名称
  7. 从下面的列表中选择您需要复制的数据库
  8. 选择 合并复制,然后点击 下一步
  9. 选择 运行 SQL Server 2000 和 SQL CE 的设备,然后点击 下一步
  10. 选择您要复制的表,并单击每张表末尾的复选框
  11. 选择 下面指定的所有表,并执行上述相同的过程
  12. 点击 下一步,然后输入发布名称
  13. 选择 否,创建指定发布
  14. 下一个屏幕点击 完成
  15. 点击 发布属性
  16. 选择 发布访问列表

第九步

  1. 完成所有这些配置后,您需要创建一个 POCKET PC 应用程序并将此代码粘贴进去

Using the Code

您可以在下载文件中看到一个 XML 文件,您需要根据您的配置修改该文件。之后,将一个类文件添加到您的 PDA 应用程序中,并将这些代码行复制到类文件中。

//

Imports System.Data.SqlServerCe

Imports System.Data.SqlServerCe.SqlCeException

Imports System.Xml

Imports System.Reflection

Imports System.io

Imports System.text

Imports System.Data

Imports System.Net

Public Class DatabaseConfig

#Region "Variables"

Private Shared boolConfigLoaded = False

Private Shared strDatabaseServer As String

Private Shared strPublisherDB As String

Private Shared strPublisher As String

Private Shared strPublisherLogin As String

Private Shared strPublisherPassword As String

Private Shared strSubscriberConnectionString As String

Private Shared strSubscriber As String

Private Shared strSqlCeUrl As String

Private Shared strLocalDBLocation As String

Private Shared strLocalDBName As String

Private Shared strIISLogin As String

Private Shared strIISPassword As String

 

#End Region

#Region "Properties"

Private Property ConfigLoaded() As Boolean

Get

Return boolConfigLoaded

End Get

Set(ByVal Value As Boolean)

boolConfigLoaded = Value

End Set

End Property

Public Property DatabaseServer() As String

Get

' Check to make sure config has been loaded

If Not ConfigLoaded Then

LoadDBConfigSettings()

End If

Return strDatabaseServer

End Get

Set(ByVal Value As String)

strDatabaseServer = Value

End Set

End Property

Public Property PublisherDB() As String

Get

' Check to make sure config has been loaded

If Not ConfigLoaded Then

LoadDBConfigSettings()

End If

Return strPublisherDB

End Get

Set(ByVal Value As String)

strPublisherDB = Value

End Set

End Property

Public Property Publisher() As String

Get

' Check to make sure config has been loaded

If Not ConfigLoaded Then

LoadDBConfigSettings()

End If

Return strPublisher

End Get

Set(ByVal Value As String)

strPublisher = Value

End Set

End Property

Public Property PublisherLogin() As String

Get

' Check to make sure config has been loaded

If Not ConfigLoaded Then

LoadDBConfigSettings()

End If

Return strPublisherLogin

End Get

Set(ByVal Value As String)

strPublisherLogin = Value

End Set

End Property

Public Property PublisherPassword() As String

Get

' Check to make sure config has been loaded

If Not ConfigLoaded Then

LoadDBConfigSettings()

End If

Return strPublisherPassword

End Get

Set(ByVal Value As String)

strPublisherPassword = Value

End Set

End Property

Public Property SubscriberConnectionString() As String

Get

' Check to make sure config has been loaded

If Not ConfigLoaded Then

LoadDBConfigSettings()

End If

Return "data source=" & AppWorkingDir & "\" & strLocalDBName

End Get

Set(ByVal Value As String)

strSubscriberConnectionString = Value

End Set

End Property

Public Property SqlCeUrl() As String

Get

' Check to make sure config has been loaded

If Not ConfigLoaded Then

LoadDBConfigSettings()

End If

Return strSqlCeUrl

End Get

Set(ByVal Value As String)

strSqlCeUrl = Value

End Set

End Property

Public Property LocalDBLocation() As String

Get

' Check to make sure config has been loaded

If Not ConfigLoaded Then

LoadDBConfigSettings()

End If

Return AppWorkingDir & "\" & LocalDBName

End Get

Set(ByVal Value As String)

strLocalDBLocation = Value

End Set

End Property

Public Property LocalDBName() As String

Get

' Check to make sure config has been loaded

If Not ConfigLoaded Then

LoadDBConfigSettings()

End If

Return strLocalDBName

End Get

Set(ByVal Value As String)

strLocalDBName = Value

End Set

End Property

Public ReadOnly Property AppWorkingDir() As String

Get

Return Path.GetDirectoryName([Assembly].GetExecutingAssembly.GetName.CodeBase)

End Get

End Property

#End Region

Private Sub LoadDBConfigSettings()

Dim strElementName As String = String.Empty

'Dim strAppWorkingDir As String = Path.GetDirectoryName([Assembly].GetExecutingAssembly.GetName.CodeBase)

'Dim strAppWorkingDir As String = "\Program Files\LATESTSYNC\"

' Get the configuration data from the config XML file

'Dim ConfigFile As New IO.FileStream("\Program Files\LATESTSYNC\SyncInfo.xml", IO.FileMode.Open)

 

'Dim strAppWorkingDir As String = Path.GetDirectoryName([Assembly].GetExecutingAssembly.GetName.CodeBase)

' Get the configuration data from the config XML file

Dim ConfigFile As New IO.FileStream(AppWorkingDir & "\syncinfo.xml", IO.FileMode.Open)

' MsgBox(Path.GetFullPath("My Documents\SyncInfo.xml"))

 

Dim ConfigReader As New Xml.XmlTextReader(ConfigFile)

ConfigReader.WhitespaceHandling = WhitespaceHandling.Significant

While ConfigReader.Read

If ConfigReader.NodeType = XmlNodeType.Element Then


strElementName = ConfigReader.Name

ElseIf ConfigReader.NodeType = XmlNodeType.Text Then

' Use the last element name read to determine what value we're looking at

Select Case strElementName

Case "DatabaseServer"

strDatabaseServer = ConfigReader.Value

Case "DatabaseName"

strPublisherDB = ConfigReader.Value

Case "DatabasePub"

strPublisher = ConfigReader.Value

Case "DatabaseLogin"

strPublisherLogin = ConfigReader.Value

Case "DatabasePassword"

strPublisherPassword = ConfigReader.Value

Case "SQLCEURL"

strSqlCeUrl = ConfigReader.Value

Case "LocalDBConnect"

strSubscriberConnectionString = ConfigReader.Value

Case "LocalDBLocation"

strLocalDBLocation = ConfigReader.Value

Case "LocalDBName"

strLocalDBName = ConfigReader.Value

End Select

End If

End While


ConfigReader.Close()

ConfigFile.Close()


ConfigLoaded = True

End Sub

End Class

Public Class DBAccess

Dim oDBConfig As New DatabaseConfig

 

Private Sub ReplicateData()

Try

Dim replicator As New SqlCeReplication

replicator.InternetUrl = oDBConfig.SqlCeUrl

replicator.Publisher = oDBConfig.DatabaseServer

replicator.PublisherDatabase = oDBConfig.PublisherDB

replicator.PublisherSecurityMode = SecurityType.NTAuthentication

replicator.Publication = oDBConfig.Publisher

replicator.InternetLogin = "Administrator"

replicator.InternetPassword = "password"

replicator.Subscriber = Dns.GetHostName()

replicator.SubscriberConnectionString = oDBConfig.SubscriberConnectionString

replicator.Synchronize()

MsgBox("Data Synchronized", , "THPMIS - PDA")

Catch ex As SqlCeException

MsgBox(ex.Message, MsgBoxStyle.Critical, "THPMIS - PDA")

End Try

End Sub

Public Sub CreateDB()

Try

If File.Exists(oDBConfig.LocalDBLocation) = False Then

Dim engine As New System.Data.SqlServerCe.SqlCeEngine(oDBConfig.SubscriberConnectionString)

engine.CreateDatabase()

ReplicateData()

engine.Dispose()

Else

ReplicateData()

End If

Catch ex As Exception

MsgBox(ex.Message)

Finally

Cursor.Current = Cursors.Default

End Try

End Sub

End Class
//

关注点

这是使用 SQL CE 进行 PDA 开发中最有趣的部分之一,您可以轻松地将 SQL 2000 或 2005 数据库传输到您的手持设备。这是我在 www.codeproject.com 上的第一篇文章。希望大家喜欢!

© . All rights reserved.