Freigeben über


SyncAdapter-Klasse

Stellt eine Gruppe von Datenbefehlen dar, die dazu dienen, Schemainformationen abzurufen und Änderungen in der Serverdatenbank abzurufen und zu übernehmen.

Namespace: Microsoft.Synchronization.Data.Server
Assembly: Microsoft.Synchronization.Data.Server (in microsoft.synchronization.data.server.dll)

Syntax

'Declaration
Public Class SyncAdapter
'Usage
Dim instance As SyncAdapter
public class SyncAdapter
public ref class SyncAdapter
public class SyncAdapter
public class SyncAdapter

Hinweise

Der SyncAdapter fungiert als Brücke zwischen dem DbServerSyncProvider und der Serverdatenbank. Der Synchronisierungsadapter, der nach dem Vorbild des Datenadapters in ADO.NET entwickelt wurde, wird für jede zu synchronisierende Tabelle definiert. Er stellt dem Serversynchronisierungsanbieter die spezifischen Befehle bereit, die für die Interaktion mit der Serverdatenbank erforderlich sind. Dazu gehört z. B. der InsertCommand, der Einfügungen aus der Clientdatenbank für die Serverdatenbank übernimmt. Da die Synchronisierungsadapter das ADO.NETDbCommand-Objekt verwenden, können Sie jede von ADO.NET unterstützte Befehlsstruktur verwenden. Dies umfasst Inline-Transact-SQL, gespeicherte Prozeduren, Ansichten, Funktionen usw. Die Befehle benötigen lediglich ein einziges Ergebnis, das die Struktur und die zu übertragenden und zu übernehmenden Daten definiert.

Beispiel

Das folgende Codebeispiel erstellt ein SyncAdapter-Objekt für die Customer-Tabelle. Die Tabelle ist für die bidirektionale Synchronisierung mit Konflikterkennung konfiguriert. Daher sind alle verfügbaren Befehle angegeben. Die Befehle für das Auswählen von Änderungen auf dem Server sind Inline-Transact-SQL-Befehle. Die Befehle für das Übernehmen von Änderungen für den Server sind gespeicherte Prozeduren, die in Setupskripts für Datenbankanbieter - Themen zur Vorgehensweise definiert werden. Weitere Informationen zu Adapterbefehlen finden Sie unter Vorgehensweise: Angeben der Synchronisierungsart (Momentaufnahmen-, Nur-Download-, Nur-Upload- oder bidirektionale Synchronisierung). Eine Darstellung dieses Codes im Kontext eines vollständigen Beispiels finden Sie unter Vorgehensweise: Behandeln von Datenkonflikten und Fehlern.

SyncAdapter customerSyncAdapter = new SyncAdapter("Customer");

//This command is used if @sync_row_count returns
//0 when changes are applied to the server.
SqlCommand customerUpdateConflicts = new SqlCommand();
customerUpdateConflicts.CommandText =
    "SELECT CustomerId, CustomerName, SalesPerson, CustomerType " +
    "FROM Sales.Customer " +
    "WHERE CustomerId = @CustomerId";
customerUpdateConflicts.Parameters.Add("@CustomerId", SqlDbType.UniqueIdentifier);
customerUpdateConflicts.Connection = serverConn;
customerSyncAdapter.SelectConflictUpdatedRowsCommand = customerUpdateConflicts;

//This command is used if the server provider cannot find
//a row in the base table.
SqlCommand customerDeleteConflicts = new SqlCommand();
customerDeleteConflicts.CommandText =
    "SELECT CustomerId, CustomerName, SalesPerson, CustomerType " +
    "FROM Sales.Customer_Tombstone " +
    "WHERE CustomerId = @CustomerId";
customerDeleteConflicts.Parameters.Add("@CustomerId", SqlDbType.UniqueIdentifier);
customerDeleteConflicts.Connection = serverConn;
customerSyncAdapter.SelectConflictDeletedRowsCommand = customerDeleteConflicts;

//Select inserts from the server.
SqlCommand customerIncrInserts = new SqlCommand();
customerIncrInserts.CommandText =
    "SELECT CustomerId, CustomerName, SalesPerson, CustomerType " +
    "FROM Sales.Customer " +
    "WHERE (InsertTimestamp > @sync_last_received_anchor " +
    "AND InsertTimestamp <= @sync_new_received_anchor " +
    "AND InsertId <> @sync_client_id)";
