SQL Server CE.NET CFWindows MobileSQL Server 2000DBAVisual Studio .NET 2003.NET 1.1Visual Studio 2005.NET 2.0SQL Server 2005C# 2.0IntermediateDevVisual StudioSQL Server.NETC#
SQL 2000 或 SQL 2005 与 SQLCE 之间的合并复制
使用 SQL 2000 或 2005 和 Compact Framework 进行合并复制
引言
本文将提供一个关于如何使用 SQL Server 2000、2005 和 SQL CE 配置合并复制的详细分步指南。
背景
如果您想了解如何创建复制,请阅读 SQL 2000 环境下合并复制的陷阱。如果您还想学习如何发布,请阅读 SQL Server 2000 - 合并复制分步过程。
合并复制设置分步过程
您可以在下载的文件中看到附带图片的步骤。
第一步
- 创建新用户
- 转到 开始 -> 控制面板 -> 管理工具 -> 计算机管理
- 本地用户和组 -> 右键单击 -> 选择 新用户
- 点击 创建
第二步
- 转到 C 盘目录并创建一个新文件夹,如下所示
- 将其命名为 snapshot。
- 右键单击文件夹并选择 共享和安全
- 选择 共享此文件夹,并输入 PDA 作为名称
- 点击 权限,授予完全控制权限。点击 应用,然后点击 确定
第三步
- 通过单击此网站从 Microsoft 下载 SQL SERVER 2005 复制组件
- http://www.microsoft.com/downloads/details.aspx?FamilyID=6ed0fb7e-7c05-4f59-879a-8fb619e36612&displaylang=en
- 在 C:\ 中单击 Sqlce30setupen.msi
- 单击“下一步”
- 选择 我接受许可协议中的条款
- 我们将看到类似的窗口
- 点击 完成
点击 安装
第四步
- 转到 开始 -> 程序 -> sql 2005 mobile edition -> 配置 Web 同步向导
- 单击“下一步”
- 选择 SQL mobile edition,然后单击 下一步
- 选择 创建新的虚拟目录,然后选择 默认网站,然后单击 下一步
- 输入 PDA,然后选择快照文件夹(此处为 C:\snapshot)(如步骤…中所述的快照文件夹的共享名称)
- 如果您是第一次运行,它将显示一个消息框,提示 “文件夹不存在,您要创建该文件夹吗?” 点击 是
- 另一个消息框将弹出,提示该文件夹
- “不包含 SQL Mobile Server Agent 的副本。您要复制并注册 SQL Mobile Server Agent 吗?” 点击 是
- 安全通信选择 不要求安全通道
- 客户端身份验证
- 选择 客户端将匿名连接
- 匿名访问
- 点击 更改
- 点击 高级,然后点击 立即查找
- 从列表中选择 您在第一步中创建的用户,然后点击 确定
- 快照共享访问
- 点击 下一步,如果出现警告“快照文件夹为空。您要继续吗?”,请点击 是
- 点击 完成
- 最后一个屏幕
第五步
- 打开 Internet Explorer 并输入 https:///PDA/sqlcesa30.dll
如果您在 Internet Explorer 中看到相同的内容,则表示 IIS 已成功配置。
第六步
- 转到 安全
- 从下面的列表中选择 sqldbalocal,然后点击 添加,然后按 确定
- 在 服务器角色 选项卡下
- 在 数据库访问 选项卡中,选择与下方所示相同的设置
第八步
- 配置复制
- 单击“下一步”
- 点击 下一步,然后为警告消息点击 确定
- 按 确定
- 按 确定
- 点击 确定,然后输入快照文件夹的名称
- 从下面的列表中选择您需要复制的数据库
- 选择 合并复制,然后点击 下一步
- 选择 运行 SQL Server 2000 和 SQL CE 的设备,然后点击 下一步
- 选择您要复制的表,并单击每张表末尾的复选框
- 选择 下面指定的所有表,并执行上述相同的过程
- 点击 下一步,然后输入发布名称
- 选择 否,创建指定发布
- 下一个屏幕点击 完成
- 点击 发布属性
- 选择 发布访问列表
第九步
- 完成所有这些配置后,您需要创建一个 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 上的第一篇文章。希望大家喜欢!