OracleConnection.ConnectionString 属性

定义

获取或设置用于打开 Oracle 数据库的字符串。Gets or sets the string used to open an Oracle database.

public:
 virtual property System::String ^ ConnectionString { System::String ^ get(); void set(System::String ^ value); };
[System.ComponentModel.SettingsBindable(true)]
public override string ConnectionString { get; set; }
member this.ConnectionString : string with get, set
Public Overrides Property ConnectionString As String

属性值

包含设置(如服务器名称等)的 Oracle 连接字符串需要建立初始连接。The Oracle connection string that includes settings, such as the server name, needed to establish the initial connection. 默认值为空字符串 ("")。The default value is an empty string ("").

属性

示例

下面的示例创建一个 OracleConnection 并在连接字符串中设置它的一些属性。The following example creates an OracleConnection and sets some of its properties in the connection string.

public void CreateOracleConnection()
{
    string connectionString = "Data Source=Oracle8i;Integrated Security=yes";
    using (OracleConnection connection = new OracleConnection(connectionString))
    {
        connection.Open();
        Console.WriteLine("ServerVersion: " + connection.ServerVersion
            + "\nDataSource: " + connection.DataSource);
    }
}
Public Sub CreateOracleConnection()
    Dim connectionString As String = _
       "Data Source=Oracle8i;Integrated Security=yes"

    Using connection As New OracleConnection(connectionString)
        connection.Open()
        Console.WriteLine("ServerVersion: " + connection.ServerVersion _
           + ControlChars.NewLine + "DataSource: " + connection.DataSource)
    End Using
End Sub

注解

只能在连接关闭时设置 ConnectionStringThe ConnectionString can be set only when the connection is closed.

您可以使用 ConnectionString 属性连接到数据库。You can use the ConnectionString property to connect to a database. 下面的示例演示了一个典型的连接字符串。The following example illustrates a typical connection string.

"Data Source=Oracle8i;Integrated Security=yes"  

字符串中指定的许多设置都具有对应的只读属性(例如,Data Source=MyServerDataSource 属性相对应)。Many of the settings specified in the string have corresponding read-only properties (for example, Data Source=MyServer, which corresponds to the DataSource property). 如果设置了连接字符串,则所有这些属性都将更新,除非检测到错误,在这种情况下不会更新任何属性。When the connection string is set, all of these properties are updated, unless an error is detected, in which case none of the properties are updated. OracleConnection 属性仅返回默认设置或 ConnectionString中指定的设置。OracleConnection properties return only default settings or those settings specified in the ConnectionString.

重置已关闭连接上的 ConnectionString 将重置所有连接字符串值(以及相关属性),包括密码。Resetting the ConnectionString on a closed connection resets all connection string values (and related properties), including the password.

设置后,将立即分析连接字符串。The connection string is parsed immediately after being set. 如果分析时发现语法错误,则会生成运行时异常,如 ArgumentExceptionIf parsing reveals errors in syntax, a run-time 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 is 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 quotes. 如果该值同时包含分号和双引号字符,则该值可以用单引号括起来。If the value contains both a semicolon and a double-quote character, the value can be enclosed in single quotes. 如果值以双引号字符开头,则单引号也很有用。The single quote is also useful if the value begins with a double-quote character. 相反,如果值以单引号开头,则可以使用双引号。Conversely, the double quote can be used if the value begins with a single quote. 如果该值同时包含单引号和双引号字符,则在每次该值出现在值中时,用来括住该值的引号字符必须成对出现。If the value contains both single-quote and double-quote characters, the quote character used to enclose the value must be doubled each time it occurs within the value.

若要在字符串值中包含前导空格或尾随空格,则必须用单引号或双引号将该值引起来。To include preceding 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 quotes. 但是,字符串文字关键字或值中的空格将被保留。However, spaces within a string literal keyword or value are preserved. 在不使用分隔符的情况下,可以在连接字符串中使用单引号或双引号,除非引号字符是值中的第一个字符或最后一个字符,例如 Data Source= my'ServerData Source= my"ServerSingle or double quotes may be used within a connection string without using delimiters unless a quote character is the first or last character in the value, for example, Data Source= my'Server or Data Source= my"Server.