customerIncrInserts.Parameters.Add("@" + SyncSession.SyncLastReceivedAnchor, SqlDbType.Timestamp);
customerIncrInserts.Parameters.Add("@" + SyncSession.SyncNewReceivedAnchor, SqlDbType.Timestamp);
customerIncrInserts.Parameters.Add("@" + SyncSession.SyncClientId, SqlDbType.UniqueIdentifier);
customerIncrInserts.Connection = serverConn;
customerSyncAdapter.SelectIncrementalInsertsCommand = customerIncrInserts;

//Apply inserts to the server.
SqlCommand customerInserts = new SqlCommand();
customerInserts.CommandType = CommandType.StoredProcedure;
customerInserts.CommandText = "usp_CustomerApplyInsert";
customerInserts.Parameters.Add("@" + SyncSession.SyncClientId, SqlDbType.UniqueIdentifier);
customerInserts.Parameters.Add("@" + SyncSession.SyncForceWrite, SqlDbType.Bit); 
customerInserts.Parameters.Add("@" + SyncSession.SyncRowCount, SqlDbType.Int).Direction = ParameterDirection.Output;
customerInserts.Parameters.Add("@CustomerId", SqlDbType.UniqueIdentifier);
customerInserts.Parameters.Add("@CustomerName", SqlDbType.NVarChar);
customerInserts.Parameters.Add("@SalesPerson", SqlDbType.NVarChar);
customerInserts.Parameters.Add("@CustomerType", SqlDbType.NVarChar);
customerInserts.Connection = serverConn;
customerSyncAdapter.InsertCommand = customerInserts;


//Select updates from the server.
SqlCommand customerIncrUpdates = new SqlCommand();
customerIncrUpdates.CommandText =
    "SELECT CustomerId, CustomerName, SalesPerson, CustomerType " +
    "FROM Sales.Customer " +
    "WHERE (UpdateTimestamp > @sync_last_received_anchor " +
    "AND UpdateTimestamp <= @sync_new_received_anchor " +
    "AND UpdateId <> @sync_client_id " +
    "AND NOT (InsertTimestamp > @sync_last_received_anchor " +
    "AND InsertId <> @sync_client_id))";
customerIncrUpdates.Parameters.Add("@" + SyncSession.SyncLastReceivedAnchor, SqlDbType.Timestamp);
customerIncrUpdates.Parameters.Add("@" + SyncSession.SyncNewReceivedAnchor, SqlDbType.Timestamp);
customerIncrUpdates.Parameters.Add("@" + SyncSession.SyncClientId, SqlDbType.UniqueIdentifier);
customerIncrUpdates.Connection = serverConn;
customerSyncAdapter.SelectIncrementalUpdatesCommand = customerIncrUpdates;

//Apply updates to the server.
SqlCommand customerUpdates = new SqlCommand();
customerUpdates.CommandType = CommandType.StoredProcedure;
customerUpdates.CommandText = "usp_CustomerApplyUpdate";
customerUpdates.Parameters.Add("@" + SyncSession.SyncLastReceivedAnchor, SqlDbType.Timestamp);
customerUpdates.Parameters.Add("@" + SyncSession.SyncClientId, SqlDbType.UniqueIdentifier);
customerUpdates.Parameters.Add("@" + SyncSession.SyncForceWrite, SqlDbType.Bit);            
customerUpdates.Parameters.Add("@" + SyncSession.SyncRowCount, SqlDbType.Int).Direction = ParameterDirection.Output;
customerUpdates.Parameters.Add("@CustomerId", SqlDbType.UniqueIdentifier);
customerUpdates.Parameters.Add("@CustomerName", SqlDbType.NVarChar);
customerUpdates.Parameters.Add("@SalesPerson", SqlDbType.NVarChar);
customerUpdates.Parameters.Add("@CustomerType", SqlDbType.NVarChar);
customerUpdates.Connection = serverConn;
customerSyncAdapter.UpdateCommand = customerUpdates;


//Select deletes from the server.
SqlCommand customerIncrDeletes = new SqlCommand();
customerIncrDeletes.CommandText =
    "SELECT CustomerId, CustomerName, SalesPerson, CustomerType " +
    "FROM Sales.Customer_Tombstone " +
    "WHERE (@sync_initialized = 1 " +
    "AND DeleteTimestamp > @sync_last_received_anchor " +
    "AND DeleteTimestamp <= @sync_new_received_anchor " +
    "AND DeleteId <> @sync_client_id)";
customerIncrDeletes.Parameters.Add("@" + SyncSession.SyncInitialized, SqlDbType.Bit);
customerIncrDeletes.Parameters.Add("@" + SyncSession.SyncLastReceivedAnchor, SqlDbType.Timestamp);
customerIncrDeletes.Parameters.Add("@" + SyncSession.SyncNewReceivedAnchor, SqlDbType.Timestamp);
customerIncrDeletes.Parameters.Add("@" + SyncSession.SyncClientId, SqlDbType.UniqueIdentifier);
customerIncrDeletes.Connection = serverConn;
customerSyncAdapter.SelectIncrementalDeletesCommand = customerIncrDeletes;

