SqlCeConnection.ConnectionString Property

Note: This namespace, class, or member is supported only in version 1.1 of the .NET Framework.

Gets or sets the string used to open a database.

  [Visual Basic]
  Public Overridable Property ConnectionString As String  Implements _
   IDbConnection.ConnectionString
[C#]
public virtual string ConnectionString {get; set;}
[C++]
public: __property virtual String* get_ConnectionString();
public: __property virtual void set_ConnectionString(String*);
[JScript]
public function get ConnectionString() : String;
public function set ConnectionString(String);

Property Value

The .NET Compact Framework data provider for SQL Server CE connection string that includes the data source name and other parameters needed to establish the initial connection. The default value is an empty string.

Implements

IDbConnection.ConnectionString

Exceptions

Exception Type Condition
ArgumentException An invalid connection string argument has been supplied.

Remarks

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 is updated. SqlCeConnection 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 "Data Source =\my app\Northwind.sdf", and then reset the connection string to "Data Source =\my app\Master.sdf; Password=<Password>", the DataSource property is no longer set to Northwind.sdf.

The following table lists the ConnectionString properties supported in SQL Server CE. For more information, see the SQL Server CE Books Online.

Name Description
data source Name of the SQL Server CE database to connect to: "\my app\myDB.sdf".
password Database password, which can be up to 40 characters in length.
persist security info When set to false (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.
encrypt database Boolean value that determines whether or not the database is encrypted, if the platform supports encryption.
max buffer size The largest amount of memory, in kilobytes, that SQL Server CE can use before it starts flushing changes to disk.
locale identifier Locale ID (LCID) of preference.
temp file directory Location of the temporary database

A preliminary validation of the connection string is performed when the property is set. When an application calls the Open method, the connection string is fully validated. If the connection string contains properties that are unsupported or not valid, a run-time exception, such as ArgumentException, is generated at that time.

Values may be delimited by single or double quotation marks, (for example, name='value' or name="value"). Either single or double quotation marks 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 quotation marks, 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 quotation marks. No escape sequences are supported. The value type is irrelevant. Names are not case-sensitive. If a property name occurs more than once in the connection string, the value associated with the last occurrence is used.

Example

[Visual Basic, C#] The following example creates a SqlCeConnection and sets some of its properties in the connection string.

  [Visual Basic] 
Dim conn As New SqlCeConnection()
conn.ConnectionString = "Persist Security Info=False; Data Source = Northwind.sdf;" + _
  "Password = <password>; server=mySQLServer;Connect Timeout=30"

conn.Open()

[C#] 
SqlCeConnection conn = new SqlCeConnection();
conn.ConnectionString = "Persist Security Info=False; Data Source = Northwind.sdf;" + 
    "Password = <password>; server=mySQLServer;Connect Timeout=30";

conn.Open();

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: .NET Compact Framework

.NET Framework Security:

See Also

SqlCeConnection Class | SqlCeConnection Members | System.Data.SqlServerCe Namespace

Syntax based on .NET Framework version 1.1.
Documentation version 1.1.1.

Send comments on this topic.

© Microsoft Corporation. All rights reserved.