How do I set the connection string at installation :general data provider?
The problem arises when you are using the general data providers such as classes from System.Data.OleDb and System.Data.Odbc namespaces. You can’t make a general GUI to generate the connection string for all the database servers. Then what to do now? The solution is the DataLink Dialog. You must have seen the dialog which looks like this in many windows applications:
http://www.programmersheaven.com/articles/images/faq/image014.gif
So how you can use this dialog in your program? For this you need add a reference to COM component ‘Microsoft OLE DB Service Component 1.0 Type Library’ which should be available if you have installed Microsoft ActiveX Data Components. Once you have added the component, you can show the Data Link Properties dialog box by making an object of type MSDASC.DataLink class and calling its PromptNew() method.
C# Version
MSDASC.DataLinks udl = new MSDASC.DataLinksClass();
udl.PromptNew();
VB.Net Version
Dim udl As MSDASC.DataLinks
udl = New MSDASC.DataLinksClass
udl.PromptNew()
The above code will show the Data Link Properties dialog box. But how can we get the connection string generated by the dialog box? The PromptNew() method returns a connection type object which can be captured in an object of type ADODB.Connection. Hence for this, add a reference to ‘adodb’ .Net assembly in your project, and get the connection string using the ConnectionString property of this object. The following code snippets demonstrate this:
C# Version
string connStr = "";
MSDASC.DataLinks udl = new MSDASC.DataLinksClass();
ADODB.Connection conn = (ADODB.Connection) udl.PromptNew();
if(conn != null)
{
connStr = conn.ConnectionString;
}
VB.Net Version
Dim connStr As String
Dim udl As MSDASC.DataLinks
udl = New MSDASC.DataLinksClass
Dim conn As ADODB.Connection = udl.PromptNew()
If Not conn Is Nothing Then
connStr = conn.ConnectionString
End If
You can use this code in the overrided Install() method of your project’s installer class (the class derived from System.Configuration.Install.Installer class), and add the project output in the ‘Custom Actions’ of the setup project.
===== Author’s Note: ===== See the next FAQs for how to build an example application for this
Back