|
1 |
| - |
2 |
| - |
3 |
| -using System; |
4 |
| -// <Snippet1> |
5 |
| -using System.Data; |
| 1 | +using System; |
6 | 2 | using System.Data.SqlClient;
|
7 | 3 |
|
8 | 4 | class Program
|
9 | 5 | {
|
10 | 6 | static void Main()
|
11 | 7 | {
|
| 8 | + // <Snippet1> |
12 | 9 | // Create a new SqlConnectionStringBuilder and
|
13 | 10 | // initialize it with a few name/value pairs.
|
14 |
| - SqlConnectionStringBuilder builder = |
15 |
| - new SqlConnectionStringBuilder(GetConnectionString()); |
| 11 | + SqlConnectionStringBuilder builder = new( |
| 12 | + "Server=(local);Integrated Security=true;" + |
| 13 | + "Initial Catalog=AdventureWorks" |
| 14 | + ); |
16 | 15 |
|
17 | 16 | // The input connection string used the
|
18 | 17 | // Server key, but the new connection string uses
|
19 | 18 | // the well-known Data Source key instead.
|
20 |
| - Console.WriteLine(builder.ConnectionString); |
21 |
| - |
22 |
| - // Pass the SqlConnectionStringBuilder an existing |
23 |
| - // connection string, and you can retrieve and |
24 |
| - // modify any of the elements. |
25 |
| - builder.ConnectionString = "server=(local);user id=ab;" + |
26 |
| - "password= a!Pass113;initial catalog=AdventureWorks"; |
| 19 | + Console.WriteLine($"Original connection string: '{builder.ConnectionString}'"); |
27 | 20 |
|
28 | 21 | // Now that the connection string has been parsed,
|
29 | 22 | // you can work with individual items.
|
30 |
| - Console.WriteLine(builder.Password); |
31 |
| - builder.Password = "new@1Password"; |
| 23 | + Console.WriteLine($"Initial catalog: '{builder.InitialCatalog}'"); |
| 24 | + builder.InitialCatalog = "Northwind"; |
32 | 25 | builder.AsynchronousProcessing = true;
|
33 | 26 |
|
34 | 27 | // You can refer to connection keys using strings,
|
35 | 28 | // as well. When you use this technique (the default
|
36 | 29 | // Item property in Visual Basic, or the indexer in C#),
|
37 |
| - // you can specify any synonym for the connection string key |
38 |
| - // name. |
| 30 | + // you can specify any synonym for the connection string key name. |
39 | 31 | builder["Server"] = ".";
|
40 | 32 | builder["Connect Timeout"] = 1000;
|
41 | 33 | builder["Trusted_Connection"] = true;
|
42 |
| - Console.WriteLine(builder.ConnectionString); |
43 |
| - |
44 |
| - Console.WriteLine("Press Enter to finish."); |
45 |
| - Console.ReadLine(); |
46 |
| - } |
47 |
| - |
48 |
| - private static string GetConnectionString() |
49 |
| - { |
50 |
| - // To avoid storing the connection string in your code, |
51 |
| - // you can retrieve it from a configuration file. |
52 |
| - return "Server=(local);Integrated Security=SSPI;" + |
53 |
| - "Initial Catalog=AdventureWorks"; |
| 34 | + Console.WriteLine($"Modified connection string: '{builder.ConnectionString}'"); |
| 35 | + // </Snippet1> |
54 | 36 | }
|
55 | 37 | }
|
56 |
| -// </Snippet1> |
0 commit comments