Find / select database with C #

0

I have a form with a button and a simple datagridView, but my problem is that I have to look for my file in ".mdb" (Access database) on the computer, so it can be shown in the datagridView then the question remains:

When I click the button, I want an openFileDialog to appear, where the user looks for the database, so when he selects the file already populates the datagridView with the information of the database he has selected.

Is there any way to do this?

I tried to use a template that the derpirscher (US Stack overflow user) advised me, it would look something like this:

private void button1_Click(object sender, EventArgs e)
{
  OpenFileDialog dlg = new OpenFileDialog();
  dlg.Filter = "Database Files|*.mdb";
  if (dlg.ShowDialog() == DialogResult.OK) {
  string dbfile = dlg.FileName;
  string connectstring = string.format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}";Persist Security Info=False;, dbfile);

 using (OleDbConnection con = new OleDbConnection(connectstring)) {
     //..realiza as query's
    }

  }

}
    
asked by anonymous 28.07.2016 / 15:39

1 answer

0

Matt, your connectionstring is already dynamic, it picks up the path of the database through the dbfile variable (which receives dlg.FileName). From there, whenever you need the connectionstring use your variable.

    
29.07.2016 / 03:48