Search code examples
c#databasems-accessvisual-studio-codeautocad

Connect MS Access Database to Visual Studio manually


I am working on a plugin for the software Autocad in Visual Studio (C#) and would like to import a MS Database manually. I looked a bit online and saw that you can connect to a database via the toolbar but I'd like for the user to import a .accdb file by clicking on a button from WinForms. Is it possible for me to do something like that? I'm thinking of a library or any helpful tool (like System.Xml) so that I can access the database via code and potentially SQL queries.

To make things clearer, here is an example of how the plugin would work:

  1. Open Autocad and the Plugin
  2. WinForms window pops up with a button: Import Database
  3. After successful import, data will be loaded in a dropdown and you can select one of the values

Every suggestion and tip is appreciated! :)


Solution

  • Use System.Data and System.Data.OleDb.
    You'll have to look up your connection string for your database. This isn't a robust solution, but it gets the job done quick.

        public DataSet GetDataSet(string sql)
        {
            DataSet dataSet = new DataSet();
            using (OleDbConnection conn = new OleDbConnection(connString))
            {
                try
                {
                    conn.Open();
                    OleDbDataAdapter adapter = new OleDbDataAdapter(sql, conn);
                    adapter.Fill(dataSet);
                }
                catch (Exception ex) { throw new Exception(ex.Message);  }
                finally { conn.Close(); }
            }
            return dataSet;
        }