HowTo... Configure a connection string like a pro.

Ever wondered how to configure a connection string using the Data Link properties dialog?

image

Well, I have. And just last night I decided to check out exactly how to do this. Here's a simple list with all the required steps:

  1. Add a reference to "Microsoft OLE DB Service Component 1.0 Type Library" (it’s in the COM Tab)
  2. Add a reference to the Primary Interop Assembly (PIA) for ADO (Add References > adodb)
  3. Add the following code (for instance to a button click event handler):
    1: ADODB.Connection conn = new ADODB.Connection(); 
    2: object oConn = (object)conn;
    3:  
    4: MSDASC.DataLinks dlg = new MSDASC.DataLinks(); 
    5: dlg.PromptEdit(ref oConn);
    6:  
    7: txtConnectionString.Text = conn.ConnectionString;

We can also pre-assign the ConnectionString property of the Connection object before we open up the Data Link dialog.

If we do this, the dialog is already filled with the configuration from the connection string.