Skip to content

Commit

Permalink
updates readme with examples (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
runeanielsen authored Dec 29, 2021
1 parent 574d2e7 commit 13d8109
Showing 1 changed file with 128 additions and 0 deletions.
128 changes: 128 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,134 @@ You can get the NuGet package [here.](https://www.nuget.org/packages/MsSqlCdc)

The MS-SQL change data capture library, simplifies using MS-SQL-CDC by providing a simplified API to interact with the SQL functions for MS-SQL-CDC. The API has intentionally been made very simplistic, because the use-cases of the consumers of the library can vary a lot.

### Examples

Usage examples can be found under the [example folder](https://github.com/DAXGRID/mssql-cdc/tree/master/examples).

## API

### GetIsBitSet

Indicates whether a captured column has been updated by checking whether its ordinal position is set within a provided bitmask.

```c#
var connectionString = GetConnectionString();
var connection = new SqlConnection(connectionString);
await connection.OpenAsync();
var columnOrdinal = await Cdc.GetColumnOrdinal(connection, "dbo_Employee", "Salary");
var isBitSet = await Cdc.IsBitSet(connection, columnOrdinal, "my_update_mask");
```

### Has column changed

Identifies whether the specified update mask indicates that the specified colum has been updated in the associated change row.

```c#
var connectionString = GetConnectionString();
var connection = new SqlConnection(connectionString);
await connection.OpenAsync();
await Cdc.HasColumnChanged(connection, "dbo_Employee", "Salary", "my_update_mask");
```

### Get column ordinal

Get the column ordinal of the specified column as it appears in the change table associated with the specified capture instance.

```c#
var connectionString = GetConnectionString();
var connection = new SqlConnection(connectionString);
await connection.OpenAsync();
var columnOrdinal = await Cdc.GetColumnOrdinal(connection, "dbo_Employee", "Salary");
```

### Map time to LSN

Map the log sequence number (LSN) value from the start_lsn column in the cdc.lsn_time_mapping system table for the specified time.

```c#
var connectionString = GetConnectionString();
var connection = new SqlConnection(connectionString);
await connection.OpenAsync();
var lsn = await Cdc.MapTimeToLsn(connection, DateTime.UtcNow, RelationalOperator.LargestLessThan);
```

### Map LSN to time

Map date and time value from the tran_end_time column in the cdc.lsn_time_mapping system table for the specified log sequence number (LSN). You can use this function to systematically map LSN ranges to date ranges in a change table.

```c#
var connectionString = GetConnectionString();
var connection = new SqlConnection(connectionString);
await connection.OpenAsync();
var time = await Cdc.MapLsnToTime(connection, 120000);
```

### Get min LSN

Get the start_lsn column value for the specified capture instance from the cdc.change_tables system table. This value represents the low endpoint of the validity interval for the capture instance.

```c#
var connectionString = GetConnectionString();
var connection = new SqlConnection(connectionString);
await connection.OpenAsync();
var minLsn = await Cdc.GetMinLsn(connection, "dbo_Employee");
```

### Get max LSN

Get the maximum log sequence number (LSN) from the start_lsn column in the cdc.lsn_time_mapping system table. You can use this function to return the high endpoint of the change data capture timeline for any capture instance.

```c#
var connectionString = GetConnectionString();
var connection = new SqlConnection(connectionString);
await connection.OpenAsync();
var maxLsn = await Cdc.GetMaxLsn(connection);
```

### Get previous LSN

Get the previous log sequence number (LSN) in the sequence based upon the specified LSN.

```c#
var connectionString = GetConnectionString();
var connection = new SqlConnection(connectionString);
await connection.OpenAsync();
var previousLsn = await Cdc.GetPreviousLsn(connection, 120000);
```

### Get next LSN

Get the next log sequence number (LSN) in the sequence based upon the specified LSN.

```c#
var connectionString = GetConnectionString();
var connection = new SqlConnection(connectionString);
await connection.OpenAsync();
var nextLsn = await Cdc.GetNextLsn(connection, 120000);
```

### Get net changes

Get one net change row for each source row changed within the specified Log Sequence Numbers (LSN) range.

```c#
var connectionString = GetConnectionString();
var connection = new SqlConnection(connectionString);
await connection.OpenAsync();
var netChanges = await Cdc.GetNetChanges(connection, "dbo_Employee", 120000, 120020);
```

### Get all changes

Get one row for each change applied to the source table within the specified log sequence number (LSN) range. If a source row had multiple changes during the interval, each change is represented in the returned result set.

```c#
var connectionString = GetConnectionString();
var connection = new SqlConnection(connectionString);
await connection.OpenAsync();
var allChanges = await Cdc.GetAllChanges(connection, "dbo_Employee", 120000, 120020);
```

## Setup CDC on MS-SQL Server

First make sure that you've enabled the MS-SQL agent, otherwise changes won't be captured in the CDC tables.
Expand Down

0 comments on commit 13d8109

Please sign in to comment.