Skip to content

Instantly share code, notes, and snippets.

@piers7
Last active August 29, 2015 14:21
Show Gist options
  • Save piers7/91141f39715a2ec133e5 to your computer and use it in GitHub Desktop.
Save piers7/91141f39715a2ec133e5 to your computer and use it in GitHub Desktop.
Interpreting SQL server CDC LSNs in C# / .net
// https://gist.github.com/piers7/91141f39715a2ec133e5
// Example of how to interpret SQL server CDC LSNs in C# / .Net
// This is required when polling a server for updates in order to determine
// if a previously stored LSN is still valid (ie > min LSN available)
// Requires .Net 4 (or equivilent BigInteger implementation)
// Sample is a Linqpad script, but you get the idea
// NB: That SQL uses big-endian representation for it's LSNs is not
// (as best I know) something they guarantee not to change
Connection.Open();
var command = Connection.CreateCommand();
command.CommandText = @"select sys.fn_cdc_get_max_lsn() as maxLsn";
var bytes = (byte[])command.ExecuteScalar();
// Uncomment this bit if you are skeptical
// var bytes = new byte[10] { 0,0,0,0,0,0,0,0,0,255 };
// dump bytes as hex
var hexString = string.Join(" ", bytes.Select(b => b.ToString("X2")))
.Dump("Hex String");
if(BitConverter.IsLittleEndian)
bytes = bytes.Reverse().ToArray();
var bigInt = new BigInteger(bytes)
// dump Integer representation
.Dump("Big Integer")
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment