本文介绍了整个本地数据库同步的SQL天青的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发使用本地数据库中的Windows应用程序。
我想添加一个功能,所有本地数据同步到SQL Azure上。
目前我用下面的code。它使我成功同步某个特定表,这里是Author_Master
字符串sqlazureConnectionString =XXXX;
字符串sqllocalConnectionString =服务器=本地主机;数据库= Enh_Branchwise_Master_Bookshop; Trusted_Connection =真; 使用(SqlConnection的serverCon =新的SqlConnection(sqlazureConnectionString))
使用(SqlConnection的clientCon =新的SqlConnection(sqllocalConnectionString))
{
VAR provider1 =新SqlSyncProvider(scope1,serverCon);
Provider2的VAR =新SqlSyncProvider(scope1,clientCon); prepareServer(provider1);
prepareClinet(Provider2之间,serverCon);
SyncOrchestrator同步=新SyncOrchestrator();
sync.LocalProvider = provider1;
sync.RemoteProvider = Provider2之间; sync.Synchronize(); }
和下面的方法也。
私有静态无效prepareServer(SqlSyncProvider提供商)
{
SqlConnection的连接=(的SqlConnection)provider.Connection;
SqlSyncScopeProvisioning配置=新SqlSyncScopeProvisioning(连接); 如果(!config.ScopeExists(provider.ScopeName))
{
DbSyncScopeDescription scopeDesc =新DbSyncScopeDescription(provider.ScopeName);
scopeDesc.Tables.Add(SqlSyncDescriptionBuilder.GetDescriptionForTable(Author_Master连接));
config.PopulateFromScopeDescription(scopeDesc);
config.SetCreateTableDefault(DbSyncCreationOption.CreateOrUseExisting);
config.Apply();
}
} 私有静态无效prepareClinet(SqlSyncProvider供应商的SqlConnection sourceConnection)
{
SqlConnection的连接=(的SqlConnection)provider.Connection;
SqlSyncScopeProvisioning配置=新SqlSyncScopeProvisioning(连接); 如果(!config.ScopeExists(provider.ScopeName))
{
DbSyncScopeDescription scopeDesc =新DbSyncScopeDescription(provider.ScopeName);
scopeDesc.Tables.Add(SqlSyncDescriptionBuilder.GetDescriptionForTable(Author_Master,sourceConnection));
config.PopulateFromScopeDescription(scopeDesc);
config.Apply();
}
}
我的问题:有什么办法可以同步在数据库中,一旦所有的表,而不会被一个表添加一个。
我的两个数据库具有相同的架构。
请给一些建议,
解决方案
昏暗clientProvision作为SqlSyncScopeProvisioning =新SqlSyncScopeProvisioning(clientCon,syncScope)
如果不是(clientProvision.ScopeExists(Scope_+ tableName值))然后
clientProvision.Apply()
万一 昏暗serverProvision作为SqlSyncScopeProvisioning =新SqlSyncScopeProvisioning(serverCon,syncScope) 如果不是(serverProvision.ScopeExists(Scope_+ tableName值))然后
serverProvision.Apply()
万一 昏暗syncOrchestrator作为新SyncOrchestrator() 创建SQL Server提供
昏暗clientProvider作为新SqlSyncProvider(Scope_+ tableName值,clientCon) '设置为SQL Azure的提供者命令超时和最大的交易规模。
昏暗serverProvider作为新SqlSyncProvider(Scope_+ tableName值,serverCon) SyncOrchestrator的设置本地供应商的上premise提供商
syncOrchestrator.LocalProvider = clientProvider SyncOrchestrator的设置远程提供商的azureProvider提供商
syncOrchestrator.RemoteProvider = serverProvider '设置SyncOrchestrator会议上传和下载方向
syncOrchestrator.Direction = SyncDirectionOrder.UploadAndDownload
昏暗的线程作为新Threading.Thread(子()ShowStatistics(syncOrchestrator.Synchronize()中,TableName))
thread.Start()
ShowStatistics(syncOrchestrator.Synchronize()中,TableName)
I am developing a windows application which is using local database.I want to add a function to sync all local data to the sql azure.
Currently I used following code. It enabled me to sync one particular table successfully., here is "Author_Master"
string sqlazureConnectionString = "XXXX";
string sqllocalConnectionString = "Server=localhost;Database=Enh_Branchwise_Master_Bookshop;Trusted_Connection=True";
using (SqlConnection serverCon = new SqlConnection(sqlazureConnectionString))
using (SqlConnection clientCon = new SqlConnection(sqllocalConnectionString))
{
var provider1 = new SqlSyncProvider("scope1", serverCon);
var provider2 = new SqlSyncProvider("scope1", clientCon);
prepareServer(provider1);
prepareClinet(provider2, serverCon);
SyncOrchestrator sync = new SyncOrchestrator();
sync.LocalProvider = provider1;
sync.RemoteProvider = provider2;
sync.Synchronize();
}
And following methods also.
private static void prepareServer(SqlSyncProvider provider)
{
SqlConnection connection = (SqlConnection)provider.Connection;
SqlSyncScopeProvisioning config = new SqlSyncScopeProvisioning(connection);
if (!config.ScopeExists(provider.ScopeName))
{
DbSyncScopeDescription scopeDesc = new DbSyncScopeDescription(provider.ScopeName);
scopeDesc.Tables.Add(SqlSyncDescriptionBuilder.GetDescriptionForTable("Author_Master", connection));
config.PopulateFromScopeDescription(scopeDesc);
config.SetCreateTableDefault(DbSyncCreationOption.CreateOrUseExisting);
config.Apply();
}
}
private static void prepareClinet(SqlSyncProvider provider, SqlConnection sourceConnection)
{
SqlConnection connection = (SqlConnection)provider.Connection;
SqlSyncScopeProvisioning config = new SqlSyncScopeProvisioning(connection);
if (!config.ScopeExists(provider.ScopeName))
{
DbSyncScopeDescription scopeDesc = new DbSyncScopeDescription(provider.ScopeName);
scopeDesc.Tables.Add(SqlSyncDescriptionBuilder.GetDescriptionForTable("Author_Master", sourceConnection));
config.PopulateFromScopeDescription(scopeDesc);
config.Apply();
}
}
My question : Is there any way to sync all the tables in the database at once, without adding one by one table .
My both Databases have the same schema.Please give some suggestions,
解决方案
Dim clientProvision As SqlSyncScopeProvisioning = New SqlSyncScopeProvisioning(clientCon, syncScope)
If Not (clientProvision.ScopeExists("Scope_" + tableName)) Then
clientProvision.Apply()
End If
Dim serverProvision As SqlSyncScopeProvisioning = New SqlSyncScopeProvisioning(serverCon, syncScope)
If Not (serverProvision.ScopeExists("Scope_" + tableName)) Then
serverProvision.Apply()
End If
Dim syncOrchestrator As New SyncOrchestrator()
' Create provider for SQL Server
Dim clientProvider As New SqlSyncProvider("Scope_" + tableName, clientCon)
' Set the command timeout and maximum transaction size for the SQL Azure provider.
Dim serverProvider As New SqlSyncProvider("Scope_" + tableName, serverCon)
' Set Local provider of SyncOrchestrator to the onPremise provider
syncOrchestrator.LocalProvider = clientProvider
' Set Remote provider of SyncOrchestrator to the azureProvider provider
syncOrchestrator.RemoteProvider = serverProvider
' Set the direction of SyncOrchestrator session to Upload and Download
syncOrchestrator.Direction = SyncDirectionOrder.UploadAndDownload
'Dim thread As New Threading.Thread(Sub() ShowStatistics(syncOrchestrator.Synchronize(), tableName))
'thread.Start()
ShowStatistics(syncOrchestrator.Synchronize(), tableName)
这篇关于整个本地数据库同步的SQL天青的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!