Just Code‎ > ‎

WPF - A simple GUI for creating SQL Connection Strings

posted Jan 24, 2012, 6:22 AM by Peter Henell   [ updated Jan 25, 2012, 5:10 AM ]
Based on the very competent component created by Jake Ginnivan:

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();
        }

       
    }
}
ċ
ConnectionStringCreatorGUI.zip
(116k)
Peter Henell,
Jan 24, 2012, 6:22 AM
Comments