dotConnect for SQL Server Documentation
Devart.Data.SqlServer Namespace / SqlConnection Class / ConnectionString Property
Example

In This Topic
    ConnectionString Property (SqlConnection)
    In This Topic
    Gets or sets the string used to open a SQL Server connection.
    Syntax
    'Declaration
     
    Public Overrides Property ConnectionString As String
    public override string ConnectionString {get; set;}

    Property Value

    The connection string that includes the parameters needed to establish the initial connection. The default value is an empty string ("").
    Remarks
    When ConnectionString property is assigned a value, connection closes. Many of the connection string values have corresponding properties. When the connection string is set, all of 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 contained in the ConnectionString.

    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 "User Id=test", and then reset the connection string to "Data Source=localhost", the UserId property is no longer set to test.

    The connection string is parsed immediately after being set. If errors in syntax are found when parsing, a runtime exception, such as System.ArgumentException, is generated. Other errors can be found only when an attempt is made to Open the connection.

    The ConnectionString is similar to an OLE DB connection string. Values may be delimited by single or double quotes, (for example, name='value' or name="value"). Either single or double quotes may be used within a connection string by using the other delimiter, for example, name="value's" or name= 'value"s', but not name= 'value's' or name= ""value"". All blank characters, except those placed within a value or within quotes, are ignored. Keyword value pairs must be separated by a semicolon (;). If a semicolon is part of a value, it also must be delimited by quotes. No escape sequences are supported. The value type is irrelevant. Names are not case sensitive. Arguments names may occur only once in the connection string.

    The following table lists the valid names for values within the ConnectionString.

    Name Description
    ApplicationIntent The application workload type when connecting to a database.
    Application Name The name of the application, or '.NET SQLClient Data Provider' if no application name is provided.
    Async When true, enables asynchronous operation support. Recognized values are true, false, yes, and no.
    AttachDBFilename
    -or-
    extended properties
    -or-
    Initial File Name
    The name of the primary database file, including the full path name of an attachable database. The path may be absolute or relative by using the DataDirectory substitution string. If DataDirectory is used, the database file must exist within a subdirectory of the directory pointed to by the substitution string.
    Connect Timeout
    -or-
    Connection Timeout
    The length of time (in seconds) to wait for a connection to the server before terminating the attempt and generating an error.
    Connection Lifetime 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 seconds) exceeds the value specified by Connection Lifetime. This is useful in clustered configurations to force load balancing between a running server and a server just brought online. A value of zero (0) causes pooled connections to have the maximum connection timeout.
    Connection Reset Determines whether the database connection is reset when being drawn from the pool. For SQL Server version 7.0, setting to false avoids making an additional server round trip when obtaining a connection, but you must realize that the connection state, such as database context, is not being reset.
    Context Connection true if an in-process connection to SQL Server should be made.
    Current Language The SQL Server Language record name.
    Data Source
    -or-
    Server
    -or-
    Address
    -or-
    Addr
    -or-
    Network Address
    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:
    server=tcp:servername, portnumber
    When specifying a local instance, always use (local). To force a protocol, add one of the following prefixes: np:(local), tcp:(local), lpc:(local)
    Default Command Timeout The time in seconds to wait while trying to execute a command before terminating the attempt and generating an error. 0 indicates no limit.
    Encrypt 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.
    Enlist true indicates that the SQL Server connection pooler automatically enlists the connection in the creation thread's current transaction context.
    Failover Partner The name of the failover partner server where database mirroring is configured.
    Initial Catalog
    -or-
    Database
    The name of the database.
    Initialization Command Specifies a database-specific command that should be executed immediately after establishing the connection.
    Integrated Security
    -or-
    Trusted_Connection
    When false, User ID and Password are specified in the connection. When true, the current Windows account credentials are used for authentication. Recognized values are true, false, yes, no, and sspi (strongly recommended), which is equivalent to true.
    Load Balance Timeout The minimum time, in seconds, for the connection to live in the connection pool before being destroyed.
    Max Pool Size The maximum number of connections allowed in the pool. The default value is 100.
    Min Pool Size The minimum number of connections allowed in the pool. The default value is 0.
    MultipleActiveResultSets 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. Recognized values are true and false.
    MultiSubnetFailover When true, an application a connection must attempt connections in parallel for multi-subnet failover or aggressively retry the TCP connection for a subnet failover. Recognized values are true and false. It is recommended to set this parameter to true when connecting to SQL Server 2012 availability group listener or SQL Server 2012 Failover Cluster Instance. Set this property to false when connecting to anything other than SQL Server 2012 availability group listener or SQL Server 2012 Failover Cluster Instance (default value).
    Network Library
    -or-
    Net
    The network library used to establish a connection to an instance of SQL Server. Supported values include dbnmpntw (Named Pipes), dbmsrpcn (Multiprotocol), dbmsadsn (Apple Talk), dbmsgnet (VIA), dbmslpcn (Shared Memory) and dbmsspxn (IPX/SPX), and dbmssocn (TCP/IP). The corresponding network DLL must be installed on the system to which you connect. If you do not specify a network and you use a local server (for example, "." or "(local)"), shared memory is used.
    Packet Size Size in bytes of the network packets used to communicate with an instance of SQL Server. The default value is 8192.
    Password
    -or-
    Pwd
    The password for the SQL Server account logging on. Not recommended. To maintain a high level of security, we strongly recommend that you use the Integrated Security or Trusted_Connection keyword instead.
    Persist Security Info When set to false or no (strongly recommended), security-sensitive information, such as the password, is not returned as part of the connection if the connection is open or has ever been in an open state. Resetting the connection string resets all connection string values including the password. Recognized values are true, false, yes, and no.
    Pooling When true, the SQLConnection object is drawn from the appropriate pool, or if it is required, is created and added to the appropriate pool. Recognized values are true, false, yes, and no. The default value is true.
    Replication true if replication is supported using the connection.
    TrustServerCertificate When set to true, SSL is used to encrypt the channel but to bypass walking the certificate chain to validate trust. If the connection string has TrustServerCertificate set to true but Encrypt is not set to true, the channel is not encrypted. Recognized values are true, false, yes, and no. For more information, see "Encryption Hierarchy" and "Using Encryption Without Validation" in SQL Server 2005 Books Online.
    Type System Version A string value that indicates the type system the application expects. Possible values are:
    Type System Version=SQL Server 2000;
    Type System Version=SQL Server 2005;
    Type System Version=Latest;
    User ID The SQL Server login account. Not recommended. To maintain a high level of security, we strongly recommend that you use the Integrated Security or Trusted_Connection keywords instead.
    User Instance 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 name of the workstation connecting to SQL Server. The default value is the local computer name.
    Validate Connection Specifies whether to validate connections that are being got from the pool.
    Example
    The following example creates a SqlConnection and sets some of its properties in the connection string.
    public void CreateSqlConnection()
    {
      string myConnString1 = 
         "User Id=sa;Server=localhost;Initial Catalog=Test;";
      SqlConnection myConnection1 = new SqlConnection(myConnString1);
      myConnection1.Open();
    }
    Public Sub CreateSqlConnection()
      Dim myConnString1 As String = _
          "User Id=sa;Server=localhost;Initial Catalog=Test;"
      Dim myConnection1 As New SqlConnection(myConnString1)
      myConnection1.Open()
    End Sub
    Requirements

    Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

    See Also