locked
CHANGING THE DATA SOURCE AT RUN TIME RRS feed

  • Question

  • Hello:

     

    I'm a begginner in databinding (I come from collections), I'm trying to develop an C# application using a Microsoft Access 2003 database. At design time, I set the data source (*.mdb) located in one of my project folders. I call it the database template because each new "document "that user creates is based on it, like the Normal.dot in Word.

     

    But I think what if user wants open a "document" located in another folder like my documents? I'm thinking in allowing  the user to browse the computer looking for the *.mdb file. After, using IO.Path, application creates the connection, using OLEDB, load the data stored in the selected file, but my cuestion is it is safe?. I've designed the data set based on the database template, but if data source changes, data set does too?

     

    And if user saves the document in that location instead of save in the database template folder?

     

    In other words, how can I change the connection string at runtime?

     

    I'll appreciate your comments

    Tuesday, June 19, 2007 7:10 PM

Answers

  •  

    Hi Eder,

     

    Just look at the connection string of Access.

    Code Snippet

    @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\v-rzhang\My Documents\test.mdb"

     

    The text in bold type is path of the database. You can use a OpenFileDialog to get the path of this database file. Try to do something like this.

    Code Snippet

    OleDbConnection cn;

    private void button1_Click(object sender, EventArgs e)

            {

                string path = "";

                if (this.openFileDialog1.ShowDialog() == DialogResult.OK)

                    path = this.openFileDialog1.FileName;

              cn  = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+path);

            }

     

    Hope this helps.

    Regards

     

    Thursday, June 21, 2007 11:43 AM