若要在关键字或值中包含等号(=),则它必须以另一个等号开头。To include an equal sign (=) in a keyword or value, it must be preceded by another equal sign. 例如,在假设的连接字符串中For example, in the hypothetical connection string

"key==word=value"  

关键字为 "key = word",值为 "value"。the keyword is "key=word" and the value is "value".

如果关键字/值对中的特定关键字在连接字符串中出现多次,则将在值集中使用最后一个匹配项。If a specific keyword in a keyword/value pair occurs more than once in a connection string, the last occurrence is used in the value set.

关键字不区分大小写。Keywords are case insensitive.

下表列出了 ConnectionString中的值的有效名称。The following table lists the valid names for values within the ConnectionString.

nameName 默认Default 说明Description
“数据源”Data Source

-or-

服务器Server
要连接到的 Oracle 实例的名称或网络地址。The name or network address of the instance of Oracle to which to connect.
Integrated SecurityIntegrated Security false'false' 该连接是否将成为安全连接。Whether the connection is to be a secure connection or not.

识别的值为 true (强烈建议)、falseyesnoRecognized values are true (strongly recommended), false, yes, and no.
PasswordPassword 用于登录的 Oracle 帐户的密码。The password for the Oracle account logging on. 为了保持较高的安全级别,强烈建议您改用集成安全关键字。To maintain a high level of security, we strongly recommend that you use the Integrated Security keyword instead.
Persist Security InfoPersist Security Info false'false' 当设置为 falseno (强烈建议)时,如果连接打开或曾经处于打开的 State,则不会作为连接的一部分返回安全敏感信息(如密码)。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.

已识别的值为 truefalseyesnoRecognized values are true, false, yes, and no.
UnicodeUnicode false'false' 指定 Oracle 的 .NET Framework 数据提供程序是否使用 UTF16 模式 API 调用。Specifies whether the .NET Framework Data Provider for Oracle uses UTF16 mode API calls. 除非使用 Oracle9i 客户端软件,否则将忽略此关键字。This keyword is ignored except when using Oracle9i client software.
User IDUser ID Oracle 登录帐户。The Oracle login account. 为了保持较高的安全级别,强烈建议您改用集成安全关键字。To maintain a high level of security, we strongly recommend that you use the Integrated Security keyword instead.

下表列出了 ConnectionString中的连接池值的有效名称。The following table lists the valid names for connection pooling values within the ConnectionString.

nameName 默认Default 说明Description
连接生存期Connection Lifetime 00 连接返回到池中后,创建时间将与当前时间进行比较,如果时间跨度(秒)超过 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 causes pooled connections to have the maximum connection time-out.
EnlistEnlist 'true''true' trueyes时,池池会在创建线程的当前事务上下文中自动登记连接。When true or yes, the pooler automatically enlists the connection in the creation thread's current transaction context.

已识别的值为 truefalseyesnoRecognized values are true, false, yes, and no.
Max Pool SizeMax Pool Size 100100 池中允许的最大连接数。The maximum number of connections allowed in the pool. 设置 ConnectionStringMax Pool Size 值可能会影响性能。Setting the Max Pool Size value of the ConnectionString can affect performance. 如果计划创建并主动使用超过100个的连接,则应将 Max Pool Size 增加到接近应用程序稳定状态连接使用情况的值。If you plan to create and actively use more than 100 connections, you should increase Max Pool Size to a value that approximates the steady-state connection usage for the application.
Min Pool SizeMin Pool Size 00 池中允许的最小连接数。The minimum number of connections allowed in the pool.
PoolingPooling 'true''true' trueyes时,OracleConnection 对象将从相应的池中进行绘制,如有必要,将创建并添加到相应的池中。When true or yes, the OracleConnection object is drawn from the appropriate pool, or if necessary, is created and added to the appropriate pool.

已识别的值为 truefalseyesnoRecognized values are true, false, yes, and no.

当设置需要布尔值的关键字或连接池值时,可以使用 "是" 而不是 "true" 和 "no" 而不是 "false"。When 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.

适用于