asp.net.ph

SqlConnection.ConnectionString Property

System.Data.SqlClient Namespace   SqlConnection Class


Sets or retrieves the string used to open an SQL Server™ database.

Syntax


Script SqlConnection.ConnectionString [ = connectString ]

Property Value


connectString The connection string that includes the source database name, and other parameters needed to establish the initial connection.

The property is read/write with no default value.

Exceptions


Exception Type Condition
ArgumentException Occurs when an invalid connection string argument has been supplied or a required connection string argument has not been supplied.

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 Persist Security Info value is set to false ( default ). The SQL Server™ .NET Data Provider does not persist or return the password in a connection string unless you set Persist Security Info to true.

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, 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

"Database=northwind"

and then reset the connection string to

"Data Source=myServer; Integrated Security=SSPI"

the Database property is no longer set to northwind.

The connection string is parsed immediately after being set. If errors in syntax are found when parsing, an SqlException is generated. Other errors, such as invalid or unsupported properties, can be found only when an attempt is made to Open the connection at run time.

Connection String Syntax Notes

  • 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"". The value type is irrelevant.
  • 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.
  • Names are not case sensitive. If a given name occurs more than once in the connection string, the value associated with the last occurence is used.
  • No escape sequences are supported.

The following table lists the valid name=value pairs that can be used within the ConnectionString.

Name Default Description
Application Name   The name of the application.
AttachDBFilename or extended properties or Initial File Name   The name of the primary file, including the full path name, of an attachable database. The database name must be specified with the keyword 'database'.
Connect Timeout or Connection Timeout 15 The length of time ( in seconds ) to wait for a connection to the server before terminating the attempt and generating an error.
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 seconds ) exceeds the value specified by connection lifetime. Useful in clustered configurations to force load balancing between a running server and a server just brought on-line.
Connection Reset 'true' Determines whether the database connection is reset when being removed from the pool. Setting to 'false' avoids making an additional server round-trip when obtaining a connection, but the programmer must be aware that the connection state is not being reset.
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.
Enlist 'true' When true, the pooler automatically enlists the connection in the creation thread's current transaction context.
Initial Catalog or Database   The name of the database.
Integrated Security or Trusted_Connection 'false' Whether the connection is to be a secure connection or not.

Recognized values are 'true', 'false', and 'sspi', which is equivalent to 'true'.

Max Pool Size 100 The maximum number of connections allowed in the pool.
Min Pool Size 0 The minimum number of connections allowed in the pool.
Network Library or Net 'dbmssocn' The network library used to establish a connection to an instance of SQL Server. The default value, dbnssocn, specifies TCP/IP. Other values include dbnmpntw ( Named Pipes ), dbmsrpcn ( Multiprotocol ), dbmsadsn ( Apple Talk ), dbmsgnet ( VIA ), dbmsipcn ( Shared Memory ) and dbmsspxn ( IPX/SPX ).

The corresponding network DLL must be installed on the system to which you connect.

Packet Size 8192 Size in bytes of the network packets used to communicate with an instance of SQL Server.
Password or Pwd   The password for the SQL Server™ account logging on.
Persist Security Info 'false' When set to 'false', 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.
Pooling 'true' When true, the SQLConnection object is drawn from the appropriate pool, or if necessary, is created and added to the appropriate pool.
User ID   The SQL Server™ login account.
Workstation ID The local computer name The name of the workstation connecting to SQL Server.

When setting boolean properties, you can use 'yes' instead of 'true', and 'no' instead of 'false'. Integer values are represented as strings.

NOTE: the SQL Server™ .NET data Provider 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.

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 ensure that a user cannot embed extra connection string parameters in these values ( for example, entering a password as "validpassword;database=somedb" in an attempt to attach to a different database ).

Example

The following example instantiates an SqlConnection, sets some of its parameters in the connection string, and opens the connection.

SqlConnection myConn = new SqlConnection ( );
myConn.ConnectionString = "user id=sa; password=yervzeñun;" +
   "data source=myServer; initial catalog=northwind;"
myConn.Open ( );
  C# VB

See Also

SqlConnection Members   ConnectionTimeout   Database   DataSource   Connecting to a Database Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph