Skip to content

Connection parameters

driver.connect(params) takes one of three parameter structs. They have the same fields and differ in the string type:

Type Strings are Use it
decltype(driver)::connection_parameters const char * / tdsl::string_view Most of the time
decltype(driver)::progmem_connection_parameters TDSL_PMEMSTR("..."), kept in flash On AVR boards, to save RAM
decltype(driver)::wconnection_parameters tdsl::wstring_view (UTF-16) For non-ASCII names or passwords

Fields

Field Type Default Description
server_name string (required) Host name or IP address of the server
port uint16_t 1433 TCP port of the server
user_name string empty SQL Server login name
password string empty Password of the login
db_name string empty Database to use after logging in. Empty: the login's default database
app_name string empty Application name, shown in SQL Server's session list
client_name string empty Client machine name, shown in SQL Server's session list
library_name string "tdslite" Client library name sent to the server
packet_size uint32_t 4096 TDS packet size to ask the server for, 512 – 32767
conn_retry_count uint32_t 10 Connection attempts before connect gives up, at most 65535. 0 makes no attempt: connect fails at once with connection_failed
conn_retry_delay_ms uint32_t 3000 Milliseconds between connection attempts, at most 65535

connect checks the parameters before connecting, and returns an error code when one is out of range.

The login struct has more fields (client_program_version, client_pid, option flags, ...) that map directly to the TDS login packet. Leave them at their defaults unless you know the protocol; see the C++ API.

Packet size

The packet size is the size of the TDS packets that client and server exchange. The server may grant a different size than the one asked for. The login itself is sent in packets of at most 4096 bytes.

  • On small boards, use 512 with a network buffer somewhat larger than that.
  • On TLS connections, the packet size must not exceed what one TLS record can hold; see Connect with TLS.

How long connect blocks

connect doesn't return until it's logged in or its last attempt has failed. The worst case is about

conn_retry_count × (time of one failed attempt + conn_retry_delay_ms)

One failed attempt takes as long as the network library takes to give up:

Situation Measured time of one attempt
Host name doesn't resolve ~2 s (ESP8266)
Server refuses the connection (port closed) ~12 s for 2 attempts (ESP8266)
Server unreachable (no reply at all) ~21 s (Uno WiFi Rev. 2), the library's connect timeout

With the defaults (10 attempts, 3 s apart) and an unreachable server, that is about 4 minutes, during which the sketch does nothing else. To keep a sketch responsive, use few attempts (e.g. conn_retry_count = 1) and retry from loop() instead; on the socket backends, set a shorter connect timeout with set_socket_timeouts.