简体   繁体   English

手动将 MS Access 数据库连接到 Visual Studio

[英]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.我正在为 Visual Studio (C#) 中的软件 Autocad 开发一个插件,并想手动导入 MS 数据库。 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.我在网上查看了一下,发现您可以通过工具栏连接到数据库,但我希望用户通过单击 WinForms 中的按钮来导入 .accdb 文件。 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.我正在考虑一个库或任何有用的工具(如 System.Xml),以便我可以通过代码和潜在的 SQL 查询访问数据库。

To make things clearer, here is an example of how the plugin would work:为了让事情更清楚,这里有一个插件如何工作的例子:

  1. Open Autocad and the Plugin打开 Autocad 和插件
  2. WinForms window pops up with a button: Import Database WinForms 窗口弹出一个按钮:导入数据库
  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!每一个建议和提示表示赞赏! :) :)

Use System.Data and System.Data.OleDb.使用 System.Data 和 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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM