Skip to content

TDS versions and encryption

SQL Server speaks TDS, the Tabular Data Stream protocol, described in Microsoft's MS-TDS specification. tdslite's implementation follows revision 33 of it. Two version numbers matter to you: the one tdslite logs in with, and the one its encrypted connections use.

Logging in as TDS 7.1

A client tells SQL Server which TDS version it speaks when it logs in, and the server shapes its answers to fit. tdslite logs in as TDS 7.1, the version of SQL Server 2000.

That's a deliberate trade. TDS 7.1 covers the classic data types, and its messages are simple to parse, which keeps the library small. For the types that came later, SQL Server does the work: it converts them into types a 7.1 client understands before sending them. A datetime2 arrives as a string, a varchar(max) as text. The data types reference lists the conversions.

What a 7.1 client can't have is the newer protocol features. One of them shows up with encryption; see transactions over TLS below.

Encryption before TDS 8.0

Older TDS versions encrypt by switching to TLS in the middle of the conversation: the client and server first exchange a plaintext "prelogin" packet, then do a TLS handshake wrapped inside TDS packets, and then continue. It works, but the handshake has to be tunneled through TDS framing, which a TLS library can't do on its own: every client needs custom glue between TDS and TLS.

tdslite doesn't implement this mode. Its plaintext backends send everything, the login included, unencrypted, so use them only on networks you trust. A server configured to force encryption refuses them at login.

TDS 8.0 strict mode

SQL Server 2022 introduced TDS 8.0 and strict encryption, which turns the order around: the TLS handshake comes first, right after the TCP connection, exactly as in HTTPS. Only then does TDS start, inside the encrypted channel.

For a small client, that's a much better fit:

  • The TLS layer is an ordinary TLS client: esp-tls, OpenSSL, or a board's WiFiClientSecure, with no TDS-specific glue.
  • Nothing, not even the prelogin, travels in plaintext.
  • The server's certificate is checked like any HTTPS server's: it must be signed by a CA you trust and name the host you connect to.

So every encrypted tdslite connection is a strict one. The price is the server requirement: strict mode needs SQL Server 2025, SQL Server 2022 on Windows, or Azure SQL, with a proper certificate.

Inside the encrypted channel, tdslite still logs in as TDS 7.1.

One packet per record

Under strict mode, SQL Server expects each TDS packet to arrive in a single TLS record, and drops the connection when one is split. That's why the packet size and the TLS client's record size have to agree, and why tdslite's TLS support refuses a packet larger than a record instead of sending it in pieces. See Connect with TLS.

Transactions over TLS

When a transaction starts, SQL Server gives it a descriptor, and from then on expects each request in the session to carry it. On a plaintext TDS 7.1 connection, the server doesn't insist on it. On a strict connection, it does, but it doesn't send the descriptor to a client that logged in as TDS 7.1.

The result: after a BEGIN TRAN sent on its own, every later request, COMMIT and ROLLBACK included, fails with error 3989 ("New request is not allowed to start because it should come with valid transaction descriptor"), until the driver reconnects.

A transaction that begins and ends within one request has no "later request", so it isn't affected. That's why, on TLS connections, a transaction must start and end within one execute_query, or one stored procedure.