我正在使用Microsoft Sync Framework v2.1来同步两个数据库,即从远程(ms sql server 2012)到本地(ms sql server express 2008 R2)。可以在本地数据库上成功创建表,但由于以下错误,数据未同步:
无法完成当前操作,因为数据库未配置为同步,或者您没有同步配置表的权限。
但是,在同步2个本地数据库时(使用ms sql server express 2008 R2),同步成功。
有人对这个问题有什么建议吗?
感谢。
CODE
以下是主要表格:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// create a connection to the SyncExpressDB database
string clientConn = ConfigurationManager.ConnectionStrings["ianConnection"].ConnectionString;
// create a connection to the SyncDB server database
string serverConn = ConfigurationManager.ConnectionStrings["arvixeConnection"].ConnectionString;
Utilities.Synchronisation.Db.DBSynchroniser dbSync = new DBSynchroniser(clientConn, serverConn);
dbSync.ProvisionSyncScope("TestScope", "Products", DBSyncSide.Both);
dbSync.Sync(Microsoft.Synchronization.SyncDirectionOrder.Download, "TestScope");
label1.Text = "Sync Done !!!";
}
}
以下是同步类
public class DBSynchroniser
{
SqlConnection clientConnection;
SqlConnection serverConnection;
SyncOrchestrator syncOrchestrator;
private EventHandler<DbApplyChangeFailedEventArgs> changeFailedHandler;
public EventHandler<DbApplyChangeFailedEventArgs> ChangeFailedHandler
{
get { return changeFailedHandler; }
set { changeFailedHandler = value; }
}
public DBSynchroniser(string clientConn, string serverConn)
{
clientConnection = new SqlConnection(clientConn);
serverConnection = new SqlConnection(serverConn);
syncOrchestrator = new SyncOrchestrator();
changeFailedHandler = new EventHandler<DbApplyChangeFailedEventArgs>(ApplyChangeFailed);
}
public void ProvisionSyncScope(string syncScopeName, string tableName, DBSyncSide syncSide)
{
ProvisionSyncScope(syncScopeName, new List<string>(new string[] { tableName }), syncSide);
}
public void ProvisionSyncScope(string syncScopeName, List<string> tableNames, DBSyncSide syncSide)
{
DbSyncScopeDescription scopeDesc;
// define a new scope
scopeDesc = new DbSyncScopeDescription(syncScopeName);
scopeDesc.UserComment = "This is to test the sync class";
foreach (string name in tableNames)
{
// get the description of the table name
// and add the table description to the sync scope definition
scopeDesc.Tables.Add(SqlSyncDescriptionBuilder.GetDescriptionForTable(name, serverConnection));
}
// create a server scope provisioning object based on the ProductScope
SqlSyncScopeProvisioning serverProvision = new SqlSyncScopeProvisioning(serverConnection, scopeDesc);
SqlSyncScopeProvisioning clientProvision = new SqlSyncScopeProvisioning(clientConnection, scopeDesc);
// skipping the creation of table since table already exists on server
serverProvision.SetCreateTableDefault(DbSyncCreationOption.CreateOrUseExisting);
clientProvision.SetCreateTableDefault(DbSyncCreationOption.CreateOrUseExisting);
// start the provisioning process
switch (syncSide)
{
case DBSyncSide.Client:
clientProvision.Apply();
break;
case DBSyncSide.Server:
serverProvision.Apply();
break;
case DBSyncSide.Both:
serverProvision.Apply();
clientProvision.Apply();
break;
default:
break;
}
}
public void DeprovisionScope(string scopeName, DBSyncSide syncSide)
{
SqlSyncScopeDeprovisioning clientSqlDepro = new SqlSyncScopeDeprovisioning(clientConnection);
SqlSyncScopeDeprovisioning serverSqlDepro = new SqlSyncScopeDeprovisioning(serverConnection);
// First save the deprovisioning script so it can be run on other SQL Server client databases.
// This step is optional.
//File.WriteAllText("SampleDeprovisionScript.txt", clientSqlDepro.ScriptDeprovisionScope(scopeName));
// Remove the scope.
switch (syncSide)
{
case DBSyncSide.Client:
clientSqlDepro.DeprovisionScope(scopeName);
break;
case DBSyncSide.Server:
serverSqlDepro.DeprovisionScope(scopeName);
break;
case DBSyncSide.Both:
clientSqlDepro.DeprovisionScope(scopeName);
serverSqlDepro.DeprovisionScope(scopeName);
break;
default:
break;
}
}
public DBSyncOperationStatistics Sync(SyncDirectionOrder direction, string syncScopeName)
{
// set local provider of orchestrator to a sync provider associated with the
// MySyncScope in the client database
syncOrchestrator.LocalProvider = new SqlSyncProvider(syncScopeName, clientConnection); //check objectPrefix and schema
// set the remote provider of orchestrator to a server sync provider associated with
// the MySyncScope in the server database
syncOrchestrator.RemoteProvider = new SqlSyncProvider(syncScopeName, serverConnection); //check objectPrefix and schema
// set the direction of sync session to Upload and Download
syncOrchestrator.Direction = direction;
// subscribe for errors that occur when applying changes to the client
((SqlSyncProvider)syncOrchestrator.LocalProvider).ApplyChangeFailed += changeFailedHandler;
// execute the synchronization process
DBSyncOperationStatistics syncStats = new DBSyncOperationStatistics(syncOrchestrator.Synchronize());
//return statistics of synchronisation
return syncStats;
}
private void ApplyChangeFailed(object sender, DbApplyChangeFailedEventArgs e)
{
throw e.Error;
}
~DBSynchroniser()
{
if (clientConnection.State == System.Data.ConnectionState.Open)
{
clientConnection.Close();
clientConnection.Dispose();
}
if (serverConnection.State == System.Data.ConnectionState.Open)
{
serverConnection.Close();
serverConnection.Dispose();
}
}
}