using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Synchronization;
using Microsoft.Synchronization.Data;
using Microsoft.Synchronization.Data.SqlServer;
using System.Configuration;
using System.Windows;
namespace TestingSynch
{
public static class PosSynch
{
/// <summary>
/// Decaription:Parmaeter
/// </summary>
static string sServerConnection = ConfigurationManager.AppSettings["POSServer"];
static string sClientConnection = ConfigurationManager.AppSettings["POSClient"];
static string sScope;
/// <summary>
/// Description:Set Server Provision Descritpion Table
/// </summary>
public static void ProvisionServer()
{
SqlConnection serverConn = new SqlConnection(sServerConnection);
SqlConnection clientConn = new SqlConnection(sClientConnection);
DataTable Dt = new DataTable();
sScope = GetScope();
Dt = GetServerTable();
DbSyncScopeDescription scopeDesc = new DbSyncScopeDescription(sScope);
for (int i = 0; i <= Dt.Rows.Count - 1; i++)
{
DbSyncTableDescription tableDesc = SqlSyncDescriptionBuilder.GetDescriptionForTable(Dt.Rows[i][0].ToString(), serverConn);
scopeDesc.Tables.Add(tableDesc);
}
Provision(scopeDesc, serverConn);
}
public static void Provision(DbSyncScopeDescription scopeDesc, SqlConnection serverConn)
{
SqlSyncScopeProvisioning serverProvision = new SqlSyncScopeProvisioning(serverConn, scopeDesc);
serverProvision.SetCreateTableDefault(DbSyncCreationOption.Skip);
serverProvision.Apply();
}
public static void Deprovision(SqlConnection serverConn,SqlConnection clientconnection)
{
SqlSyncScopeDeprovisioning objServer = new SqlSyncScopeDeprovisioning(serverConn);
SqlSyncScopeDeprovisioning objClient = new SqlSyncScopeDeprovisioning(clientconnection);
DataTable dtserver = new DataTable();
DataTable dtclient = new DataTable();
try
{
string query = null;
query = ConfigurationManager.AppSettings["ScopInfo"];
serverConn = new SqlConnection(sServerConnection);
SqlDataAdapter sda = new SqlDataAdapter(query, serverConn);
sda.Fill(dtserver);
foreach (var item in dtserver.Rows)
{
objServer.DeprovisionScope(item.ToString());
}
query = ConfigurationManager.AppSettings["ScopInfo"];
serverConn = new SqlConnection(sClientConnection);
SqlDataAdapter sdac = new SqlDataAdapter(query, clientconnection);
sda.Fill(dtclient);
foreach (var item in dtclient.Rows)
{
objClient.DeprovisionScope(item.ToString());
}
}
catch
{
}
finally
{
dtclient.Dispose();
dtserver.Dispose();
}
// return null;
//obj.DeprovisionTemplate(scopeDesc.ScopeName);
//foreach (var item in scopeDesc.Tables)
//{
// obj.DeprovisionTemplate(item.LocalName.ToString());
//}
}
public static DataTable GetServerTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("Tables");
try
{
//string query = null;
//query = ConfigurationManager.AppSettings["ServerTableQuery"];
//SqlConnection serverConn = new SqlConnection(sServerConnection);
//SqlDataAdapter sda = new SqlDataAdapter(query, serverConn);
//sda.Fill(dt);
//dt.Rows.Add("POSEmailSetting");
//dt.Rows.Add("UserDetails");
//dt.Rows.Add("ParkingRequestHistory");
//dt.Rows.Add("Logins");
dt.Rows.Add("ParkingRequestMaster");
//dt.Rows.Add("ParkingRequestProductOrderDetail");
//dt.Rows.Add("ParkingYardMaster");
//dt.Rows.Add("PosLogRecords");
return dt;
}
catch
{
}
finally
{
dt.Dispose();
}
return null;
}
/// <summary>
/// Description Create New Scope While Hitting Server
/// </summary>
/// <returns></returns>
public static string GetScope()
{
Random objrand = new Random();
sScope = "SyncTableScope_" + objrand.Next().ToString();
return sScope;
}
/// <summary>
/// Description:Set Client Provision Description Table
/// </summary>
public static void ProvisionClient()
{
SqlConnection serverConn = new SqlConnection(sServerConnection);
SqlConnection clientConn = new SqlConnection(sClientConnection);
DbSyncScopeDescription scopeDesc = SqlSyncDescriptionBuilder.GetDescriptionForScope(sScope, serverConn);
//DbSyncScopeDescription scopeDesc = SqlSyncDescriptionBuilder.GetDescriptionForTable(sScope, serverConn);
// DbSyncScopeDescription scopeDesc = SqlSyncDescriptionBuilder.GetDescriptionForTable(sScope, serverConn);
SqlSyncScopeProvisioning clientProvision = new SqlSyncScopeProvisioning(clientConn, scopeDesc);
var text = clientProvision.Script();
clientProvision.Apply();
}
/// <summary>
/// Description :Synchronized Data Between Client & Server Apps
/// </summary>
public static void Sync()
{
SqlConnection serverConn = new SqlConnection(sServerConnection);
SqlConnection clientConn = new SqlConnection(sClientConnection);
SyncOrchestrator syncOrchestrator = new SyncOrchestrator();
syncOrchestrator.LocalProvider = new SqlSyncProvider(sScope, clientConn);
syncOrchestrator.RemoteProvider = new SqlSyncProvider(sScope, serverConn);
syncOrchestrator.Direction = SyncDirectionOrder.DownloadAndUpload;
((SqlSyncProvider)syncOrchestrator.LocalProvider).ApplyChangeFailed += new EventHandler<DbApplyChangeFailedEventArgs>(Program_ApplyChangeFailed);
SyncOperationStatistics syncStats = syncOrchestrator.Synchronize();
Console.WriteLine("Start Time: " + syncStats.SyncStartTime);
Console.WriteLine("Total Changes Uploaded: " + syncStats.UploadChangesTotal);
Console.WriteLine("Total Changes Downloaded: " + syncStats.DownloadChangesTotal);
Console.WriteLine("Complete Time: " + syncStats.SyncEndTime);
Console.WriteLine(String.Empty);
Console.ReadLine();
}
static void Program_ApplyChangeFailed(object sender, DbApplyChangeFailedEventArgs e)
{
Console.WriteLine(e.Conflict.Type);
Console.WriteLine(e.Error);
}
}
}
While Provisoning Server Db ,in I m facing error Invallid Coloumn name
无效的列名称&#39; BookingSource&#39;。 列名称无效&#39; IsCheckedIn&#39;。 列名称无效&#39; FlightDetails&#39;。 列名称无效&#39; CheckedOutBy&#39;。 列名称无效&#39; CheckedInBy&#39;。 列名称无效&#39; IsCheckedOut&#39;。 列名称无效&#39; ParkedAtLocation&#39;。