//Apply deletes to the server.
SqlCommand customerDeletes = new SqlCommand();
customerDeletes.CommandType = CommandType.StoredProcedure;
customerDeletes.CommandText = "usp_CustomerApplyDelete";
customerDeletes.Parameters.Add("@" + SyncSession.SyncLastReceivedAnchor, SqlDbType.Timestamp);
customerDeletes.Parameters.Add("@" + SyncSession.SyncClientId, SqlDbType.UniqueIdentifier);
customerDeletes.Parameters.Add("@" + SyncSession.SyncForceWrite, SqlDbType.Bit);           
customerDeletes.Parameters.Add("@" + SyncSession.SyncRowCount, SqlDbType.Int).Direction = ParameterDirection.Output;
customerDeletes.Parameters.Add("@CustomerId", SqlDbType.UniqueIdentifier);
customerDeletes.Connection = serverConn;
customerSyncAdapter.DeleteCommand = customerDeletes;


//Add the SyncAdapter to the server synchronization provider.
this.SyncAdapters.Add(customerSyncAdapter);
Dim customerSyncAdapter As New SyncAdapter("Customer")

'This command is used if @sync_row_count returns
'0 when changes are applied to the server.
Dim customerUpdateConflicts As New SqlCommand()
With customerUpdateConflicts
    .CommandText = _
        "SELECT CustomerId, CustomerName, SalesPerson, CustomerType " _
      & "FROM Sales.Customer " + "WHERE CustomerId = @CustomerId"
    .Parameters.Add("@CustomerId", SqlDbType.UniqueIdentifier)
    .Connection = serverConn
End With
customerSyncAdapter.SelectConflictUpdatedRowsCommand = customerUpdateConflicts

'This command is used if the server provider cannot find
'a row in the base table.
Dim customerDeleteConflicts As New SqlCommand()
With customerDeleteConflicts
    .CommandText = _
        "SELECT CustomerId, CustomerName, SalesPerson, CustomerType " _
      & "FROM Sales.Customer_Tombstone " + "WHERE CustomerId = @CustomerId"
    .Parameters.Add("@CustomerId", SqlDbType.UniqueIdentifier)
    .Connection = serverConn
End With
customerSyncAdapter.SelectConflictDeletedRowsCommand = customerDeleteConflicts

'Select inserts from the server.
Dim customerIncrInserts As New SqlCommand()
With customerIncrInserts
    .CommandText = _
        "SELECT CustomerId, CustomerName, SalesPerson, CustomerType " _
      & "FROM Sales.Customer " _
      & "WHERE (InsertTimestamp > @sync_last_received_anchor " _
      & "AND InsertTimestamp <= @sync_new_received_anchor " _
      & "AND InsertId <> @sync_client_id)"
    .Parameters.Add("@" + SyncSession.SyncLastReceivedAnchor, SqlDbType.Timestamp)
    .Parameters.Add("@" + SyncSession.SyncNewReceivedAnchor, SqlDbType.Timestamp)
    .Parameters.Add("@" + SyncSession.SyncClientId, SqlDbType.UniqueIdentifier)
    .Connection = serverConn
End With
customerSyncAdapter.SelectIncrementalInsertsCommand = customerIncrInserts

'Apply inserts to the server.
Dim customerInserts As New SqlCommand()
customerInserts.CommandType = CommandType.StoredProcedure
customerInserts.CommandText = "usp_CustomerApplyInsert"
customerInserts.Parameters.Add("@" + SyncSession.SyncClientId, SqlDbType.UniqueIdentifier)
customerInserts.Parameters.Add("@" + SyncSession.SyncForceWrite, SqlDbType.Bit)
customerInserts.Parameters.Add("@" + SyncSession.SyncRowCount, SqlDbType.Int).Direction = ParameterDirection.Output
customerInserts.Parameters.Add("@CustomerId", SqlDbType.UniqueIdentifier)
customerInserts.Parameters.Add("@CustomerName", SqlDbType.NVarChar)
customerInserts.Parameters.Add("@SalesPerson", SqlDbType.NVarChar)
customerInserts.Parameters.Add("@CustomerType", SqlDbType.NVarChar)
customerInserts.Connection = serverConn
customerSyncAdapter.InsertCommand = customerInserts


