What is the best way to allow a user to specify a SQL server and database from a Windows application?

I am working on building a Windows application that will require a connection to at least two different Microsoft SQL Server databases (for example, a source and a target), which may or may not be on the same server.

So, in the application settings, I would like the user to be able to specify both the server(s) involved, as well as the source and target databases. I did a little bit of research and immediately found this article on Code Project:

http://www.codeproject.com/Articles/31826/SQL-Server-Authentication-using-SMO

It uses something I wasn’t previously aware of called, SQL Server Management Objects:

http://msdn.microsoft.com/en-us/library/ms162169(v=sql.105).aspx

Anyway, I am wondering if SMO is the best way to handle establishing connections to data sources that are not known to me, but will be known by the user of the application. Part of the reason I am asking is that most information I am finding seems to be from roughly 5 years ago and want to make sure there isn’t a better way to do this today.

Technical Details: I am looking at building it with Windows Forms or as a WPF application and will be dealing with Microsoft SQL Server 2008 R2 and up (likely 2012 and potentially 2014).

Thank you in advance for any advice, thoughts, or suggestions.

3

If you want to allow users to discover servers/databases on the network SMO is the way to go. If you just want to allow them to enter known (to them) connection information, just collect the information in a form and use a SqlConnectionStringBuilder. Here is a nice article showing how to use it

using System.Data.SqlClient;

private void CreateConnectionString()
{
  SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();

  builder.DataSource = "(local)";
  builder.InitialCatalog = "Northwind";
  builder.UserID = "user1";
  builder.Password = "P@ssw0rd";

  MessageBox.Show(builder.ConnectionString);
}

This is a pretty easy class to use. You can just fill in the basic information such as the DataSource, InitialCatalog, the UserId and
Password and it will create a connection string for you. The output
from the above code will be: “Data Source=(local);Initial
Catalog=Northwind;User ID=user1;Password=p@ssword”.

To add on additional keywords for your connection string you may use
the Add method. This method takes the keyword and the value and will
add them in the appropriate format to your connection string.

As mentioned, each ADO.NET data provider supplies one of these
classes. For example, if you are using Oracle, you would use the
System.Data.OracleClient namespace, then use the
OracleConnectionStringBuilder.

The ConnectionStringBuilder class allows you to parse the individual
elements of a connection string and put them into the corresponding
properties in the ConnectionStringBuilder. In the following example
you take an existing connection string like the one shown in the code
below and place it into the ConnectionString property of the
ConnectionStringBuilder. The ConnectionStringBuilder will then break
it into the appropriate properties.

5

To build on @Mike’s solution, This is how I solved a similar problem

Public Con_Str As String

    Function Get_Con_Str()

        If .......Write yo code here...... Then
            Con_Str= "Initial Catalog=databasename1;" & "Data Source=datasource1;Integrated Security=True;"

        Else
           Con_Str= "Initial Catalog=databasename2;" & "Data Source=datasource2;Integrated Security=True;"
        End If
        Return Con_Str
    End Function

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật