adopilot
adopilot

Reputation: 4520

SqlConnectionStringBuilder form in .net C#

I am wondering how I can add DATA LINK form to my WIN application. You know those forms where Users can choose on witch SQL server they going to connect and what type of security they going to use and on wht database.

Something like on this picture alt text http://img186.imageshack.us/img186/7259/datalink.png

Upvotes: 0

Views: 2677

Answers (3)

Adriano Machado
Adriano Machado

Reputation: 232

I think that the best choice is to use the code provided from Microsoft here: http://code.msdn.microsoft.com/Connection.

It is the connection dialog used inside Visual Studio.

However, it only works with registered ADO.NET providers.

Upvotes: 1

JP Alioto
JP Alioto

Reputation: 45127

More on that here.

Upvotes: 1

Paul Alexander
Paul Alexander

Reputation: 32377

You can do it through some COM Introp...but then you have to pull in a bunch of Interop assemblies into your project which can be a drag. This code will show the dialog using reflection instead.

public static string ShowDialog( IWin32Window owner, 
                                 string connectionString )
{
    Type dlType = Type.GetTypeFromProgID( "DataLinks", true );
    Type acType = Type.GetTypeFromProgID( "ADODB.Connection", true );

    object form = Activator.CreateInstance( dlType );
    object connection = Activator.CreateInstance( acType ); 

    acType.InvokeMember( 
        "ConnectionString", 
        BindingFlags.Public | BindingFlags.SetProperty, 
        null, 
        connection, 
        new object[]{ connectionString } 
        );  
    object result = 
    dlType.InvokeMember( 
        "PromptEdit", 
        BindingFlags.Public | BindingFlags.InvokeMethod, 
        null, 
        form, 
        new object[]{ connection } 
        );          
    if( result != null && (bool)result )
        return acType.InvokeMember( 
                    "ConnectionString", 
                    BindingFlags.Public | BindingFlags.GetProperty, 
                    null, 
                    connection, 
                    new object[]{} ) as string;

    return null;
}

This basically translates to the following VB Script

form = GetObject( "DataLinks" )
connection = GetOBject( "ADODB.Connection" )
connection.ConnectionString = "existing connection"
form.PromptEdit( connection )
Return connection.ConnectionString

Upvotes: 2

Related Questions