How to connect to SQL Server using TCL
It only takes a few lines of TCL code to connect your program to SQL Server. The key to a successful connection is having the right connection string.
Visit ConnectionStrings.com – Forgot that connection string? Get it here! Replace the “my_________” entries in the connection string with the settings for your server. We’re using SQL Server 2019. The connection strings we can use according to ConnectoinStrings.com are pictured below:

The MagicSplat and ActiveTcl distributions include the tdbc::odbc package we need to create a connection to SQL Server. Connecting to SQL Server only requires two lines of TCL code:
package require tdbc::odbc
tdbc::odbc::connection create dbx "Driver={SQL Server};Server={LAPTOP-S2K3BNR7};Trusted_Connection=Yes;Database=MicroQueue;"
The above code creates or opens a connection named “dbx” that is actually a TCL command to perform other actions on the database.
The following code uses tdbc::odbc to display driver and data source information before creating a connection to SQL Server and displaying the current connection configuration.
package require tdbc::odbc
# Dictionary of available drivers
dict for {Driver Detail} [tdbc::odbc::drivers] {
puts "Driver: $Driver\n\t $Detail"
}
# Dictionary of available datasources
# datasources options include: -users or -system
dict for {DataSource Details} [tdbc::odbc::datasources] {
puts "DataSource: $DataSource\n\t$Details"
}
# Create connection
tdbc::odbc::connection create dbx "Driver={SQL Server};Server={LAPTOP-S2K3BNR7};Trusted_Connection=Yes;Database=MicroQueue;"
# Show connection configuration
puts -nonewline "dbx configure: "
puts [dbx configure]
# Configuration options include:
# - encoding NAME
# - use TCL encoding command
# - isolation ISOLATION
# - readuncommitted,
# - readcommitted,
# - repeatableread, or
# - serializable
# - timeout MILLISECS
# - 0 = default
# - readonly BOOLEAN
# - True or 1 = read only
dbx close
When we copy the above code and paste it into an open TkCon window to run it, the following output is displayed on the console.

Why TCL?
TCL is one of our “go-to” languages for rapid prototyping applications. In other cases, it serves as the solution we need for short-term applications. TCL is an easy language to learn while being capable of providing unique features This
TCL carries a small footprint and is relatively easy to learn. If you’re interested in learning TCL, we highly recommend visiting http://www.magicsplat.com and purchase a copy of Ashok P. Nadkarni’s book titled The TCL Programming Language – A Comprehensive Guide.
Where to get TCL
ActiveTCL from ActiveState also includes the powerful KOMODO IDE when you create a free account! We use the Komodo IDE exclusively for our TCL projects. There was a time when you had to purchase the Komodo IDE but ActiveState has since made it freely available to those with an account.
- ActiveState also allows you to create custom configurations for your language download.
- Free KOMODO IDE when you create and account.
Magicsplat TCL Version 8.6.11 is now available via SOURCEFORGE.net.
Learning TCL
We highly recommend “The TCL Programming Language – A Comprehensive Guide” (Gum Road), by Ashok P. Nadkarni. This book offers more insights than you’ll find on the language website itself and serves as a valuable reference in our library.
Practical Programming in Tcl and Tk – Fourth Edition, by Brent B. Welch, Ken Jones, with Jeffrey Hobbs, (Prentice Hall) is an invaluable reference for TCL and Tk programming. This book is a “hands-on” reference that is always on hand when working with TCL/Tk.
TCL will surprise you!
The power of TCL remains an untapped resource for many programmers. Are you one of them? There’s more to TCL than meets the eye.
Related Articles and Resources
Database Connectivity – SQL Server
tdbc connection object – tdbc::connection
tdbc statement object – tdbc::statement
tdbc result set – tdbc::resultset