nagates
nagates

Reputation: 630

Strongly typed data set change source dynamically

I'm maintain an app that uses an access database, I would like to use something smarter than formatting sql statements on the fly, so out of my limited options I decided to use a strongly Data Set, but I need the option to change the data source its using on the fly, as the user can change which database its pointing at, the db are same as far a schema goes, the only difference is a data, is there a good way of doing this? I basically need to ignore whats in the config settings, and use the path the user picks.

Upvotes: 0

Views: 396

Answers (1)

HelloW
HelloW

Reputation: 1617

You can change the connection string by changing the data source connection string that points to the datafile in this example

public void ConnectToAccess()
{
    System.Data.OleDb.OleDbConnection conn = new 
        System.Data.OleDb.OleDbConnection();
    // TODO: Modify the connection string and include any
    // additional required properties for your database.
    conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
        @"Data source= C:\Documents and Settings\username\" +
        @"My Documents\AccessFile.mdb";
    try
    {
        conn.Open();
        // Insert code to process data.
    }
        catch (Exception ex)
    {
        MessageBox.Show("Failed to connect to data source");
    }
    finally
    {
        conn.Close();
    }
}

Upvotes: 1

Related Questions