'Select updates from the server.
Dim customerIncrUpdates As New SqlCommand()
With customerIncrUpdates
    .CommandText = _
        "SELECT CustomerId, CustomerName, SalesPerson, CustomerType " _
      & "FROM Sales.Customer " _
      & "WHERE (UpdateTimestamp > @sync_last_received_anchor " _
      & "AND UpdateTimestamp <= @sync_new_received_anchor " _
      & "AND UpdateId <> @sync_client_id " _
      & "AND NOT (InsertTimestamp > @sync_last_received_anchor " _
      & "AND InsertId <> @sync_client_id))"
    .Parameters.Add("@" + SyncSession.SyncLastReceivedAnchor, SqlDbType.Timestamp)
    .Parameters.Add("@" + SyncSession.SyncNewReceivedAnchor, SqlDbType.Timestamp)
    .Parameters.Add("@" + SyncSession.SyncClientId, SqlDbType.UniqueIdentifier)
    .Connection = serverConn
End With
customerSyncAdapter.SelectIncrementalUpdatesCommand = customerIncrUpdates

'Apply updates to the server.
Dim customerUpdates As New SqlCommand()
customerUpdates.CommandType = CommandType.StoredProcedure
customerUpdates.CommandText = "usp_CustomerApplyUpdate"
customerUpdates.Parameters.Add("@" + SyncSession.SyncLastReceivedAnchor, SqlDbType.Timestamp)
customerUpdates.Parameters.Add("@" + SyncSession.SyncClientId, SqlDbType.UniqueIdentifier)
customerUpdates.Parameters.Add("@" + SyncSession.SyncForceWrite, SqlDbType.Bit)
customerUpdates.Parameters.Add("@" + SyncSession.SyncRowCount, SqlDbType.Int).Direction = ParameterDirection.Output
customerUpdates.Parameters.Add("@CustomerId", SqlDbType.UniqueIdentifier)
customerUpdates.Parameters.Add("@CustomerName", SqlDbType.NVarChar)
customerUpdates.Parameters.Add("@SalesPerson", SqlDbType.NVarChar)
customerUpdates.Parameters.Add("@CustomerType", SqlDbType.NVarChar)
customerUpdates.Connection = serverConn
customerSyncAdapter.UpdateCommand = customerUpdates


'Select deletes from the server.
Dim customerIncrDeletes As New SqlCommand()
With customerIncrDeletes
    .CommandText = _
        "SELECT CustomerId, CustomerName, SalesPerson, CustomerType " _
      & "FROM Sales.Customer_Tombstone " _
      & "WHERE (@sync_initialized = 1 " _
      & "AND DeleteTimestamp > @sync_last_received_anchor " _
      & "AND DeleteTimestamp <= @sync_new_received_anchor " _
      & "AND DeleteId <> @sync_client_id)"
    .Parameters.Add("@" + SyncSession.SyncInitialized, SqlDbType.Bit)
    .Parameters.Add("@" + SyncSession.SyncLastReceivedAnchor, SqlDbType.Timestamp)
    .Parameters.Add("@" + SyncSession.SyncNewReceivedAnchor, SqlDbType.Timestamp)
    .Parameters.Add("@" + SyncSession.SyncClientId, SqlDbType.UniqueIdentifier)
    .Connection = serverConn
End With
customerSyncAdapter.SelectIncrementalDeletesCommand = customerIncrDeletes

'Apply deletes to the server.
Dim customerDeletes As New SqlCommand()
customerDeletes.CommandType = CommandType.StoredProcedure
customerDeletes.CommandText = "usp_CustomerApplyDelete"
customerDeletes.Parameters.Add("@" + SyncSession.SyncLastReceivedAnchor, SqlDbType.Timestamp)
customerDeletes.Parameters.Add("@" + SyncSession.SyncClientId, SqlDbType.UniqueIdentifier)
customerDeletes.Parameters.Add("@" + SyncSession.SyncForceWrite, SqlDbType.Bit)
customerDeletes.Parameters.Add("@" + SyncSession.SyncRowCount, SqlDbType.Int).Direction = ParameterDirection.Output
customerDeletes.Parameters.Add("@CustomerId", SqlDbType.UniqueIdentifier)
customerDeletes.Connection = serverConn
customerSyncAdapter.DeleteCommand = customerDeletes


'Add the SyncAdapter to the server synchronization provider.
Me.SyncAdapters.Add(customerSyncAdapter)

Vererbungshierarchie

System.Object
  Microsoft.Synchronization.Data.Server.SyncAdapter

Threadsicherheit

Alle öffentlichen statischen (Shared in Visual Basic) Member dieses Typs sind threadsicher. Bei Instanzmembern ist die Threadsicherheit nicht gewährleistet.

Siehe auch

Verweis

SyncAdapter-Member
Microsoft.Synchronization.Data.Server-Namespace