Saturday, March 19, 2016

Parsing .NET Connection Strings

Have you ever wanted to get a piece of a connection string? Like say the database? There’s no need to grab IndexOf and Substring and try it parse it yourself. .Net already has the ability to parse connection string using the StringConnectionBuilder:
using System.Data.SqlClient;
var conString = "Server=myServerAddress;Database=Customers;User Id=tim;Password=lassie;";
var s = new SqlConnectionStringBuilder(conString); var datasource = s.DataSource;   // "myServerAddress" var database = s.InitialCatalog; // "Customers" var username = s.UserID;         // "tim" var password = s.Password;       // "lassie"

No comments: