SqlConnectionStringBuilder Class
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Caution
Use the Microsoft.Data.SqlClient package instead.
Provides a simple way to create and manage the contents of connection strings used by the SqlConnection class.
public ref class SqlConnectionStringBuilder sealed : System::Data::Common::DbConnectionStringBuilder
public sealed class SqlConnectionStringBuilder : System.Data.Common.DbConnectionStringBuilder
[System.Obsolete("Use the Microsoft.Data.SqlClient package instead.")]
public sealed class SqlConnectionStringBuilder : System.Data.Common.DbConnectionStringBuilder
[System.ComponentModel.TypeConverter(typeof(System.Data.SqlClient.SqlConnectionStringBuilder+SqlConnectionStringBuilderConverter))]
public sealed class SqlConnectionStringBuilder : System.Data.Common.DbConnectionStringBuilder
type SqlConnectionStringBuilder = class
inherit DbConnectionStringBuilder
[<System.Obsolete("Use the Microsoft.Data.SqlClient package instead.")>]
type SqlConnectionStringBuilder = class
inherit DbConnectionStringBuilder
[<System.ComponentModel.TypeConverter(typeof(System.Data.SqlClient.SqlConnectionStringBuilder+SqlConnectionStringBuilderConverter))>]
type SqlConnectionStringBuilder = class
inherit DbConnectionStringBuilder
Public NotInheritable Class SqlConnectionStringBuilder
Inherits DbConnectionStringBuilder
- Inheritance
- Attributes
The following console application builds connection strings for a SQL Server database. The code uses the SqlConnectionStringBuilder class to create the connection string. The example then parses the connection string and demonstrates various ways of manipulating the its contents.
// Create a new SqlConnectionStringBuilder and
// initialize it with a few name/value pairs.
SqlConnectionStringBuilder builder = new(
"Server=(local);Integrated Security=true;" +
"Initial Catalog=AdventureWorks"
);
// The input connection string used the
// Server key, but the new connection string uses
// the well-known Data Source key instead.
Console.WriteLine($"Original connection string: '{builder.ConnectionString}'");
// Now that the connection string has been parsed,
// you can work with individual items.
Console.WriteLine($"Initial catalog: '{builder.InitialCatalog}'");
builder.InitialCatalog = "Northwind";
builder.AsynchronousProcessing = true;
// You can refer to connection keys using strings,
// as well. When you use this technique (the default
// Item property in Visual Basic, or the indexer in C#),
// you can specify any synonym for the connection string key name.
builder["Server"] = ".";
builder["Connect Timeout"] = 1000;
builder["Trusted_Connection"] = true;
Console.WriteLine($"Modified connection string: '{builder.ConnectionString}'");
Imports System.Data.SqlClient
Module Module1
Sub Main()
' Create a new SqlConnectionStringBuilder and
' initialize it with a few name/value pairs:
Dim builder As New SqlConnectionStringBuilder(
"Server=(local);Integrated Security=true;" &
"Initial Catalog=AdventureWorks"
)
' The input connection string used the
' Server key, but the new connection string uses
' the well-known Data Source key instead.
Console.WriteLine("Original connection string: " + builder.ConnectionString)
' Now that the connection string has been parsed,
' you can work with individual items.
Console.WriteLine("Initial catalog: " + builder.InitialCatalog)
builder.InitialCatalog = "Northwind"
builder.AsynchronousProcessing = True
' You can refer to connection keys using strings,
' as well. When you use this technique (the default
' Item property in Visual Basic, or the indexer in C#)
' you can specify any synonym for the connection string key
' name.
builder("Server") = "."
builder("Connect Timeout") = 1000
' The Item property is the default for the class,
' and setting the Item property adds the value to the
' dictionary, if necessary.
builder.Item("Trusted_Connection") = True
Console.WriteLine("Modified connection string: " + builder.ConnectionString)
End Sub
End Module
The connection string builder lets developers programmatically create syntactically correct connection strings, and parse and rebuild existing connection strings, using properties and methods of the class. The connection string builder provides strongly typed properties corresponding to the known key/value pairs allowed by SQL Server. If you need to create connection strings as part of your app, you can use the SqlConnectionStringBuilder class to build and modify connection strings. The class also makes it easy to manage connection strings stored in an application configuration file.
SqlConnectionStringBuilder performs checks for valid key/value pairs. Therefore, you cannot use this class to create invalid connection strings; trying to add invalid pairs will throw an exception. The class maintains a fixed collection of synonyms and can translate from a synonym to the corresponding well-known key name.
For example, when you use the Item
property to retrieve a value, you can specify a string that contains any synonym for the key you need. For example, you can specify "Network Address", "addr", or any other acceptable synonym for this key within a connection string when you use any member that requires a string that contains the key name, such as the Item[String] property or the Remove method. See the ConnectionString property for a full list of acceptable synonyms.
The Item[String] property handles attempts to insert malicious entries. For example, the following code, using the default Item
property (the indexer, in C#) correctly escapes the nested key/value pair:
Dim builder As New System.Data.SqlClient.SqlConnectionStringBuilder
builder("Data Source") = "(local)"
builder("Integrated Security") = True
builder("Initial Catalog") = "AdventureWorks;NewValue=Bad"
Console.WriteLine(builder.ConnectionString)
System.Data.SqlClient.SqlConnectionStringBuilder builder =
new System.Data.SqlClient.SqlConnectionStringBuilder();
builder["Data Source"] = "(local)";
builder["integrated Security"] = true;
builder["Initial Catalog"] = "AdventureWorks;NewValue=Bad";
Console.WriteLine(builder.ConnectionString);
The result is the following connection string that handles the invalid value in a safe manner:
Source=(local);Initial Catalog="AdventureWorks;NewValue=Bad";
Integrated Security=True
Sql |
Initializes a new instance of the SqlConnectionStringBuilder class. |
Sql |
Initializes a new instance of the SqlConnectionStringBuilder class. The provided connection string provides the data for the instance's internal connection information. |
Application |
Declares the application workload type when connecting to a database in an SQL Server Availability Group. You can set the value of this property with ApplicationIntent. For more information about SqlClient support for Always On Availability Groups, see SqlClient Support for High Availability, Disaster Recovery. |
Application |
Gets or sets the name of the application associated with the connection string. |
Asynchronous |
Gets or sets a Boolean value that indicates whether asynchronous processing is allowed by the connection created by using this connection string. |
Attach |
Gets or sets a string that contains the name of the primary data file. This includes the full path name of an attachable database. |
Authentication |
Gets the authentication of the connection string. |
Browsable |
Gets or sets a value that indicates whether the ConnectionString property is visible in Visual Studio designers. (Inherited from DbConnectionStringBuilder) |
Column |
Gets or sets the column encryption settings for the connection string builder. |
Connection |
Obsolete.
Obsolete. Gets or sets a Boolean value that indicates whether the connection is reset when drawn from the connection pool. |
Connection |
Gets or sets the connection string associated with the DbConnectionStringBuilder. (Inherited from DbConnectionStringBuilder) |
Connect |
The number of reconnections attempted after identifying that there was an idle connection failure. This must be an integer between 0 and 255. Default is 1. Set to 0 to disable reconnecting on idle connection failures. An ArgumentException will be thrown if set to a value outside of the allowed range. |
Connect |
Amount of time (in seconds) between each reconnection attempt after identifying that there was an idle connection failure. This must be an integer between 1 and 60. The default is 10 seconds. An ArgumentException will be thrown if set to a value outside of the allowed range. |
Connect |
Gets or sets the length of time (in seconds) to wait for a connection to the server before terminating the attempt and generating an error. |
Context |
Gets or sets a value that indicates whether a client/server or in-process connection to SQL Server should be made. |
Count |
Gets the current number of keys that are contained within the ConnectionString property. (Inherited from DbConnectionStringBuilder) |
Current |
Gets or sets the SQL Server Language record name. |
Data |
Gets or sets the name or network address of the instance of SQL Server to connect to. |
Enclave |
Gets or sets the enclave attestation Url to be used with enclave based Always Encrypted. |
Encrypt |
Gets or sets a Boolean value that indicates whether SQL Server uses SSL encryption for all data sent between the client and server if the server has a certificate installed. |
Enlist |
Gets or sets a Boolean value that indicates whether the SQL Server connection pooler automatically enlists the connection in the creation thread's current transaction context. |
Failover |
Gets or sets the name or address of the partner server to connect to if the primary server is down. |
Initial |
Gets or sets the name of the database associated with the connection. |
Integrated |
Gets or sets a Boolean value that indicates whether User ID and Password are specified in the connection (when |
Is |
Gets a value that indicates whether the SqlConnectionStringBuilder has a fixed size. |
Is |
Gets a value that indicates whether the DbConnectionStringBuilder has a fixed size. (Inherited from DbConnectionStringBuilder) |
Is |
Gets a value that indicates whether the DbConnectionStringBuilder is read-only. (Inherited from DbConnectionStringBuilder) |
Item[String] |
Gets or sets the value associated with the specified key. In C#, this property is the indexer. |
Keys |
Gets an ICollection that contains the keys in the SqlConnectionStringBuilder. |
Load |
Gets or sets the minimum time, in seconds, for the connection to live in the connection pool before being destroyed. |
Max |
Gets or sets the maximum number of connections allowed in the connection pool for this specific connection string. |
Min |
Gets or sets the minimum number of connections allowed in the connection pool for this specific connection string. |
Multiple |
When true, an application can maintain multiple active result sets (MARS). When false, an application must process or cancel all result sets from one batch before it can execute any other batch on that connection. For more information, see Multiple Active Result Sets (MARS). |
Multi |
If your application is connecting to an Always On availability group (AG) or Always On Failover Cluster Instance (FCI) on different subnets, setting MultiSubnetFailover=true provides faster detection of and connection to the (currently) active server. For more information about SqlClient support for the Always On features, see SqlClient Support for High Availability, Disaster Recovery. |
Network |
Gets or sets a string that contains the name of the network library used to establish a connection to the SQL Server. |
Packet |
Gets or sets the size in bytes of the network packets used to communicate with an instance of SQL Server. |
Password |
Gets or sets the password for the SQL Server account. |
Persist |
Gets or sets a value indicating if security-sensitive information, such as the password or access token, should be returned as part of the connection string on a connection created with this SqlConnectionStringBuilder after that connection has ever been in an open state. |
Pool |
The blocking period behavior for a connection pool. |
Pooling |
Gets or sets a Boolean value that indicates whether the connection will be pooled or explicitly opened every time that the connection is requested. |
Replication |
Gets or sets a Boolean value that indicates whether replication is supported using the connection. |
Transaction |
Gets or sets a string value that indicates how the connection maintains its association with an enlisted |
Transparent |
When the value of this key is set to |
Trust |
Gets or sets a value that indicates whether the channel will be encrypted while bypassing walking the certificate chain to validate trust. |
Type |
Gets or sets a string value that indicates the type system the application expects. |
UserID |
Gets or sets the user ID to be used when connecting to SQL Server. |
User |
Gets or sets a value that indicates whether to redirect the connection from the default SQL Server Express instance to a runtime-initiated instance running under the account of the caller. |
Values |
Gets an ICollection that contains the values in the SqlConnectionStringBuilder. |
WorkstationID |
Gets or sets the name of the workstation connecting to SQL Server. |
Add(String, Object) |
Adds an entry with the specified key and value into the DbConnectionStringBuilder. (Inherited from DbConnectionStringBuilder) |
Clear() |
Clears the contents of the SqlConnectionStringBuilder instance. |
Clear |
Clears the collection of PropertyDescriptor objects on the associated DbConnectionStringBuilder. (Inherited from DbConnectionStringBuilder) |
Contains |
Determines whether the SqlConnectionStringBuilder contains a specific key. |
Equals(Object) |
Determines whether the specified object is equal to the current object. (Inherited from Object) |
Equivalent |
Compares the connection information in this DbConnectionStringBuilder object with the connection information in the supplied object. (Inherited from DbConnectionStringBuilder) |
Get |
Serves as the default hash function. (Inherited from Object) |
Get |
Fills a supplied Hashtable with information about all the properties of this DbConnectionStringBuilder. (Inherited from DbConnectionStringBuilder) |
Get |
Gets the Type of the current instance. (Inherited from Object) |
Memberwise |
Creates a shallow copy of the current Object. (Inherited from Object) |
Remove(String) |
Removes the entry with the specified key from the SqlConnectionStringBuilder instance. |
Should |
Indicates whether the specified key exists in this SqlConnectionStringBuilder instance. |
To |
Returns the connection string associated with this DbConnectionStringBuilder. (Inherited from DbConnectionStringBuilder) |
Try |
Retrieves a value corresponding to the supplied key from this SqlConnectionStringBuilder. |
ICollection. |
Copies the elements of the ICollection to an Array, starting at a particular Array index. (Inherited from DbConnectionStringBuilder) |
ICollection. |
Gets a value indicating whether access to the ICollection is synchronized (thread safe). (Inherited from DbConnectionStringBuilder) |
ICollection. |
Gets an object that can be used to synchronize access to the ICollection. (Inherited from DbConnectionStringBuilder) |
ICustom |
Returns a collection of custom attributes for this instance of a component. (Inherited from DbConnectionStringBuilder) |
ICustom |
Returns the class name of this instance of a component. (Inherited from DbConnectionStringBuilder) |
ICustom |
Returns the name of this instance of a component. (Inherited from DbConnectionStringBuilder) |
ICustom |
Returns a type converter for this instance of a component. (Inherited from DbConnectionStringBuilder) |
ICustom |
Returns the default event for this instance of a component. (Inherited from DbConnectionStringBuilder) |
ICustom |
Returns the default property for this instance of a component. (Inherited from DbConnectionStringBuilder) |
ICustom |
Returns an editor of the specified type for this instance of a component. (Inherited from DbConnectionStringBuilder) |
ICustom |
Returns the events for this instance of a component. (Inherited from DbConnectionStringBuilder) |
ICustom |
Returns the events for this instance of a component using the specified attribute array as a filter. (Inherited from DbConnectionStringBuilder) |
ICustom |
Returns the properties for this instance of a component. (Inherited from DbConnectionStringBuilder) |
ICustom |
Returns the properties for this instance of a component using the attribute array as a filter. (Inherited from DbConnectionStringBuilder) |
ICustom |
Returns an object that contains the property described by the specified property descriptor. (Inherited from DbConnectionStringBuilder) |
IDictionary. |
Adds an element with the provided key and value to the IDictionary object. (Inherited from DbConnectionStringBuilder) |
IDictionary. |
Determines whether the IDictionary object contains an element with the specified key. (Inherited from DbConnectionStringBuilder) |
IDictionary. |
Returns an IDictionaryEnumerator object for the IDictionary object. (Inherited from DbConnectionStringBuilder) |
IDictionary. |
Gets a value indicating whether the IDictionary object has a fixed size. (Inherited from DbConnectionStringBuilder) |
IDictionary. |
Gets a value indicating whether the IDictionary is read-only. (Inherited from DbConnectionStringBuilder) |
IDictionary. |
Gets or sets the element with the specified key. (Inherited from DbConnectionStringBuilder) |
IDictionary. |
Removes the element with the specified key from the IDictionary object. (Inherited from DbConnectionStringBuilder) |
IEnumerable. |
Returns an enumerator that iterates through a collection. (Inherited from DbConnectionStringBuilder) |
Cast<TResult>(IEnumerable) |
Casts the elements of an IEnumerable to the specified type. |
Of |
Filters the elements of an IEnumerable based on a specified type. |
As |
Enables parallelization of a query. |
As |
Converts an IEnumerable to an IQueryable. |
מוצר | גירסאות (מיושן) |
---|---|
.NET | Core 1.0, Core 1.1, 6 (package-provided) (8 (package-provided)) |
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 (4.7 (package-provided), 4.7.1 (package-provided), 4.7.2 (package-provided), 4.8 (package-provided)) |
.NET Standard | (2.0 (package-provided)) |
משוב של .NET
.NET הוא פרויקט קוד פתוח. בחר קישור כדי לספק משוב: