Database Connection String RRS feed

  • Question

  • Good Evening All.

         I am brand new to coding and have been trying to track down an answer to the following situation:

    I am building a WPF program that uses an Access Database as a partial backbone. The program requires the user to pick a database file to use with their instance of the program. Once the user chooses the appropriate file the complete path shows up in a text box on the form. What I am trying to do is write the connection string to the database using the file path that is populated in the text box as the data source after the user chooses their database file. I have been searching for proper syntax on how to do this for a few days with no success. I've put the pertinent code snippet below.

     // Database Connection Variables
            OleDbConnection dbTest;
            OleDbCommand cmd1;
            string strcon = @"Provider=Microsoft.ace.oleDB.12.0;data source=J:\Average Joes' Theatrical Automation\Fluid Canvas UI\WPF Fluid Canvas\Test Database.accdb;";
            // Database Load Button
            private void Button_Click(object sender, RoutedEventArgs e)
                Microsoft.Win32.OpenFileDialog dbLoad = new Microsoft.Win32.OpenFileDialog();
                dbLoad.DefaultExt = ".accdb";
                dbLoad.Filter = "Access Database (.accdb) |*.accdb";

                Nullable<bool> result = dbLoad.ShowDialog();
                txtDataBase.Text = dbLoad.FileName;

    To better clarify, I want to take the file path that's populated in the txtDataBase text box and use it as the data source for the connection string. Thank you for any and all help.

    Tuesday, January 8, 2019 1:07 AM

All replies

  • Hi Joe Fasciano,

    Thank you for posting here,

    For your question, here is the code to read data from Access database and show in datagridView.

      private void Button1_Click(object sender, EventArgs e)
                dataGridView1.AllowUserToAddRows = false;
                string strDSN = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source =" + textBox1.Text;
                string strSQL = "SELECT * FROM Table1";
                // create Objects of ADOConnection and ADOCommand  
                OleDbConnection myConn = new OleDbConnection(strDSN);
                OleDbDataAdapter myCmd = new OleDbDataAdapter(strSQL, myConn);
                DataSet dtSet = new DataSet();
                myCmd.Fill(dtSet, "Table1");
                DataTable dTable = dtSet.Tables[0];
                dataGridView1.DataSource = dtSet.Tables["Table1"].DefaultView;

    If you could get the access database path and input in TextBox1, you could show the table in datagridView.

    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Tuesday, January 8, 2019 7:21 AM