📄 service.asmx.cs
字号:
using System;
using System.Collections;
using System.Text;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Sql;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data.SqlClient;
using System.Configuration;
using Microsoft.Synchronization;
using Microsoft.Synchronization.Data;
using Microsoft.Synchronization.Data.Server;
namespace HardwareDistributorSyncService
{
/// <summary>
/// Service provider for synchronization. This provider is specific to SQL 2008 version
/// and makes use of version specific features to enable synchronization
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class Service : System.Web.Services.WebService
{
private DbServerSyncProvider _serverSyncProvider;
//codes used by SQL 2008 Changes() return table for operations
private static string OPERATION_INSERT = "I";
private static string OPERATION_UPDATE = "U";
private static string OPERATION_DELETE = "D";
public Service()
{
try
{
InitializeSyncProvider();
}
catch (Exception e)
{
throw;
}
}
/// <summary>
/// sets up the table adapters and connections for the provider
/// </summary>
private void InitializeSyncProvider()
{
_serverSyncProvider = new DbServerSyncProvider();
//get connection
string connectionString = ConfigurationManager.ConnectionStrings["HardwareDistributorDB"].ConnectionString;
SqlConnection serverConnection = new SqlConnection(connectionString);
_serverSyncProvider.Connection = serverConnection;
//setup adapters for the tables
//customer
_serverSyncProvider.SyncAdapters.Add(
GetDownloadOnlyTableAdapter("Customers", "CustomerId", null));
//Distribution Centres
_serverSyncProvider.SyncAdapters.Add(
GetDownloadOnlyTableAdapter("DistributionCenters", "DistributionCenterId", null));
//Employees
_serverSyncProvider.SyncAdapters.Add(
GetDownloadOnlyTableAdapter("Employees", "EmployeeId", null));
//OrderState
_serverSyncProvider.SyncAdapters.Add(
GetDownloadOnlyTableAdapter("OrderState", "OrderStateId", null));
//Roles
_serverSyncProvider.SyncAdapters.Add(
GetDownloadOnlyTableAdapter("Roles", "RoleId", null));
//Routes
_serverSyncProvider.SyncAdapters.Add(
GetDownloadOnlyTableAdapter("Routes", "RouteId", null));
//Trucks
_serverSyncProvider.SyncAdapters.Add(
GetDownloadOnlyTableAdapter("Trucks", "TruckId", null));
//configure the bi-directional tables
_serverSyncProvider.SyncAdapters.Add(
GetInventoryTableAdapter()
);
_serverSyncProvider.SyncAdapters.Add(
GetOrderTableAdapter()
);
_serverSyncProvider.SyncAdapters.Add(
GetOrderDetailsTableAdapter()
);
//setup the anchoring command that is used to determine newer rows
//this command is at a global level and will be used across tables
_serverSyncProvider.SelectNewAnchorCommand = GetNewAnchorCommand(serverConnection);
}
private SyncAdapter GetOrderTableAdapter()
{
//// Leveraging the SyncAdapterBuilder to construct all the commands
//SqlSyncAdapterBuilder builder = new SqlSyncAdapterBuilder((SqlConnection)this.Connection);
//builder.SyncDirection = SyncDirection.Bidirectional;
//builder.CreationTrackingColumn = "CreationDate";
//builder.UpdateTrackingColumn = "LastEditDate";
//builder.DeletionTrackingColumn = "DeletionDate";
//// Orders
//builder.TableName = "Orders";
//builder.TombstoneTableName = builder.TableName + "_Tombstone";
//// Add the configured adapter to the collection
////TODO: What's the default here? Do we need to pass these parameters for the default scenario?
//return this.SyncAdapters.Add(builder.ToSyncAdapter(true, true, false, false));
//server to client incremental updates
SyncAdapter adapter = GetDownloadOnlyTableAdapter("Orders", "OrderId", null);
//client to server insert
SqlCommand insertCommand = new SqlCommand();
insertCommand.CommandText = "spInsertOrder";
insertCommand.CommandType = CommandType.StoredProcedure;
insertCommand.Parameters.Add("@OrderId", SqlDbType.UniqueIdentifier);
insertCommand.Parameters.Add("@CustomerId", SqlDbType.Int);
insertCommand.Parameters.Add("@DeliveryDate", SqlDbType.DateTime);
insertCommand.Parameters.Add("@OrderState", SqlDbType.Int);
insertCommand.Parameters.Add("@Signature", SqlDbType.VarBinary);
insertCommand.Parameters.Add("@" + SyncSession.SyncRowCount, SqlDbType.Int).Direction = ParameterDirection.Output;
adapter.InsertCommand = insertCommand;
//client to server update
SqlCommand updateCommand = new SqlCommand();
updateCommand.CommandText ="spUpdateOrder";
updateCommand.CommandType = CommandType.StoredProcedure;
updateCommand.Parameters.Add("@OrderId", SqlDbType.UniqueIdentifier);
updateCommand.Parameters.Add("@CustomerId", SqlDbType.Int);
updateCommand.Parameters.Add("@DeliveryDate", SqlDbType.DateTime);
updateCommand.Parameters.Add("@OrderState", SqlDbType.Int);
updateCommand.Parameters.Add("@Signature", SqlDbType.VarBinary);
updateCommand.Parameters.Add("@" + SyncSession.SyncLastReceivedAnchor, SqlDbType.Int);
updateCommand.Parameters.Add("@" + SyncSession.SyncRowCount, SqlDbType.Int).Direction = ParameterDirection.Output;
adapter.UpdateCommand = updateCommand;
SqlCommand updateConflict = new SqlCommand();
updateConflict.CommandText = @"
SELECT CustomerId, DeliveryDate, OrderState, Signature
FROM Orders
WHERE OrderId = @OrderId";
updateConflict.Parameters.Add("@OrderId", SqlDbType.UniqueIdentifier);
adapter.SelectConflictUpdatedRowsCommand = updateConflict;
//client to server delete
SqlCommand deleteCommand = new SqlCommand();
deleteCommand.CommandText = "spDeleteOrder";
deleteCommand.CommandType = CommandType.StoredProcedure;
deleteCommand.Parameters.Add("@OrderId", SqlDbType.UniqueIdentifier);
deleteCommand.Parameters.Add("@" + SyncSession.SyncRowCount, SqlDbType.Int).Direction = ParameterDirection.Output;
adapter.DeleteCommand = deleteCommand;
return adapter;
}
private SyncAdapter GetOrderDetailsTableAdapter()
{
SyncAdapter adapter = GetDownloadOnlyTableAdapter("OrderDetails", "OrderDetailId", null);
//client to server insert
SqlCommand insertCommand = new SqlCommand();
insertCommand.CommandText = "spInsertOrderDetails";
insertCommand.CommandType = CommandType.StoredProcedure;
insertCommand.Parameters.Add("@OrderDetailId", SqlDbType.UniqueIdentifier);
insertCommand.Parameters.Add("@OrderId", SqlDbType.UniqueIdentifier);
insertCommand.Parameters.Add("@InventoryId", SqlDbType.Int);
insertCommand.Parameters.Add("@Quantity", SqlDbType.Int);
insertCommand.Parameters.Add("@" + SyncSession.SyncRowCount, SqlDbType.Int).Direction = ParameterDirection.Output;
adapter.InsertCommand = insertCommand;
//client to server update
SqlCommand updateCommand = new SqlCommand();
updateCommand.CommandText = "spUpdateOrderDetails";
updateCommand.CommandType = CommandType.StoredProcedure;
updateCommand.Parameters.Add("@OrderId", SqlDbType.UniqueIdentifier);
updateCommand.Parameters.Add("@InventoryId", SqlDbType.Int);
updateCommand.Parameters.Add("@Quantity", SqlDbType.Int);
updateCommand.Parameters.Add("@OrderDetailId", SqlDbType.UniqueIdentifier);
updateCommand.Parameters.Add("@" + SyncSession.SyncLastReceivedAnchor, SqlDbType.Int);
updateCommand.Parameters.Add("@" + SyncSession.SyncRowCount, SqlDbType.Int).Direction = ParameterDirection.Output;
adapter.UpdateCommand = updateCommand;
//client to server delete
SqlCommand deleteCommand = new SqlCommand();
deleteCommand.CommandText = "spDeleteOrder";
deleteCommand.Parameters.Add("@OrderDetailId", SqlDbType.UniqueIdentifier);
deleteCommand.Parameters.Add("@" + SyncSession.SyncRowCount, SqlDbType.Int).Direction = ParameterDirection.Output;
adapter.DeleteCommand = deleteCommand;
return adapter;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -