Using Connection Strings
- Last Updated: April 16, 2026
- 3 minute read
- ADO.NET
- Documentation
You can define the behavior of a connection using a connection string or the properties of the data provider’s Connection object. However, values set in the connection string cannot be changed by the connection properties.
The basic format of a connection string includes a series of keyword/value pairs separated by semicolons. The following example shows the keywords and values for a simple connection string for the Oracle data provider:
"Host=Accounting1;Port=1521;Service Name=ORCL;User ID=scott;Password=tiger"
Use the following guidelines when specifying a connection string:
-
The spaces in the connection string option names are required.
-
All connection string option names are case-insensitive. For example, Password is the same as password. However, the values of options such as User ID and Password may be case-sensitive.
-
To include values that contain a semicolon, single quote, or double quotes, enclose the value in double quotes. If the value contains both a semicolon and double quotes, use single quotes to enclose the value.
You can also use single quotes when the value starts with a double quote. Conversely, double quotes can be used if the value starts with a single quote. If the value contains both single quotes and double quotes, the character used to enclose the value must be doubled every time it occurs within the value.
-
To include leading or trailing spaces in the string value, the value must be enclosed in either single quotes or double quotes. Any leading or trailing spaces around integer, Boolean, or enumerated values are ignored, even if enclosed in single or double quotes. However, spaces within a string literal keyword or value are preserved. Single or double quotes can be used within a connection string without using delimiters (for example,
Data Source= my'ServerorData Source= my"Server), unless it is the first or last character in the value. -
Special characters can be used in the value of the connection string option. To escape special characters, surround the value in single or double quotes.
The Equals character (=) can also be repeated within the connection string. For example:
Initialization String="update mytable set col1 == 'foo'"
- If the connection string contains duplicated connection string options, the data provider uses the connection string option that appears last in the connection string. For example, Connection Timeout appears twice in the following connection string, with different values. The data provider uses the second value and waits 35 seconds before terminating an attempted connection:
"Host=Accounting1;Port=1521;Service Name=ORCL; Connection Timeout=15;User ID=scott;Password=tiger;Connection Timeout=35"
- If the connection string contains invalid connection string options, the data provider returns an error. For example, an error is returned if you specify a value for Load Balancing when Alternate Servers has not been defined.
Connection String Option Descriptions contains detailed information about supported connection string options:
- See DB2 Data Provider Connection String Options for information for the DB2 data provider and DB2 Entity Framework data provider.
- See Oracle Data Provider Connection String Options for information for the Oracle data provider and Oracle Entity Framework data provider.
- See SQL Server Data Provider Connection String Options for information for the Microsoft SQL Server data provider.
- See Sybase Data Provider Connection String Options for information for the Sybase data provider and Sybase Entity Framework data provider.
Connecting to Entity Framework Applications
NOTE: This section applies to the Oracle and DB2 Entity Framework data providers.
When a Code First application does not provide a connection or name when creating a DbContext, the Entity Framework uses the DefaultConnectionFactory to create a DbConnection object to service the context. By default, the Entity Framework uses the SqlCeConnectionFactory class but you can override this using the data provider’s ConnectionFactory interface.
DataDirect Connect for ADO.NET Entity Framework data providers include an implementation of the IDbConnectionFactory interface that developers can use to make the provider the default. See the specific data provider sections for an example
Below is a simple Code First application that inserts a row into the database.
class SimpleCodeFirst {
public static void Main(string[] args) {
RkoContext context = new RkoContext(args[0]);
RkoPerson person = new RkoPerson();
person.Name = "John Smith";
context.RkoPeople.Add(person);
context.SaveChanges(); // performs insert
}
}
public class RkoPerson {
public int Id { get; set; }
public string Name { get; set; }
}
public class RkoContext : DbContext {
public RkoContext(string connectionName) : base(connectionName) { }
public DbSet<RkoPerson> RkoPeople { get; set; }
}