Fixed exception with parameter number mismatch in WriteRecords

Also added a unit test to be sure it works...