Last active
August 29, 2015 14:21
-
-
Save piers7/91141f39715a2ec133e5 to your computer and use it in GitHub Desktop.
Interpreting SQL server CDC LSNs in C# / .net
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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