[Perhaps old news for some, but I've found there's enough people I've bumped into who haven't heard of this to make it worth mentioning.]

In ADO.NET 2.0, there is a new class, SqlConnectionStringBuilder that can both create a connection string from strongly-typed properties or parse properties from an existing connection string.  It inherits from the base DBConnectionStringBuilder and there are other classes for different systems as well (e.g. ODBC and Oracle.)

Here's a simple console-based sample:




  static void Main(string[] args)


    //Compose and use a connection string

    SqlConnectionStringBuilder csb = new SqlConnectionStringBuilder();

    csb.DataSource = @"LOCALHOST\SQLEXPRESS";

    csb.InitialCatalog = "SomeDatabase";

    csb.IntegratedSecurity = true;

    Console.WriteLine("Generated Connection String = " + csb.ConnectionString);

    using (SqlConnection cn = new SqlConnection(csb.ConnectionString))



        Console.WriteLine("Connection is " + cn.State);



    //Parse an existing connection string

    const string CONNECTION = "Server=myServerAddress;Database=myDataBase;User ID=myUsername;Password=myPassword;Trusted_Connection=False;Packet Size=4096;";

    SqlConnectionStringBuilder csb2 = new SqlConnectionStringBuilder(CONNECTION);

    Console.WriteLine("Server = " + csb2.DataSource);

    Console.WriteLine("Database = " + csb2.InitialCatalog);

    Console.WriteLine("User = " + csb2.UserID);

    Console.WriteLine("Password = " + csb2.Password);

    Console.WriteLine("Packet Size = " + csb2.PacketSize);



Also note that if you need assistance with connection string formats, ConnectionStrings.com is a good reference site with plenty of examples.