04. Connection string for SQL Server

This article explains about connectint string for SQL Server instance.

What is a connection string

We have to specify a connection string for connecting to SQL Server. The available connection strings vary depending on the driver used for the connection.

As an example, the following elements can be specified in a connection string:
Server Name : Specifies the name or network address of the SQL Server instance.
Database: Specifies the name of the database to connect to.
User Name: Specifies the username for authentication (used for SQL Server authentication).
Password: Specifies the password for authentication (used for SQL Server authentication).
Encrypt Connection: Enables or disables encryption for the connection
Trusted_Connection: Indicates whether a trusted connection should be use
Connection Timeout: Sets the number of seconds to wait before the connection times out.



Details of connection strings for each driver can be found on the following website.

SQL Server connection strings - ConnectionStrings.com
Connection strings for SQL Server. Connect using Microsoft.Data.SqlClient, SqlConnection, MSOLEDBSQL, SQLNCLI11 OLEDB, S...


Connect to the default instance using sqlcmd

To connect to the default instance, specify the hostname or IP address as the server name.
The following is an example of a connection.

// Connection using Windows Authentication
sqlcmd -S serverA -E

// Connection using SQL Server Authentication
sqlcmd -S ServerA -U userName - P password

// Connection using Windows Authentication with a specified timeout
sqlcmd -S serverA -E -l 30


sqlcmd utility - SQL Server
The sqlcmd utility lets you enter Transact-SQL statements, system procedures, and script files using different modes, us...


Connect to the named instance using sqlcmd

When installing SQL Server, you can assign a name to the instance. This can be specified during installation but cannot be changed afterward.
In the following example, the instance is named “namedInstance”.


An example of a connection string for connecting to the above instance is as follows. We need to specify [hostname\instance name] or [IP address\instance name] as the server name.

// Connection using Windows Authentication
sqlcmd -S serverA\namedInstance -E

// Connection using SQL Server Authentication
sqlcmd -S serverA\namedInstance -U userName - P password

// Connection using Windows Authentication with a specified timeout
sqlcmd -S serverA\namedInstance -E -l 30