WPF - A simple GUI for creating SQL Connection Strings

Post date: Jan 24, 2012 2:22:46 PM

Based on the very competent component created by Jake Ginnivan:

http://jake.ginnivan.net/wpf-sql-connection-user-control

I wanted something much simpler that did not require any installation of SMO for example. This GUI does nothing for you automatically. You need to fill in all the fields by hand, it does not search the network for servers like the component made by Jake.

The attached file contain a project that is the only thing that is required to use this component as part of your project. The download link can be found the bottom of this post.

I use this component in Console applications where I require a connection string.

Example when used:

Example of the GUI of ConnectionStringBuilder

The following console app will start up this GUI and use the connectionstring that was created from it.

You need to add the following references to be able to use the GUI from a Console application:

    • WindowsBase
    • PresentationCore
    • PresentationFramework
    • System.Xaml

using System;using System.Windows;namespace TestConsole { class Program { [STAThread] // Important to have this when starting WPF Applications from within a Console app. static void Main(string[] args) { // This connStr object can be stored in a Settings file to make the user not have to write the same information every time. ConnectionStringCreatorGUI.SqlConnectionString connStr = new ConnectionStringCreatorGUI.SqlConnectionString(); // The constructor of the GUI takes two parameters // 1: a SqlConnectionString object, in case you want to save that information in a Settings file or simular // 2: An Action<SqlConnectionString>, that is the callback method that will be called when the user presses OK in the GUI Window win = new ConnectionStringCreatorGUI.ConnectionStringBuilderWindow(connStr, conn => { // This is the body of the callback that will be called when the user have pressed OK on the GUI // conn is the SqlConnectionString object that have been created // Now use the SqlConnectionString object to do what you need ExecuteProgramThatRequiresSQLConnection(conn); }); // Start the GUI to create the SqlConnectionString object Application app = new Application(); app.Run(win); } private static void ExecuteProgramThatRequiresSQLConnection(ConnectionStringCreatorGUI.SqlConnectionString x) { throw new NotImplementedException(); } }}