Shaykatc's WebLog

VS 2003 Tip #13: Getting a connection string quickly

Often I will be writing some quick code to access a database, and I'll need to add a connection string to get this. Heres a quick way to get the connection string using VS:

Open Server Explorer. Right click on Data connections and select “Add new connection”. Pass the information that you want to connect with to the dialog. Hit ok. Once this connection gets added to your Data Connection, right click on it and select Properties. The properties window opens up and one of the fields in it (conveniently) is Connect String. Copy and Paste it into your code. You can trim it down further if you happen to know more details about it. Voila!

 

 

 

Published Tuesday, April 13, 2004 2:47 PM by shaykatc

Comments

 

AT said:

Bad... Bad ... Bad ...

Take a look on OLE DB IDBPromptInitialize interface.
C++ users (as well some others ;o) can use it to show nice connection dialogs in their applications.

Also I've made suggestion to allow .NET (C# for example) developers to be able do the same. Microsoft most likely will add this in Beta 1 of Whidbey.



April 13, 2004 8:47 PM
 

shaykatc said:

Sorry - but for my benefit, what exactly is bad?
April 14, 2004 7:04 AM
 

David said:

I do it like this: Go to desktop, create new text file, change extension to .udl, double click, do what you have to, change back to .txt or throw in *pad.
April 14, 2004 8:56 AM
 

jvanva said:

Using IDBPromptInitialize gets you an OLEDB connect string doesn't it? So it has to be edited for use with the SqlServer provider.

THe UDL approach threw me into an ODBC dialog to construct a connect string. Ugh.

In the absence of a sound reason for not doing so, think I prefer the approach proposed by the OP. At least the string can be used with the SqlServer provider without editing if desired. Of course the hardest part is selecting it to paste into another file.

There is also a project on GotDotNet.com that will manufacture SqlServer or Oracle connect strings from dialog box input and spit out config files ready to use (or at least paste into another config file). It will even encrypt strings encrypted if you wish.
May 13, 2004 5:35 AM
 

JDL said:

If you want a run-time connection string, I like adding Microsoft ActiveX Data Objects 2.7 Library and Microsoft OLD DB Service Component 1.0 Type Library (both are COM components) then using the following (C#):

MSDASC.DataLinks oDL = New MSDASC.DataLinksClass();
ADODB.Connection conn = New ADODB.ConnectionClass();
objet oConn = (object) conn;
lDL.PromptEdit(ref oConn);

This gives the Windows dialog to point and click whatever connection parameters the user wants, and then conn.ConnectionString holds a string with all those parameters.
May 29, 2004 7:41 AM
 

Shaykatc s WebLog VS 2003 Tip 13 Getting a connection string quickly | fix my credit said:

June 16, 2009 10:09 PM
New Comments to this post are disabled

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker