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