SQ L Connection
SQ L Connection
Remarks
The ConnectionString is similar to an OLE DB connection string, but is not identical. Unlike OLE DB or
ADO, the connection string that is returned is the same as the user-set ConnectionString, minus security
information if the Persist Security Info value is set to false (default). The .NET Framework Data Provider
for SQL Server does not persist or return the password in a connection string unless you set Persist
Security Info to true .
You can use the ConnectionString property to connect to a database. The following example illustrates a
typical connection string.
Use the new SqlConnectionStringBuilder to construct valid connection strings at run time. For more
information, see Connection String Builders.
The ConnectionString property can be set only when the connection is closed. Many of the connection
string values have corresponding read-only properties. When the connection string is set, these
properties are updated, except when an error is detected. In this case, none of the properties are
updated. SqlConnection properties return only those settings that are contained in the ConnectionString.
To connect to a local computer, specify "(local)" for the server. If a server name is not specified, a
connection will be attempted to the default instance on the local computer.
Resetting the ConnectionString on a closed connection resets all connection string values (and related
properties) including the password. For example, if you set a connection string that includes "Database=
AdventureWorks", and then reset the connection string to "Data Source=myserver;Integrated
Security=true", the Database property is no longer set to "AdventureWorks".
The connection string is parsed immediately after being set. If errors in syntax are found when parsing, a
runtime exception, such as ArgumentException, is generated. Other errors can be found only when an
attempt is made to open the connection.
The basic format of a connection string includes a series of keyword/value pairs separated by
semicolons. The equal sign (=) connects each keyword and its value. To include values that contain a
semicolon, single-quote character, or double-quote character, the value must be enclosed in double
quotation marks. If the value contains both a semicolon and a double-quote character, the value can be
f f
https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnection.connectionstring?view=dotnet-plat-ext-7.0 1/12
2023/7/24 下午4:43 SqlConnection.ConnectionString Property (System.Data.SqlClient) | Microsoft Learn
enclosed in single quotation marks. The single quotation mark is also useful if the value starts with a
double-quote character. Conversely, the double quotation mark can be used if the value starts with a
single quotation mark. If the value contains both single-quote and double-quote characters, the
quotation mark character used to enclose the value must be doubled every time it occurs within the
value.
To include preceding or trailing spaces in the string value, the value must be enclosed in either single
quotation marks or double quotation marks. Any leading or trailing spaces around integer, Boolean, or
enumerated values are ignored, even if enclosed in quotation marks. However, spaces within a string
literal keyword or value are preserved. Single or double quotation marks may be used within a
connection string without using delimiters (for example, Data Source= my'Server or Data Source=
my"Server), unless a quotation mark character is the first or last character in the value.
The following table lists the valid names for keyword values within the ConnectionString.
Application Name N/A The name of the application, or '.NET SQLClient Data Provider' if no
application name is provided.
ApplicationIntent ReadWrite Declares the application workload type when connecting to a server.
Possible values are ReadOnly and ReadWrite . For example:
ApplicationIntent=ReadOnly
Asynchronous Processing 'false' When true , enables asynchronous operation support. Recognized values
are true , false , yes , and no .
-or-
This property is ignored beginning in .NET Framework 4.5. For more
Async information about SqlClient support for asynchronous programming, see
Asynchronous Programming.
AttachDBFilename N/A The name of the primary database file, including the full path name of an
attachable database. AttachDBFilename is only supported for primary data
-or- files with an .mdf extension.
Extended Properties If the value of the AttachDBFileName key is specified in the connection
string, the database is attached and becomes the default database for the
-or- connection.
Initial File Name If this key is not specified and if the database was previously attached, the
database will not be reattached. The previously attached database will be
used as the default database for the connection.
https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnection.connectionstring?view=dotnet-plat-ext-7.0 2/12
2023/7/24 下午4:43 SqlConnection.ConnectionString Property (System.Data.SqlClient) | Microsoft Learn
If this key is specified together with the AttachDBFileName key, the value
of this key will be used as the alias. However, if the name is already used in
another attached database, the connection will fail.
The database name must be specified with the keyword 'database' (or one
of its aliases) as in the following:
"AttachDbFileName=|DataDirectory|\data\YourDB.mdf;integrated
security=true;database=YourDatabase"
An error will be generated if a log file exists in the same directory as the
data file and the 'database' keyword is used when attaching the primary
data file. In this case, remove the log file. Once the database is attached, a
new log file will be automatically generated based on the physical path.
Authentication N/A The authentication method used for Connecting to SQL Database By Using
Azure Active Directory Authentication.
Column Encryption Setting N/A Enables or disables Always Encrypted functionality for the connection.
Connect Timeout 15 The length of time (in seconds) to wait for a connection to the server
before terminating the attempt and generating an error.
-or-
Valid values are greater than or equal to 0 and less than or equal to
Connection Timeout 2147483647.
-or- When opening a connection to a Azure SQL Database, set the connection
timeout to 30 seconds.
Timeout
Connection Lifetime 0 When a connection is returned to the pool, its creation time is compared
with the current time, and the connection is destroyed if that time span (in
-or- seconds) exceeds the value specified by Connection Lifetime . This is useful
in clustered configurations to force load balancing between a running
Load Balance Timeout server and a server just brought online.
ConnectRetryCount 1 Controls the number of reconnection attempts after the client identifies an
idle connection failure. Valid values are 0 to 255. The default is 1. 0 means
do not attempt to reconnect (disable connection resiliency).
Context Connection 'false' true if an in-process connection to SQL Server should be made.
Current Language N/A Sets the language used for database server warning or error messages.
Language
Data Source N/A The name or network address of the instance of SQL Server to which to
connect. The port number can be specified after the server name:
-or-
server=tcp:servername, portnumber
Server
When specifying a local instance, always use (local). To force a protocol,
-or- add one of the following prefixes:
-or- Beginning in .NET Framework 4.5, you can also connect to a LocalDB
database as follows:
Addr
server=(localdb)\\myInstance
-or-
For more information about LocalDB, see SqlClient Support for LocalDB.
Network Address
Data Source must use the TCP format or the Named Pipes format.
The TCP format must start with the prefix "tcp:" and is followed by the
database instance, as specified by a host name and an instance name. This
format is not applicable when connecting to Azure SQL Database. TCP is
automatically selected for connections to Azure SQL Database when no
protocol is specified.
- NetBIOSName
- IPv4Address
- IPv6Address
h i i d l i l b
https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnection.connectionstring?view=dotnet-plat-ext-7.0 4/12
2023/7/24 下午4:43 SqlConnection.ConnectionString Property (System.Data.SqlClient) | Microsoft Learn
The instance name is used to resolve to a particular TCP/IP port number on
which a database instance is hosted. Alternatively, specifying a TCP/IP port
number directly is also allowed. If both instance name and port number are
The Named Pipes format MUST start with the prefix "np:" and is followed
by a named pipe name.
- NetBIOSName
- IPv4Address
- IPv6Address
The pipe name is used to identify the database instance to which the .NET
Framework application will be connected.
If the value of the Network key is specified, the prefixes "tcp:" and "np:"
should not be specified. Note: You can force the use of TCP instead of
shared memory, either by prefixing tcp: to the server name in the
connection string, or by using localhost.
Encrypt 'false' When true , SQL Server uses SSL encryption for all data sent between the
client and server if the server has a certificate installed. Recognized values
are true , false , yes , and no . For more information, see Connection String
Syntax.
Enlist 'true' true indicates that the SQL Server connection pooler automatically enlists
the connection in the creation thread's current transaction context.
Failover Partner N/A The name of the failover partner server where database mirroring is
configured.
If the value of this key is "", then Initial Catalog must be present, and its
value must not be "".
If you specify a failover partner but the failover partner server is not
configured for database mirroring and the primary server (specified with
the Server keyword) is not available, then the connection will fail.
If you specify a failover partner and the primary server is not configured for
database mirroring, the connection to the primary server (specified with
the Server keyword) will succeed if the primary server is available.
https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnection.connectionstring?view=dotnet-plat-ext-7.0 5/12
2023/7/24 下午4:43 SqlConnection.ConnectionString Property (System.Data.SqlClient) | Microsoft Learn
Database
Integrated Security 'false' When false , User ID and Password are specified in the connection. When
true , the current Windows account credentials are used for authentication.
-or-
Recognized values are true , false , yes , no , and sspi (strongly
Trusted_Connection recommended), which is equivalent to true .
If User ID and Password are specified and Integrated Security is set to true,
the User ID and Password will be ignored and Integrated Security will be
used.
Max Pool Size 100 The maximum number of connections that are allowed in the pool.
Valid values are greater than or equal to 1. Values that are less than Min
Pool Size generate an error.
Min Pool Size 0 The minimum number of connections that are allowed in the pool.
Valid values are greater than or equal to 0. Zero (0) in this field means no
minimum connections are initially opened.
Values that are greater than Max Pool Size generate an error.
MultipleActiveResultSets 'false' When true , an application can maintain multiple active result sets (MARS).
When false , an application must process or cancel all result sets from one
batch before it can execute any other batch on that connection.
MultiSubnetFailover=True
The default is False . For more information about SqlClient's support for
Always On AGs, see SqlClient Support for High Availability, Disaster
Recovery.
Network Library N/A The network library used to establish a connection to an instance of SQL
Server. Supported values include:
-or-
dbnmpntw (Named Pipes)
Network
https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnection.connectionstring?view=dotnet-plat-ext-7.0 6/12
2023/7/24 下午4:43 SqlConnection.ConnectionString Property (System.Data.SqlClient) | Microsoft Learn
dbmsspxn (IPX/SPX)
dbmssocn (TCP/IP)
Packet Size 8000 Size in bytes of the network packets used to communicate with an instance
of SQL Server.
The packet size can be greater than or equal to 512 and less than or equal
to 32768.
Password N/A The password for the SQL Server account logging on. Not recommended.
To maintain a high level of security, we strongly recommend that you use
-or- the Integrated Security or Trusted_Connection keyword instead.
SqlCredential is a more secure way to specify credentials for a connection
PWD that uses SQL Server Authentication.
Persist Security Info 'false' When set to false or no (strongly recommended), security-sensitive
information, such as the password, is not returned as part of the
-or- connection if the connection is open or has ever been in an open state.
Resetting the connection string resets all connection string values
PersistSecurityInfo including the password. Recognized values are true , false , yes , and no .
PoolBlockingPeriod Auto Sets the blocking period behavior for a connection pool. See
PoolBlockingPeriod property for details.
Pooling 'true' When the value of this key is set to true, any newly created connection will
be added to the pool when closed by the application. In a next attempt to
open the same connection, that connection will be drawn from the pool.
Connections are considered the same if they have the same connection
string. Different connections have different connection strings.
Unbind transaction.
If the system ends the transaction (in the scope of a using block) before
the last command completes, it will throw InvalidOperationException.
TransparentNetworkIPResolution See When the value of this key is set to true , the application is required to
description. retrieve all IP addresses for a particular DNS entry and attempt to connect
with the first one in the list. If the connection is not established within 0.5
seconds, the application will try to connect to all others in parallel. When
the first answers, the application will establish the connection with the
respondent IP address.
false when:
.database.chinacloudapi.cn
.database.usgovcloudapi.net
.database.cloudapi.de
.database.windows.net
https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnection.connectionstring?view=dotnet-plat-ext-7.0 8/12
2023/7/24 下午4:43 SqlConnection.ConnectionString Property (System.Data.SqlClient) | Microsoft Learn
Authentication is 'Active Directory Password' or 'Active Directory
Integrated'
true in all other cases.
Type System Version N/A A string value that indicates the type system the application expects. The
functionality available to a client application is dependent on the version of
SQL Server and the compatibility level of the database. Explicitly setting the
type system version that the client application was written for avoids
potential problems that could cause an application to break if a different
version of SQL Server is used. Note: The type system version cannot be set
for common language runtime (CLR) code executing in-process in SQL
Server. For more information, see SQL Server Common Language Runtime
Integration.
Type System Version=SQL Server 2012; specifies that the application will
require version 11.0.0.0 of Microsoft.SqlServer.Types.dll. The other Type
System Version settings will require version 10.0.0.0 of
Microsoft.SqlServer.Types.dll.
User ID N/A The SQL Server login account. Not recommended. To maintain a high level
of security, we strongly recommend that you use the Integrated Security
-or- or Trusted_Connection keywords instead. SqlCredential is a more secure
way to specify credentials for a connection that uses SQL Server
UID Authentication.
User
User Instance 'false' A value that indicates whether to redirect the connection from the default
SQL Server Express instance to a runtime-initiated instance running under
the account of the caller.
Workstation ID The local The name of the workstation connecting to SQL Server.
computer
-or- name The ID must be 128 characters or less.
WSID
https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnection.connectionstring?view=dotnet-plat-ext-7.0 9/12
2023/7/24 下午4:43 SqlConnection.ConnectionString Property (System.Data.SqlClient) | Microsoft Learn
The following list contains the valid names for connection pooling values within the ConnectionString.
For more information, see SQL Server Connection Pooling (ADO.NET).
Enlist
Pooling
When you are setting keyword or connection pooling values that require a Boolean value, you can use
'yes' instead of 'true', and 'no' instead of 'false'. Integer values are represented as strings.
7 Note
The .NET Framework Data Provider for SQL Server uses its own protocol to communicate with SQL
Server. Therefore, it does not support the use of an ODBC data source name (DSN) when
connecting to SQL Server because it does not add an ODBC layer.
7 Note
Universal data link (UDL) files are not supported for the .NET Framework Data Provider for SQL
Server.
U Caution
In this release, the application should use caution when constructing a connection string based on
user input (for example when retrieving user ID and password information from a dialog box, and
appending it to the connection string). The application should make sure that a user cannot embed
additional connection string parameters in these values (for example, entering a password as
"validpassword;database=somedb" in an attempt to attach to a different database). If you need to
construct connection strings based on user input, use the new SqlConnectionStringBuilder, which
validates the connection string and helps to eliminate this problem. See Connection String Builders
for more information.
Applies to
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Xamarin.iOS 10.8
https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnection.connectionstring?view=dotnet-plat-ext-7.0 10/12
2023/7/24 下午4:43 SqlConnection.ConnectionString Property (System.Data.SqlClient) | Microsoft Learn
Xamarin.Mac 3.0
See also
ADO.NET Managed Providers and DataSet Developer Center
Connection Strings in ADO.NET
SQL Server Connection Pooling (ADO.NET)
Connecting to a Data Source in ADO.NET
ADO.NET Overview
https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnection.connectionstring?view=dotnet-plat-ext-7.0 11/12
2023/7/24 下午4:43 SqlConnection.ConnectionString Property (System.Data.SqlClient) | Microsoft Learn
https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnection.connectionstring?view=dotnet-plat-ext-7.0 12/12