Many of the apps I have written need to target multiple databases or I want the connection to be configurable by the user. I have a user control in my common library that I use all the time to let the user connect to a database.

You will need the Sql Management Objects from http://www.microsoft.com/Downloads/details.aspx?familyid=B33D2C78-1059-4CE2-B80D-2343C099BCB4&displaylang=en to compile the demo project.

I also make use of http://umbrella.codeplex.com/ and http://www.codeplex.com/clinq in my common libraries, it should be quite simple to remove the dependencies on these two libraries if you wanted to. The first library adds a heap of really useful extension methods, like AddRange to collections that do not have it implemented. The second is all about reactive linq queries, if the underlying ObservableCollection changes any linq queries are automatically re-evaluated.

Features:

  • Finds database instances on your Lan, these are stored statically so will only be loaded once during the application lifetime.
  • Will populate the databases on that SQL server.
  • Is easily extendable, you can expose other options within the control if you want.
  • Has a cool little loading circle I built included :P
  • It uses a SqlConnectionString object which is much nicer than dealing with a SqlConnectionStringBuilder or a string. SqlConnectionString is also implicitly converts to a string so you can pass it directly to your SqlConnection constructor without .ToString() or casting.

All in all, for small apps I think this makes connecting to a SQL database really easy!

Here are some examples of the control in use.

image image image image

And to show off the ability to add child content to the control:


  
      
      Test Connection
  

image image

To add any options you want just inherit from or edit SqlConnectionBuilderViewModel.cs to add your own custom properties then bind to them.

Adding child content

It took me a little while to figure out how to force the content to be positioned in a certain place so I will quickly cover that.

I started off by creating a Footer DependencyProperty on the UserControl (I felt this was more a UI thing, so it is not in the ViewModel):

public static readonly DependencyProperty FooterProperty =
    DependencyProperty.Register("Footer",
                                typeof(FrameworkElement),
                                typeof(SqlConnectionBuilder));

public FrameworkElement Footer
{
    get
    {
        return (FrameworkElement)GetValue(FooterProperty);
    }
    set
    {
        SetValue(FooterProperty, value);
    }
}

And decorated the user control class with:

[ContentProperty("Footer")]

This means that if you set the content on the SqlConnectionBuilder then it will be assigned to the Footer property rather than replacing the content of our control =)

Then to top it off we just add a content presenter to our user control bound to the Footer property of our user control (you have to add x:Name=”_this” to the user control itself):

<ContentPresenter Grid.Column="0" Grid.Row="3" Grid.ColumnSpan="3" Content="{Binding ElementName=_this, Path=Footer}" />

Download and licence

I have added an extension method in for testing the database connection to the sample project. This is all released under the MIT licence so feel free to use it and give me feedback if you want. :)

Just grab the code directly out of the demo project.