Prompting for database connection

| | No TrackBacks

Add a COM reference to:
Microsoft OLE DB Service Component 1.0 Type Library
Microsoft ActiveX Data Objects 2.7 // any 2.5 or higher version is fine

You can then write code like:

//
// Prompt for a totally new connection string
//
private void button1_Click(object sender, EventArgs e)
{
    bool Cancelled = false;
    string ConnectionString = string.Empty;

    MSDASC.DataLinks d = new MSDASC.DataLinks();
    d.hWnd = (int)this.Handle;
    ADODB._Connection c = (ADODB._Connection)d.PromptNew();
    if (c == null)
        Cancelled = true;
    else
    {
        Cancelled = false;
        ConnectionString = c.ConnectionString;
    }
}

//
// Edit a connection string
//
private void button2_Click(object sender, EventArgs e)
{
    bool Saved = false;
    string ConnectionString = "Provider=SQLOLEDB.1;Integrated
Security=SSPI;Persist Security Info=False;User ID=sa;Initial
Catalog=Northwind;Data Source=.";

    MSDASC.DataLinks d = new MSDASC.DataLinks();
    d.hWnd = (int)this.Handle;
    object c = new ADODB.Connection();
    ((ADODB._Connection)c).ConnectionString = ConnectionString;
    Saved = d.PromptEdit(ref c);
    if (Saved)
    {
        ConnectionString = ((ADODB._Connection)c).
          ConnectionString;
    }
}

No TrackBacks

TrackBack URL: http://www.iwebthereforeiam.com/cgi-bin/mt/mt-tb.cgi/225

Leave a comment

Verification (needed to reduce spam):

Pages

OpenID accepted here Learn more about OpenID
Powered by Movable Type 4.32-en

About this Entry

This page contains a single entry by Hugh Brown published on April 22, 2007 11:33 AM.

Using the Visual Studio Connection String Dialog was the previous entry in this blog.

Import Excel Spreadsheet Data into SQL Server Database Table Using SqlBulkCopy is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.