A while ago I needed to shorten SqlClient ConnectionStrings. One way to do that is to use the shortest Key for each property (and not use the default key names that are much longer).
I beefed up the code to show you both the shortest and all equivalent keys (a few of the Microsoft exams want you to memorize most of these).
The HTML table below (similar to the huge and therefore hard to read table on MSDN) comes directly from the C# code at the bottom of the post. The only post-editing I did was making the header row bold.
| Key | ShortesEquivalentKey | EquivalentKeys |
| Application Name | app | Application Name,app |
| ApplicationIntent | ApplicationIntent | ApplicationIntent |
| Asynchronous Processing | async | Asynchronous Processing,async |
| AttachDbFilename | AttachDbFilename | AttachDbFilename,extended properties,initial file name |
| Connect Timeout | timeout | Connect Timeout,connection timeout,timeout |
| Connection Reset | Connection Reset | Connection Reset |
| Context Connection | Context Connection | Context Connection |
| Current Language | language | Current Language,language |
| Data Source | addr | Data Source,addr,address,network address,server |
| Encrypt | Encrypt | Encrypt |
| Enlist | Enlist | Enlist |
| Failover Partner | Failover Partner | Failover Partner |
| Initial Catalog | database | Initial Catalog,database |
| Integrated Security | trusted_connection | Integrated Security,trusted_connection |
| Load Balance Timeout | connection lifetime | Load Balance Timeout,connection lifetime |
| Max Pool Size | Max Pool Size | Max Pool Size |
| Min Pool Size | Min Pool Size | Min Pool Size |
| MultipleActiveResultSets | MultipleActiveResultSets | MultipleActiveResultSets |
| MultiSubnetFailover | MultiSubnetFailover | MultiSubnetFailover |
| Network Library | net | Network Library,net,network |
| Packet Size | Packet Size | Packet Size |
| Password | pwd | Password,pwd |
| Persist Security Info | persistsecurityinfo | Persist Security Info,persistsecurityinfo |
| Pooling | Pooling | Pooling |
| Replication | Replication | Replication |
| Transaction Binding | Transaction Binding | Transaction Binding |
| TrustServerCertificate | TrustServerCertificate | TrustServerCertificate |
| Type System Version | Type System Version | Type System Version |
| User ID | uid | User ID,uid,user |
| User Instance | User Instance | User Instance |
| Workstation ID | wsid | Workstation ID,wsid |
The code below uses a few techniques referenced as StackOverflow links:
- Sorting enumerable strings using LINQ.
- Generating CSV from an enumerable strings using LINQ and string.Join.
- Converting a DataTable to an HTML Table using an ASP.NET DataGrid and HtmlTextWriter to do the rendering.
- Getting a private static field by name using reflection.
Both the main program and the SqlConnectionStringBuilderHelper class are less than 70 lines of code (each about 50 when excluding comments and empty lines).
The SqlConnectionStringBuilderHelper uses the internals of the SqlConnectionStringBuilder class (all DbConnectionStringBuilder descendants I have seen work in a similar way):
- each DbConnectionStringBuilder instance has a public Keys property that exposes the static _validKeywords field of the descendant. It contains all possible keys that can appear in a generated ConnectionString.
- the SqlConnectionStringBuilder class (and other DbConnectionStringBuilder descendants) has a static private property _keywords that maps all possible keyword strings (including equivalents) to an enumerator (which indexes into the Keys property).
Mono uses the same mechanism. - The trick is to walk the _keywords property and search for equivalent keywords.
- For a list of equivalent keywords, you find the shortest one.
Related:
Persist Security Info: [WayBack] c# – Persist Security Info Property=true and Persist Security Info Property=false – Stack OverflowNetwork Library:OLE DB Services:
Enjoy the code: Read the rest of this entry »





