在 ADO.NET 中的连接字符串Connection Strings in ADO.NET

连接字符串包含作为参数从数据提供程序传递到数据源的初始化信息。A connection string contains initialization information that is passed as a parameter from a data provider to a data source. 数据访问接口接收连接字符串作为属性的值 DbConnection.ConnectionStringThe data provider receives the connection string as the value of the DbConnection.ConnectionString property. 提供程序分析连接字符串,并确保语法正确,并且支持关键字。The provider parses the connection string and ensures that the syntax is correct and that the keywords are supported. 然后, DbConnection.Open() 方法将已分析的连接参数传递到数据源。Then the DbConnection.Open() method passes the parsed connection parameters to the data source. 数据源执行进一步的验证,并建立连接。The data source performs further validation and establishes a connection.

连接字符串语法Connection string syntax

连接字符串是以分号分隔的键/值参数对列表:A connection string is a semicolon-delimited list of key/value parameter pairs:

keyword1=value; keyword2=value;

关键字不区分大小写。Keywords are not case-sensitive. 但是,值可能区分大小写,具体取决于数据源。Values, however, may be case-sensitive, depending on the data source. 关键字和值可以包含空格字符Both keywords and values may contain whitespace characters. 关键字和无引号值中会忽略前导空格和尾随空格。Leading and trailing white space is ignored in keywords and unquoted values.

如果值包含分号、 Unicode 控制字符或前导或尾随空格,则必须用单引号或双引号将其引起来。If a value contains the semicolon, Unicode control characters, or leading or trailing white space, it must be enclosed in single or double quotation marks. 例如:For example:

Keyword=" whitespace  ";
Keyword='special;character';

封闭字符不能出现在它所包含的值中。The enclosing character may not occur within the value it encloses. 因此,包含单引号的值只能用双引号引起来,反之亦然:Therefore, a value containing single quotation marks can be enclosed only in double quotation marks, and vice versa:

Keyword='double"quotation;mark';
Keyword="single'quotation;mark";

还可以通过将其中两个字符一起使用来转义封闭字符:You can also escape the enclosing character by using two of them together:

Keyword="double""quotation";
Keyword='single''quotation';

引号本身以及等号不需要进行转义,因此以下连接字符串有效:The quotation marks themselves, as well as the equals sign, do not require escaping, so the following connection strings are valid:

Keyword=no "escaping" 'required';
Keyword=a=b=c

由于每个值都是在下一个分号或字符串末尾之前读取的,因此后一示例中的值为 a=b=c ,而最后的分号是可选的。Since each value is read till the next semicolon or the end of string, the value in the latter example is a=b=c, and the final semicolon is optional.

所有连接字符串共享上述相同的基本语法。All connection strings share the same basic syntax described above. 但所识别的关键字集取决于提供程序,但在以前的 Api (例如ODBC)中已经演变。The set of recognized keywords depends on the provider, however, and has evolved over the years from earlier APIs such as ODBC. 用于SQL Server ()的 .NET Framework数据提供程序 SqlClient 支持来自较低版本 api 的多个关键字,但通常更灵活,并接受多个常用连接字符串关键字的同义词。The .NET Framework data provider for SQL Server (SqlClient) supports many keywords from older APIs, but is generally more flexible and accepts synonyms for many of the common connection string keywords.

键入错误会导致错误。Typing mistakes can cause errors. 例如, Integrated Security=true 是有效的,但 IntegratedSecurity=true 会导致错误。For example, Integrated Security=true is valid, but IntegratedSecurity=true causes an error.

在运行时从未经验证用户输入手动构造的连接字符串容易受到字符串注入式攻击,并危害数据源的安全性。Connection strings constructed manually at run time from unvalidated user input are vulnerable to string-injection attacks and jeopardize security at the data source. 为了解决这些问题, ADO.NET 2.0 引入了每个 .NET Framework数据提供程序的连接字符串生成器To address these problems, ADO.NET 2.0 introduced connection string builders for each .NET Framework data provider. 这些连接字符串生成器将参数作为强类型属性公开,并在将连接字符串发送到数据源之前验证连接字符串。These connection string builders expose parameters as strongly typed properties, and make it possible to validate the connection string before it's sent to the data source.

本节内容In This Section

连接字符串生成器Connection String Builders
演示如何使用 ConnectionStringBuilder 类在运行时构造有效的连接字符串。Demonstrates how to use the ConnectionStringBuilder classes to construct valid connection strings at run time.

连接字符串和配置文件Connection Strings and Configuration Files
演示如何在配置文件中存储和检索连接字符串。Demonstrates how to store and retrieve connection strings in configuration files.

连接字符串语法Connection String Syntax
描述如何为 SqlClientOracleClientOleDbOdbc 配置提供程序专用的连接字符串。Describes how to configure provider-specific connection strings for SqlClient, OracleClient, OleDb, and Odbc.

保护连接信息Protecting Connection Information
演示保护用于连接到数据源的信息的各项技术。Demonstrates techniques for protecting information used to connect to a data source.

另请参阅See also