An easy way to do this would be to add a reference to "Microsoft OLE DB
Service Component 1.0 Type Library". If you want to work with an existing
connection string, then you will have to add a reference to ADODB as well.
The following code will then invoke the dialog:
// The data links component.
MSDASC.DataLinks pobjDataLinks = null;
// A connection class instance.
ADODB._Connection pobjConnection = null;
// A temp object.
object pobjTemp = null;
// Create the data links component. Wrap in
// a try/finally block to release when done.
try
{
// Create the data links instance.
pobjDataLinks = new MSDASC.DataLinksClass();
// Set the window handle.
pobjDataLinks.hWnd = (int) this.Handle;
// Prompt for either a new value or an edit
// of an existing value. In this case,
// mobjValueTextBox has the connection string in it.
if (mobjValueTextBox.Text.Trim() != string.Empty)
{
// Create a connection.
pobjConnection = new ADODB.ConnectionClass();
// Set the connection string.
pobjConnection.ConnectionString = mobjValueTextBox.Text;
// Set the dummy reference.
pobjTemp = pobjConnection;
// Prompt for the new connection.
if (pobjDataLinks.PromptEdit(ref pobjTemp))
// Set the connection string.
mobjValueTextBox.Text = pobjConnection.ConnectionString;
}
else
{
// Get the new connection.
pobjConnection = (ADODB._Connection) pobjDataLinks.PromptNew();
// If the connection is not null, then set the text here.
if (pobjConnection != null)
// Set the text.
mobjValueTextBox.Text = pobjConnection.ConnectionString;
}
}
finally
{
// Release the data links class if it is not null.
if (pobjDataLinks != null)
// Release.
Marshal.ReleaseComObject(pobjDataLinks);
// Release the connection if it exists.
if (pobjConnection != null)
// Release.
Marshal.ReleaseComObject(pobjConnection);
}
Hope this helps.