0

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
    It's just connection strings. What is your specific question? – Robert Harvey Oct 30 '14 at 18:20
  • Of course, it's all connection strings in the end, but my concern was giving the end-user a (hopefully) better means of browsing available databases, from which I would then build them. Sorry about the phrasing of my question--I'm sure I could have been a little more clear. Thank you very much for your reply, but it looks like Mike caught my drift and has answered my question. Much appreciated! – Bill Kron Oct 30 '14 at 20:00
  • Be aware a server is not necessarily discoverable. – paparazzo Jun 26 '15 at 16:21
2

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.

  • Mike, you have basically answered my main question about SMO and whether there is a better way of doing SQL server/database discovery. I will likely combine SMO (to let the user browse to the desired databases) with the SqlConnectionStringBuilder class, as you have demonstrated above. One other question: in your comments after the ConnectionStringBuilder code snippet, you reference other code, "...like the one shown in the code below..." Did you mean to include another code reference? – Bill Kron Oct 30 '14 at 19:57
  • By the way, THANK YOU very much for your helpful and thoughtful answer! – Bill Kron Oct 30 '14 at 19:58
  • I just posted/quoted text from the article that I linked to in case it ever goes away. If you follow the link, you'll see the full article. – Michael Brown Oct 30 '14 at 20:01
  • 1
    Wonderful! I just wanted to be sure I was on the same page. I know the rules say to avoid comments like, "thanks", but I would feel rude, if you didn't know I appreciate the assistance. So, thank you once again! :-) – Bill Kron Oct 30 '14 at 20:04
  • No problem at all! Glad I was able to help. – Michael Brown Oct 30 '14 at 20:14
-1

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
  • Programmers is about conceptual questions and answers are expected to explain things. Throwing code dumps instead of explanation is like copying code from IDE to whiteboard: it may look familiar and even sometimes be understandable, but it feels weird... just weird. Whiteboard doesn't have compiler – gnat Jan 7 '15 at 12:02
  • I just shared my experience on a similar challenge as @gnat threw comments, not explaining anything at all. As a programmer, understanding logic is very important, comments do not add much. Thanks anyway – sav Jan 7 '15 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.