-
Notifications
You must be signed in to change notification settings - Fork 329
Expand file tree
/
Copy pathSqlConnectionStringBuilder.xml
More file actions
1707 lines (1610 loc) · 89.9 KB
/
SqlConnectionStringBuilder.xml
File metadata and controls
1707 lines (1610 loc) · 89.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<docs>
<members name="SqlConnectionStringBuilder">
<SqlConnectionStringBuilder>
<summary>
Provides a simple way to create and manage the contents of connection strings used by the <see cref="T:Microsoft.Data.SqlClient.SqlConnection" /> class.
</summary>
<remarks>
<format type="text/markdown">
<![CDATA[
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. Developers needing to create connection strings as part of applications can use the <xref:Microsoft.Data.SqlClient.SqlConnectionStringBuilder> class to build and modify connection strings. The class also makes it easy to manage connection strings stored in an application configuration file.
The <xref:Microsoft.Data.SqlClient.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** property or the <xref:Microsoft.Data.SqlClient.SqlConnectionStringBuilder.Remove%2A> method. See the <xref:Microsoft.Data.SqlClient.SqlConnection.ConnectionString%2A> property for a full list of acceptable synonyms.
The **Item** property handles tries 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:
```vb
Dim builder As New Microsoft.Data.SqlClient.SqlConnectionStringBuilder
builder("Data Source") = "(local)"
builder("Integrated Security") = True
builder("Initial Catalog") = "AdventureWorks;NewValue=Bad"
Console.WriteLine(builder.ConnectionString)
```
```csharp
Microsoft.Data.SqlClient.SqlConnectionStringBuilder builder =
new Microsoft.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
```
]]></format>
</remarks>
<example>
<format type="text/markdown">
<![CDATA[
The following console application builds connection strings for a SQL Server database. The code uses a <xref:Microsoft.Data.SqlClient.SqlConnectionStringBuilder> class to create the connection string, and then passes the <xref:System.Data.Common.DbConnectionStringBuilder.ConnectionString%2A> property of the <xref:Microsoft.Data.SqlClient.SqlConnectionStringBuilder> instance to the constructor of the connection class. The example also parses an existing connection string and demonstrates various ways of manipulating the connection string's contents.
> [!NOTE]
> This example includes a password to demonstrate how <xref:Microsoft.Data.SqlClient.SqlConnectionStringBuilder> works with connection strings. In your applications, we recommend that you use Windows Authentication. If you must use a password, do not include a hard-coded password in your application.
[!code-csharp[SqlConnectionStringBuilder#1](~/../sqlclient/doc/samples/SqlConnectionStringBuilder.cs#1)]
]]></format>
</example>
</SqlConnectionStringBuilder>
<ctor2>
<summary>
Initializes a new instance of the <see cref="T:Microsoft.Data.SqlClient.SqlConnectionStringBuilder" /> class.
</summary>
</ctor2>
<ctorConnectionString>
<param name="connectionString">
The basis for the object's internal connection information. Parsed into name/value pairs. Invalid key names raise <see cref="T:System.Collections.Generic.KeyNotFoundException" /> .
</param>
<summary>
Initializes a new instance of the <see cref="T:Microsoft.Data.SqlClient.SqlConnectionStringBuilder" /> class. The provided connection string provides the data for the instance's internal connection information.
</summary>
<remarks>
<format type="text/markdown">
<![CDATA[
The <xref:Microsoft.Data.SqlClient.SqlConnectionStringBuilder> class provides a fixed internal collection of key/value pairs. Even if you supply only a small subset of the possible connection string values in the constructor, the object always provides default values for each key/value pair. When the `ConnectionString` property of the object is retrieved, the string contains only key/value pairs in which the value is not the default value for the item.
]]></format>
</remarks>
<example>
<format type="text/markdown">
<![CDATA[
The following example supplies a simple SQL Server connection string in the <xref:Microsoft.Data.SqlClient.SqlConnectionStringBuilder> object's constructor, and then iterates through all the key/value pairs within the object. Note that the collection provides default values for each item. Also note that the <xref:Microsoft.Data.SqlClient.SqlConnectionStringBuilder> class converts synonyms for the well-known keys so that they are consistent with the well-known names.
> [!NOTE]
> This example includes a password to demonstrate how <xref:Microsoft.Data.SqlClient.SqlConnectionStringBuilder> works with connection strings. In your applications, we recommend that you use Windows Authentication. If you must use a password, do not include a hard-coded password in your application.
[!code-csharp[SqlConnectionStringBuilder3#1](~/../sqlclient/doc/samples/SqlConnectionStringBuilder3.cs#1)]
]]></format>
</example>
<exception cref="T:System.Collections.Generic.KeyNotFoundException">
Invalid key name within the connection string.
</exception>
<exception cref="T:System.FormatException">
Invalid value within the connection string (specifically, when a Boolean or numeric value was expected but not supplied).
</exception>
<exception cref="T:System.ArgumentException">
The supplied <paramref name="connectionString" /> is not valid.
</exception>
</ctorConnectionString>
<ApplicationIntent>
<summary>
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 <see cref="T:Microsoft.Data.SqlClient.ApplicationIntent" />. For more information about SqlClient support for Always On Availability Groups, see <see href="https://learn.microsoft.com/sql/connect/ado-net/sql/sqlclient-support-high-availability-disaster-recovery">SqlClient Support for High Availability, Disaster Recovery</see>.
</summary>
<value>
Returns the current value of the property.
</value>
<remarks>
<para>
This property corresponds to the <c>Application Intent</c> and <c>ApplicationIntent</c> keys within the connection string.
</para>
<para>
The default value is <c>ApplicationIntent.ReadWrite</c> .
</para>
</remarks>
<seealso href="https://learn.microsoft.com/sql/connect/ado-net/overview-sqlclient-driver">
Overview of the SqlClient driver
</seealso>
</ApplicationIntent>
<ApplicationName>
<summary>
Gets or sets the name of the application associated with the connection string.
</summary>
<value>
The name of the application. If no name has been supplied, "Framework Microsoft SqlClient Data Provider" when running on .NET Framework and "Core Microsoft SqlClient Data Provider" otherwise.
</value>
<remarks>
<para>
This property corresponds to the "Application Name" and "app" keys within the connection string.
</para>
<para>
An application name can be 128 characters or fewer.
</para>
</remarks>
<example>
<para>
The following example creates a new <see cref="T:Microsoft.Data.SqlClient.SqlConnectionStringBuilder" /> and assigns a connection string in the object's constructor. The code displays the parsed and recreated version of the connection string, and then modifies the <see cref="P:Microsoft.Data.SqlClient.SqlConnectionStringBuilder.ApplicationName" /> property of the object. Finally, the code displays the new connection string, including the new key/value pair.
</para>
<!-- SqlConnectionStringBuilder_ApplicationName -->
<code language="c#">
using System;
using System.Data;
using Microsoft.Data.SqlClient;
class Program
{
static void Main()
{
try
{
string connectString = "Server=(local);Initial Catalog=AdventureWorks;" +
"Integrated Security=true";
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectString);
Console.WriteLine("Original: " + builder.ConnectionString);
Console.WriteLine("ApplicationName={0}", builder.ApplicationName);
builder.ApplicationName = "My Application";
Console.WriteLine("Modified: " + builder.ConnectionString);
Console.WriteLine("Press any key to finish.");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
</code>
<para>
The sample displays the following text in the console window:
</para>
<code>
Original: Data Source=(local);Initial Catalog=AdventureWorks;Integrated Security=True;ApplicationName="Core Microsoft SqlClient Data Provider"
Modified: Data Source=(local);Initial Catalog=AdventureWorks;Integrated Security=True;Application Name="My Application"
</code>
</example>
<exception cref="T:System.ArgumentNullException">
To set the value to null, use <see cref="F:System.DBNull.Value" /> .
</exception>
</ApplicationName>
<AttachDBFilename>
<summary>
Gets or sets a string that contains the name of the primary data file. This includes the full path name of an attachable database.
</summary>
<value>
The value of the <see langword="AttachDBFilename" /> property, or <see cref="P:System.String.Empty" /> if no value has been supplied.
</value>
<remarks>
<para>
This property corresponds to the "AttachDBFilename", "extended properties", and "initial file name" keys within the connection string. <c>AttachDBFilename</c> is only supported for primary data files with an .mdf extension.
</para>
<para>
If the value of the AttachDBFileName key is specified in the connection string, the database is attached and becomes the default database for the connection. If this key is not specified and if the database was previously attached, the database will not be reattached. The previously attached database will be used as the default database for the connection. If this key is specified together with the AttachDBFileName key, the value of this key will be used as the alias. However, if the name is already used in another attached database, the connection will fail.
</para>
<para>
The path may be absolute or relative by using the DataDirectory substitution string. If DataDirectory is used, the database file must exist within a subdirectory of the directory pointed to by the substitution string. <b>Note:</b> Remote server, HTTP, and UNC path names are not supported.
</para>
<para>
The database name must be specified with the keyword 'database' (or one of its aliases) as in the following: <c>"AttachDbFileName=|DataDirectory|\data\YourDB.mdf;integrated security=true;database=YourDatabase"</c> An error will be generated if a log file exists in the same directory as the data file and the 'database' keyword is used when attaching the primary data file. In this case, remove the log file. Once the database is attached, a new log file will be automatically generated based on the physical path.
</para>
</remarks>
<example>
<para>
The following example creates a new <see cref="T:Microsoft.Data.SqlClient.SqlConnectionStringBuilder" /> instance, and sets the <c>AttachDBFilename</c> property in order to specify the name of an attached data file.
</para>
<!-- SqlConnectionStringBuilder_AttachDBFilename -->
<code language="c#">
using System;
using System.Data;
using Microsoft.Data.SqlClient;
class Program
{
static void Main()
{
try
{
string connectString =
"Server=(local);" +
"Integrated Security=true";
SqlConnectionStringBuilder builder =
new SqlConnectionStringBuilder(connectString);
Console.WriteLine("Original: " + builder.ConnectionString);
Console.WriteLine("AttachDBFileName={0}", builder.AttachDBFilename);
builder.AttachDBFilename = @"C:\MyDatabase.mdf";
Console.WriteLine("Modified: " + builder.ConnectionString);
using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
{
connection.Open();
// Now use the open connection.
Console.WriteLine("Database = " + connection.Database);
}
Console.WriteLine("Press any key to finish.");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
</code>
</example>
<exception cref="T:System.ArgumentNullException">
To set the value to null, use <see cref="F:System.DBNull.Value" /> .
</exception>
<seealso href="https://learn.microsoft.com/sql/connect/ado-net/connection-strings">
Working with Connection Strings
</seealso>
<seealso href="https://learn.microsoft.com/sql/connect/ado-net/overview-sqlclient-driver">
Overview of the SqlClient driver
</seealso>
</AttachDBFilename>
<AttestationProtocol>
<summary>
Gets or sets the value of Attestation Protocol.
</summary>
<value>
The attestation protocol.
</value>
<remarks>
When no value is specified, secure enclaves are disabled on the connection.
</remarks>
</AttestationProtocol>
<Authentication>
<summary>
Gets or sets the authentication method used for <see href="https://learn.microsoft.com/azure/azure-sql/database/authentication-aad-overview#connect-with-microsoft-entra-to-azure-sql-resources">Connect with Microsoft Entra to Azure SQL resources</see>.
</summary>
<value>
The authentication method of the connection string.
</value>
<remarks>
For more information, see <see href="https://learn.microsoft.com/sql/connect/ado-net/sql/azure-active-directory-authentication">Connect to Azure SQL with Microsoft Entra authentication and SqlClient</see>.
</remarks>
</Authentication>
<Clear>
<summary>
Clears the contents of the <see cref="T:Microsoft.Data.SqlClient.SqlConnectionStringBuilder" /> instance.
</summary>
<remarks>
The <see cref="M:System.Data.Common.DbConnectionStringBuilder.Clear" /> method removes all key/value pairs from the <see cref="T:Microsoft.Data.SqlClient.SqlConnectionStringBuilder" /> , and resets all corresponding properties. This includes setting the <see cref="P:System.Data.Common.DbConnectionStringBuilder.Count" /> property to 0, and setting the <see cref="P:System.Data.Common.DbConnectionStringBuilder.ConnectionString" /> property to an empty string.
</remarks>
<example>
<para>
The following example demonstrates calling the <see cref="M:System.Data.Common.DbConnectionStringBuilder.Clear" /> method. This example populates the <see cref="T:Microsoft.Data.SqlClient.SqlConnectionStringBuilder" /> with some key/value pairs, and then calls the <see cref="M:System.Data.Common.DbConnectionStringBuilder.Clear" /> method and shows the results.
</para>
<!-- SqlConnectionStringBuilder_Clear -->
<code language="c#">
using System;
using System.Data;
using Microsoft.Data.SqlClient;
class Program
{
static void Main()
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = "(local)";
builder.IntegratedSecurity = true;
builder.InitialCatalog = "AdventureWorks";
Console.WriteLine("Initial connection string: " + builder.ConnectionString);
builder.Clear();
Console.WriteLine("After call to Clear, count = " + builder.Count);
Console.WriteLine("Cleared connection string: " + builder.ConnectionString);
Console.WriteLine();
Console.WriteLine("Press Enter to continue.");
Console.ReadLine();
}
}
</code>
</example>
</Clear>
<ColumnEncryptionSetting>
<summary>
Gets or sets the column encryption settings for the connection string builder.
</summary>
<value>
The column encryption settings for the connection string builder.This property enables or disables <see href="https://learn.microsoft.com/sql/relational-databases/security/encryption/always-encrypted-database-engine">Always Encrypted</see> functionality for the connection.
</value>
</ColumnEncryptionSetting>
<CommandTimeout>
<summary>
The default wait time (in seconds) before terminating the attempt to execute a command and generating an error. The default is 30 seconds.
</summary>
<value>
The time in seconds to wait for the command to execute. The default is 30 seconds.
</value>
<remarks>
<para>
This property corresponds to the <c>Command Timeout</c> key within the <see cref="T:Microsoft.Data.SqlClient.SqlConnection" /> connection string.
</para>
<para>
Valid values are greater than or equal to 0 and less than or equal to 2147483647.
</para>
</remarks>
<exception cref="T:System.ArgumentException">
The value set is less than 0.
</exception>
</CommandTimeout>
<ConnectionReset>
<summary>
Obsolete. Gets or sets a Boolean value that indicates whether the connection is reset when drawn from the connection pool.
</summary>
<value>
The value of the <see cref="P:Microsoft.Data.SqlClient.SqlConnectionStringBuilder.ConnectionReset" /> property, or true if no value has been supplied.
</value>
<remarks>
This property corresponds to the "Connection Reset" key within the <see cref="T:Microsoft.Data.SqlClient.SqlConnection" /> connection string, which has been removed from version 3.5 SP1 of the .NET Framework.
</remarks>
</ConnectionReset>
<ConnectRetryCount>
<summary>
The number of reconnections attempted after identifying that there was an idle connection failure. This must be an integer between 0 and 255. The default value for non Azure endpoints is 1. For Azure SQL endpoints, the default is 2. Starting in version 5.x, for Azure SQL serverless or on demand endpoints, the default is 5 to improve connection success for connections to an idle or paused instance. Set to 0 to disable reconnecting on idle connection failures. An <see cref="T:System.ArgumentException" /> will be thrown if set to a value outside the allowed range.
</summary>
<remarks>
<format type="text/markdown">
<![CDATA[
This property corresponds to the "Connect Retry Count" key within the <xref:Microsoft.Data.SqlClient.SqlConnection> connection string.
> [!NOTE]
> Since version 5.x the default value for none Azure endpoints is 1 and for Azure SQL and Azure Synapse has increased to 2 and 5 to improve the recovery against on high demand Azure endpoints. It should be detected first, and Synapse could be detected as a regular Azure SQL DB endpoint.
]]>
</format>
</remarks>
</ConnectRetryCount>
<ConnectRetryInterval>
<summary>
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.
</summary>
<value>
Amount of time (in seconds) between each reconnection attempt after identifying that there was an idle connection failure.
</value>
<exception cref="T:System.ArgumentException">
Value is outside the allowed range.
</exception>
<remarks>
<para>
This property corresponds to the "Connect Retry Interval" key within the <see cref="T:Microsoft.Data.SqlClient.SqlConnection" /> connection string.
</para>
<para>
This value is applied after the first reconnection attempt. When a broken connection is detected, the client immediately attempts to reconnect; this is the first reconnection attempt and only occurs if <see cref="P:Microsoft.Data.SqlClient.SqlConnectionStringBuilder.ConnectRetryCount" /> is greater than 0. If the first reconnection attempt fails and <see cref="P:Microsoft.Data.SqlClient.SqlConnectionStringBuilder.ConnectRetryCount" /> is greater than 1, the client waits <c>ConnectRetryInterval</c> to try the second and subsequent reconnection attempts.
</para>
</remarks>
</ConnectRetryInterval>
<ConnectTimeout>
<summary>
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.
</summary>
<value>
The value of the <see cref="P:Microsoft.Data.SqlClient.SqlConnectionStringBuilder.ConnectTimeout" /> property, or 15 seconds if no value has been supplied.
</value>
<remarks>
<para>
This property corresponds to the "Connect Timeout", "connection timeout", and "timeout" keys within the connection string.
</para>
<para>
When opening a connection to a Azure SQL Database, set the connection timeout to 30 seconds. Valid values are greater than or equal to 0 and less than or equal to 2147483647.
</para>
</remarks>
<example>
<para>
The following example first displays the contents of a connection string that does not specify the "Connect Timeout" value, sets the <see cref="P:Microsoft.Data.SqlClient.SqlConnectionStringBuilder.ConnectTimeout" /> property, and then displays the new connection string.
</para>
<!-- SqlConnectionStringBuilder_ConnectTimeout -->
<code language="c#">
using System;
using System.Data;
using Microsoft.Data.SqlClient;
class Program
{
static void Main()
{
try
{
string connectString =
"Server=(local);Initial Catalog=AdventureWorks;" +
"Integrated Security=true";
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectString);
Console.WriteLine("Original: " + builder.ConnectionString);
Console.WriteLine("ConnectTimeout={0}",
builder.ConnectTimeout);
builder.ConnectTimeout = 100;
Console.WriteLine("Modified: " + builder.ConnectionString);
Console.WriteLine("Press any key to finish.");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
</code>
</example>
</ConnectTimeout>
<ContainsKey>
<param name="keyword">
The key to locate in the <see cref="T:Microsoft.Data.SqlClient.SqlConnectionStringBuilder" /> .
</param>
<summary>
Determines whether the <see cref="T:Microsoft.Data.SqlClient.SqlConnectionStringBuilder" /> contains a specific key.
</summary>
<returns>
<see langword="true" /> if the <see cref="T:Microsoft.Data.SqlClient.SqlConnectionStringBuilder" /> contains an element that has the specified key; otherwise, <see langword="false" />.
</returns>
<remarks>
<para>
Because the <see cref="T:Microsoft.Data.SqlClient.SqlConnectionStringBuilder" /> contains a fixed-size collection of key/value pairs, the <see cref="M:System.Data.Common.DbConnectionStringBuilder.ContainsKey(System.String)" /> method determines only if a particular key name is valid.
</para>
</remarks>
<example>
<para>
The following example creates a <see cref="T:Microsoft.Data.SqlClient.SqlConnectionStringBuilder" /> instance, sets some of its properties, and then tries to determine whether various keys exist within the object by calling the <b>ContainsKey</b> method.
</para>
<!-- SqlConnectionStringBuilder_ContainsKey -->
<code language="c#">
using System;
using System.Data;
using Microsoft.Data.SqlClient;
class Program
{
static void Main()
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(GetConnectionString());
Console.WriteLine("Connection string = " + builder.ConnectionString);
// Keys you have provided return true.
Console.WriteLine(builder.ContainsKey("Server"));
// Comparison is case-insensitive, and synonyms
// are automatically converted to their "well-known"
// names.
Console.WriteLine(builder.ContainsKey("Database"));
// Keys that are valid but have not been set return true.
Console.WriteLine(builder.ContainsKey("Max Pool Size"));
// Keys that do not exist return false.
Console.WriteLine(builder.ContainsKey("MyKey"));
Console.WriteLine("Press Enter to continue.");
Console.ReadLine();
}
private static string GetConnectionString()
{
// To avoid storing the connection string in your code,
// you can retrieve it from a configuration file.
return "Server=(local);Integrated Security=SSPI;" +
"Initial Catalog=AdventureWorks";
}
}
</code>
<para>
The example displays the following output in the console window:
</para>
<code>
Connection string = Data Source=(local);Initial Catalog=AdventureWorks;Integrated Security=True
True
True
True
False
</code>
</example>
<exception cref="T:System.ArgumentNullException"><paramref name="keyword" /> is null (<see langword="Nothing" /> in Visual Basic)</exception>
</ContainsKey>
<CurrentLanguage>
<summary>
Gets or sets the language used for database server warning or error messages..
</summary>
<value>
The value of the <see cref="P:Microsoft.Data.SqlClient.SqlConnectionStringBuilder.CurrentLanguage" /> property, or <c>string.Empty</c> if no value has been supplied.
</value>
<remarks>
<para>
This property corresponds to the "Current Language" and "language" keys within the connection string.
</para>
<para>
The language name can be 128 characters or fewer.
</para>
</remarks>
<exception cref="T:System.ArgumentNullException">
To set the value to null, use <see cref="F:System.DBNull.Value" /> .
</exception>
</CurrentLanguage>
<DataSource>
<summary>
Gets or sets the name or network address of the instance of SQL Server to connect to.
</summary>
<value>
The value of the <see cref="P:Microsoft.Data.SqlClient.SqlConnectionStringBuilder.DataSource" /> property, or <see langword="String.Empty" /> if none has been supplied.
</value>
<remarks>
<para>
This property corresponds to the "Data Source", "server", "address", "addr", and "network address" keys within the connection string. Regardless of which of these values has been supplied within the supplied connection string, the connection string created by the <c>SqlConnectionStringBuilder</c> will use the well-known "Data Source" key. The port number can be specified after the server name: <c>server=tcp:servername,port</c> .
</para>
<para>
When specifying a local instance, always use (local). To force a protocol, add one of the following prefixes: <c>np:(local), tcp:(local), lpc:(local)</c> .
</para>
<para>
You can also connect to a LocalDB database as follows: <c>server=(localdb)\\myInstance</c> . For more information about LocalDB, see <see href="https://learn.microsoft.com/sql/connect/ado-net/sql/sqlclient-support-localdb">SqlClient Support for LocalDB</see> . <b>Data Source</b> must use the TCP format or the Named Pipes format. TCP format is as follows:
</para>
<list type="bullet">
<item><description><c>tcp:\<host name>\\<instance name></c></description></item>
<item><description><c>tcp:\<host name>,\<TCP/IP port number></c></description></item>
</list>
<para>
The TCP format must start with the prefix "tcp:" and is followed by the database instance, as specified by a host name and an instance name. This format is not applicable when connecting to Azure SQL Database. TCP is automatically selected for connections to Azure SQL Database when no protocol is specified.
</para>
<para>
The host name MUST be specified in one of the following ways:
</para>
<list type="bullet">
<item><description>NetBIOSName</description></item>
<item><description>IPv4Address</description></item>
<item><description>IPv6Address</description></item>
</list>
<para>
The instance name is used to resolve to a particular TCP/IP port number on which a database instance is hosted. Alternatively, specifying a TCP/IP port number directly is also allowed. If both instance name and port number are not present, the default database instance is used.
</para>
<para>
The Named Pipes format is as follows: -
</para>
<list type="bullet">
<item><description><c>np:\\\\<host name>\pipe\\<pipe name></c></description></item>
</list>
<para>
The Named Pipes format MUST start with the prefix "np:" and is followed by a named pipe name.
</para>
<para>
The host name MUST be specified in one of the following ways:
</para>
<list type="bullet">
<item><description>NetBIOSName</description></item>
<item><description>IPv4Address</description></item>
<item><description>IPv6Address</description></item>
</list>
<para>
The pipe name is used to identify the database instance to which the .NET application will connect.
</para>
<para>
If the value of the <b>Network</b> key is specified, the prefixes "tcp:" and "np:" should not be specified. <b>Note:</b> You can force the use of TCP instead of shared memory, either by prefixing <b>tcp:</b> to the server name in the connection string, or by using <b>localhost</b>.
</para>
</remarks>
<example>
<para>
The following example demonstrates that the <see cref="T:Microsoft.Data.SqlClient.SqlConnectionStringBuilder" /> class converts synonyms for the "Data Source" connection string key into the well-known key:
</para>
<!-- SqlConnectionStringBuilder_DataSource -->
<code language="c#">
using System;
using System.Data;
using Microsoft.Data.SqlClient;
class Program
{
static void Main()
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(
"Network Address=(local);Integrated Security=SSPI;" +
"Initial Catalog=AdventureWorks");
// Display the connection string, which should now
// contain the "Data Source" key, as opposed to the
// supplied "Network Address".
Console.WriteLine(builder.ConnectionString);
// Retrieve the DataSource property.
Console.WriteLine("DataSource = " + builder.DataSource);
Console.WriteLine("Press any key to continue.");
Console.ReadLine();
}
}
</code>
</example>
<exception cref="T:System.ArgumentNullException">
To set the value to null, use <see cref="F:System.DBNull.Value" /> .
</exception>
</DataSource>
<EnclaveAttestationUrl>
<summary>
Gets or sets the enclave attestation URL to be used with enclave based Always Encrypted.
</summary>
<value>
The enclave attestation URL.
</value>
</EnclaveAttestationUrl>
<Encrypt>
<summary>
Gets or sets a <see cref="T:Microsoft.Data.SqlClient.SqlConnectionEncryptOption" /> value since version 5.0 or a <see cref="T:System.Boolean" /> value for the earlier versions that indicates whether TLS encryption is required for all data sent between the client and server.
</summary>
<value>
The value of the <see cref="P:Microsoft.Data.SqlClient.SqlConnectionStringBuilder.Encrypt" /> property.
</value>
<remarks>
<format type="text/markdown">
<.
> [!NOTE]
> Starting from **version 4.0**, the default value of the property `Encrypt` is set to `true` while it is `false` for earlier versions.
> [!NOTE]
> Starting from **version 5.0**, the data type is updated to <xref:Microsoft.Data.SqlClient.SqlConnectionEncryptOption>, and the default value of the `Encrypt` property is set to <xref:Microsoft.Data.SqlClient.SqlConnectionEncryptOption.Mandatory%2A>.
]]></format>
</remarks>
<seealso href="https://learn.microsoft.com/sql/connect/ado-net/connection-strings">
Working with Connection Strings
</seealso>
<seealso href="https://learn.microsoft.com/sql/connect/ado-net/overview-sqlclient-driver">
Overview of the SqlClient driver
</seealso>
</Encrypt>
<Enlist>
<summary>
Gets or sets a Boolean value that indicates whether the SQL Server connection pool automatically enlists the connection in the creation thread's current transaction context.
</summary>
<value>
The value of the <see cref="P:Microsoft.Data.SqlClient.SqlConnectionStringBuilder.Enlist" /> property, or <see langword="true" /> if none has been supplied.
</value>
<remarks>
This property corresponds to the "Enlist" key within the connection string.
</remarks>
</Enlist>
<FailoverPartner>
<summary>
Gets or sets the name or address of the partner server to connect to if the primary server is down.
</summary>
<value>
The value of the <see cref="P:Microsoft.Data.SqlClient.SqlConnectionStringBuilder.FailoverPartner" /> property, or <see langword="String.Empty" /> if none has been supplied.
</value>
<exception cref="T:System.ArgumentNullException">
To set the value to null, use <see cref="F:System.DBNull.Value" /> .
</exception>
<remarks>
<para>
If the value of this key is "", then <b>Initial Catalog</b> must be present, and its value must not be "".
</para>
<para>
The server name can be 128 characters or fewer.
</para>
<para>
If you specify a failover partner but the failover partner server is not configured for database mirroring and the primary server (specified with the Server keyword) is not available, then the connection will fail.
</para>
<para>
If you specify a failover partner and the primary server is not configured for database mirroring, the connection to the primary server (specified with the Server keyword) will succeed if the primary server is available.
</para>
</remarks>
</FailoverPartner>
<FailoverPartnerSPN>
<summary>
Gets or sets the service principal name (SPN) of the failover partner for the connection.
</summary>
<value>
The value of the <see cref="P:Microsoft.Data.SqlClient.SqlConnectionStringBuilder.FailoverPartnerSPN" /> property, or <see langword="String.Empty" /> if none has been supplied.
</value>
<remarks>
<format type="text/markdown">
<![CDATA[
This property corresponds to the "FailoverPartnerSPN" and "Failover Partner SPN" keys within the connection string.
> [!NOTE]
> This property only applies when using Integrated Security mode, otherwise it is ignored.
]]>
</format>
</remarks>
</FailoverPartnerSPN>
<HostNameInCertificate>
<summary>
Gets or sets the host name to use when validating the server certificate for the connection. When not specified, the server name from the <c>Data Source</c> is used for certificate validation. (Only available in v5.0+)
</summary>
<value>
The value of the <see cref="P:Microsoft.Data.SqlClient.SqlConnectionStringBuilder.HostNameInCertificate" /> property, or <see langword="String.Empty" /> if none has been supplied.
</value>
<remarks>
<format type="text/markdown">
<![CDATA[
This property corresponds to the "HostNameInCertificate" and "Host Name in Certificate" keys within the connection string.
> [!NOTE]
> This property only applies when using `Encrypt` in <xref:Microsoft.Data.SqlClient.SqlConnectionEncryptOption.Mandatory%2A> or <xref:Microsoft.Data.SqlClient.SqlConnectionEncryptOption.Strict%2A> mode, otherwise it is ignored.
]]>
</format>
</remarks>
</HostNameInCertificate>
<InitialCatalog>
<summary>
Gets or sets the name of the database associated with the connection.
</summary>
<value>
The value of the <see cref="P:Microsoft.Data.SqlClient.SqlConnectionStringBuilder.InitialCatalog" /> property, or <c>string.Empty</c> if none has been supplied.
</value>
<remarks>
<para>
This property corresponds to the "Initial Catalog" and "database" keys within the connection string.
</para>
<para>
The database name can be 128 characters or fewer.
</para>
</remarks>
<example>
<para>
The following example creates a simple connection string and then uses the <see cref="T:Microsoft.Data.SqlClient.SqlConnectionStringBuilder" /> class to add the name of the database to the connection string. The code displays the contents of the <see cref="P:Microsoft.Data.SqlClient.SqlConnectionStringBuilder.InitialCatalog" /> property, just to verify that the class was able to convert from the synonym ("Database") to the appropriate property value.
</para>
<!-- SqlConnectionStringBuilder_InitialCatalog -->
<code language="c#">
using System;
using System.Data;
using Microsoft.Data.SqlClient;
class Program
{
static void Main()
{
try
{
string connectString = "Data Source=(local);" +
"Integrated Security=true";
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectString);
Console.WriteLine("Original: " + builder.ConnectionString);
// Normally, you could simply set the InitialCatalog
// property of the SqlConnectionStringBuilder object. This
// example uses the default Item property (the C# indexer)
// and the "Database" string, simply to demonstrate that
// setting the value in this way results in the same
// connection string:
builder["Database"] = "AdventureWorks";
Console.WriteLine("builder.InitialCatalog = " + builder.InitialCatalog);
Console.WriteLine("Modified: " + builder.ConnectionString);
using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
{
connection.Open();
// Now use the open connection.
Console.WriteLine("Database = " + connection.Database);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine("Press any key to finish.");
Console.ReadLine();
}
}
</code>
</example>
<exception cref="T:System.ArgumentNullException">
To set the value to null, use <see cref="F:System.DBNull.Value" /> .
</exception>
</InitialCatalog>
<IntegratedSecurity>
<summary>
Gets or sets a Boolean value that indicates whether User ID and Password are specified in the connection (when <see langword="false" /> ) or whether the current Windows account credentials are used for authentication (when <see langword="true" /> ).
</summary>
<value>
The value of the <see cref="P:Microsoft.Data.SqlClient.SqlConnectionStringBuilder.IntegratedSecurity" /> property, or <see langword="false" /> if none has been supplied.
</value>
<remarks>
<format type="text/markdown">
<![CDATA[
This property corresponds to the "Integrated Security" and "trusted_connection" keys within the connection string.
If User ID and Password are specified and Integrated Security is set to true, the User ID and Password will be ignored and Integrated Security will be used.
<xref:Microsoft.Data.SqlClient.SqlCredential> is a more secure way to specify credentials for a connection that uses SQL Server Authentication (`Integrated Security=false`).
]]></format>
</remarks>
<example>
<format type="text/markdown">
<![CDATA[
The following example converts an existing connection string from using SQL Server Authentication to using integrated security. The example does its work by removing the user name and password from the connection string and then setting the <xref:Microsoft.Data.SqlClient.SqlConnectionStringBuilder.IntegratedSecurity%2A> property of the <xref:Microsoft.Data.SqlClient.SqlConnectionStringBuilder> object.
> [!NOTE]
> This example includes a password to demonstrate how <xref:Microsoft.Data.SqlClient.SqlConnectionStringBuilder> works with connection strings. In your applications, we recommend that you use Windows Authentication. If you must use a password, do not include a hard-coded password in your application.
[!code-csharp[SqlConnectionStringBuilder_IntegratedSecurity#1](~/../sqlclient/doc/samples/SqlConnectionStringBuilder_IntegratedSecurity.cs#1)]
]]></format>
</example>
</IntegratedSecurity>
<IPAddressPreference>
<summary>
Gets or sets the IP address family preference when establishing TCP connections.
</summary>
<returns>
Returns the IP address preference.
</returns>
<remarks>
If <c>Transparent Network IP Resolution</c> (in .NET Framework) or <c>Multi Subnet Failover</c> is set to true, this setting has no effect.
</remarks>
</IPAddressPreference>
<IsFixedSize>
<summary>
Gets a value that indicates whether the <see cref="T:Microsoft.Data.SqlClient.SqlConnectionStringBuilder" /> has a fixed size.
</summary>
<value>
<see langword="true" /> in every case, because the <see cref="T:Microsoft.Data.SqlClient.SqlConnectionStringBuilder" /> supplies a fixed-size collection of key/value pairs.
</value>
<seealso href="https://learn.microsoft.com/sql/connect/ado-net/connection-strings">
Working with Connection Strings
</seealso>
<seealso href="https://learn.microsoft.com/sql/connect/ado-net/overview-sqlclient-driver">
Overview of the SqlClient driver
</seealso>
</IsFixedSize>
<Item>
<param name="keyword">
The key of the item to get or set.
</param>
<summary>
Gets or sets the value associated with the specified key. In C#, this property is the indexer.
</summary>
<value>
The value associated with the specified key.
</value>
<remarks>
Because the <see cref="T:Microsoft.Data.SqlClient.SqlConnectionStringBuilder" /> contains a fixed-size dictionary, trying to add a key that does not exist within the dictionary throws a <see cref="T:System.Collections.Generic.KeyNotFoundException" />.
</remarks>
<example>
<para>
The following code, in a console application, creates a new <see cref="T:Microsoft.Data.SqlClient.SqlConnectionStringBuilder" /> and adds key/value pairs to its connection string, using the <see cref="P:Microsoft.Data.SqlClient.SqlConnectionStringBuilder.Item(System.String)" /> property.
</para>
<!-- SqlConnectionStringBuilder2 -->
<code language="c#">
using System;
using System.Data;
using Microsoft.Data.SqlClient;
class Program
{
static void Main()
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder["Data Source"] = "(local)";
builder["Integrated Security"] = true;
builder["Initial Catalog"] = "AdventureWorks";
// Overwrite the existing value for the Data Source value.
builder["Data Source"] = ".";
Console.WriteLine(builder.ConnectionString);
Console.WriteLine();
Console.WriteLine("Press Enter to continue.");
Console.ReadLine();
}
}
</code>
</example>
<exception cref="T:System.ArgumentNullException">
<paramref name="keyword" /> is a null reference (<see langword="Nothing" /> in Visual Basic).
</exception>
<exception cref="T:System.Collections.Generic.KeyNotFoundException">
Tried to add a key that does not exist within the available keys.
</exception>
<exception cref="T:System.FormatException">
Invalid value within the connection string (specifically, a Boolean or numeric value was expected but not supplied).
</exception>
</Item>
<Keys>
<summary>
Gets an <see cref="T:System.Collections.ICollection" /> that contains the keys in the <see cref="T:Microsoft.Data.SqlClient.SqlConnectionStringBuilder" />.
</summary>
<value>
An <see cref="T:System.Collections.ICollection" /> that contains the keys in the <see cref="T:Microsoft.Data.SqlClient.SqlConnectionStringBuilder" />.
</value>
<remarks>
The order of the values in the <see cref="T:System.Collections.ICollection" /> is unspecified, but it is the same order as the associated values in the <see cref="T:System.Collections.ICollection" /> returned by the <see cref="P:Microsoft.Data.SqlClient.SqlConnectionStringBuilder.Values" /> property.
</remarks>
<example>
<para>
The following console application example creates a new <see cref="T:Microsoft.Data.SqlClient.SqlConnectionStringBuilder" />. The code loops through the <see cref="T:System.Collections.ICollection" /> returned by the <see cref="P:Microsoft.Data.SqlClient.SqlConnectionStringBuilder.Keys" /> property displaying the key/value pairs.
</para>
<!-- SqlConnectionStringBuilder_Keys -->
<code language="c#">
using System;
using System.Data;
using Microsoft.Data.SqlClient;
class Program
{
static void Main()
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = "(local)";
builder.IntegratedSecurity = true;
builder.InitialCatalog = "AdventureWorks";
// Loop through the collection of keys, displaying
// the key and value for each item:
foreach (string key in builder.Keys)
{
Console.WriteLine("{0}={1}", key, builder[key]);
}
Console.WriteLine();
Console.WriteLine("Press Enter to continue.");
Console.ReadLine();
}
}
</code>
</example>
<seealso cref="P:Microsoft.Data.SqlClient.SqlConnectionStringBuilder.Values" />
<seealso cref="P:Microsoft.Data.SqlClient.SqlConnectionStringBuilder.Item(System.String)" />
</Keys>
<LoadBalanceTimeout>
<summary>
Gets or sets the minimum time, in seconds, for the connection to live in the connection pool before being destroyed.
</summary>
<value>
The value of the <see cref="P:Microsoft.Data.SqlClient.SqlConnectionStringBuilder.LoadBalanceTimeout" /> property, or 0 if none has been supplied.
</value>
<remarks>
<para>
This property corresponds to the "Load Balance Timeout" and "connection lifetime" keys within the connection string.
</para>
<para>
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 <c>Connection Lifetime</c>. This is useful in clustered configurations to force load balancing between a running server and a server just brought online.
</para>
<para>
A value of zero (0) causes pooled connections to have the maximum connection timeout.
</para>
</remarks>
</LoadBalanceTimeout>
<IdleTimeout>
<summary>
Gets or sets the maximum time, in seconds, that a connection can sit unused (idle) in the connection pool before it is discarded. The default is 300 (5 minutes).
</summary>
<value>
The idle timeout for pooled connections, in seconds.
</value>
<remarks>
<para>
This property corresponds to the "Connection Idle Timeout" key within the connection string.
</para>
<para>
In versions where the AppContext switch <c>Switch.Microsoft.Data.SqlClient.UseLegacyIdleTimeoutBehavior</c> is enabled (the default), the driver preserves historical pooling behavior and does not enforce this setting. Set the switch to <see langword="false" /> to enable idle-timeout enforcement.
</para>
<para>
The driver makes a best effort to discard connections that have remained idle in the pool for longer than this value. The exact point in the connection lifecycle at which the check occurs is an implementation detail and may change over time. This protects callers from receiving connections that may have been silently closed by firewalls, load balancers, or server-side inactivity thresholds.
</para>
<para>
A value of zero (0) disables idle expiration; connections are kept in the pool indefinitely (subject to other expiry rules such as <see cref="P:Microsoft.Data.SqlClient.SqlConnectionStringBuilder.LoadBalanceTimeout" />).