连接到 .NET Framework 应用程序中的 Access 数据库

注意

数据集和相关类是 2000 年代初的旧 .NET Framework 技术,使应用程序能够在应用程序与数据库断开连接时处理内存中的数据。 它们对于使用户能够修改数据并持续更改回数据库的应用程序特别有用。 虽然数据集已被证明是一项非常成功的技术,但我们建议新的 .NET 应用程序使用 Entity Framework Core。 实体框架提供了一种更自然的方式来将表格数据作为对象模型,并且具有更简单的编程接口。

可以通过使用 Visual Studio 连接到 Access 数据库(.mdb 文件或 .accdb 文件)。 在定义此连接后,数据会显示在“数据源”窗口中。 可从该位置将表或视图拖动到设计图面上。

注意

如果正在使用 Visual Studio 链接到 Access 数据库,将需要注意低于 Visual Studio 2022 的 Visual Studio 版本都是 32 位进程。 这意味着,Visual Studio 2019 及更低版本中的某些数据工具将只能使用 32 位数据提供程序连接到 Access 数据库。

如果使用 Visual Studio 2022 连接到 Access 数据库,则需要注意 Visual Studio 2022 现在是 64 位进程。 这意味着,Visual Studio 中的某些数据工具将无法使用 32 位数据提供程序连接到 Access 数据库。

如果需要维护连接到 Access 数据库的 32 位应用程序,仍可以使用 Visual Studio 2022 生成并运行该应用程序。 但是,如果需要使用任何 Visual Studio 数据工具(如服务器资源管理器、数据源向导或数据集设计器),则需要使用仍为 32 位进程的 Visual Studio 的早期版本。 32 位进程的 Visual Studio 的上一版本为 Visual Studio 2019。

如果计划将项目转换为 64 位进程,则建议使用 64 位 Microsoft Access 数据库引擎,也称为访问连接引擎 (ACE)。 有关详细信息,请参阅仅适用于 Jet 和 ODBC 驱动程序的 OLE DB 提供程序为 32 位版本

先决条件

要使用这些操作过程,需要:

  • Visual Studio
  • Windows 窗体或 WPF 项目
  • Access 数据库(.accdb 文件)或 Access 2000-2003 数据库(.mdb 文件)。 按照与你的文件类型对应的过程操作。

为 .accdb 文件创建数据集

通过使用以下过程,可以连接到使用 Microsoft 365、Access 2016、Access 2013、Access 2010 或 Access 2007 创建的数据库。

  1. 在 Visual Studio 中打开 Windows 窗体或 WPF 应用程序项目。

  2. 若要打开“数据源”窗口,请按 Ctrl+Q,在搜索框中输入“数据”,然后选择“数据源”窗口 。 或在“查看”菜单中,选择“其他窗口”>“数据源”。 或在键盘上按 Shift+Alt+D 。

    搜索框中“数据源”的屏幕截图

  3. “数据源” 窗口中,单击 “添加新数据源”

    “数据源配置”向导随即打开。

    显示“数据源配置向导”的屏幕截图

  4. 在“选择数据源类型”页上,选择“数据库”,然后选择“下一步”。

  5. 在“选择数据库模型”页上,选择“数据集”,然后选择“下一步”。

    “选择数据库模型”页面的屏幕截图

  6. 在“选择数据连接”页面上选择“新建连接”,配置一个新的数据连接

    “选择数据连接”页面的屏幕截图

    随即会打开“添加连接”对话框

    “添加连接”对话框的屏幕截图

  7. 如果“数据源”未设置为“Microsoft Access 数据库文件”,则选择“更改”按钮。

    “选择数据源”对话框随即打开。 在数据源列表中,选择“Microsoft Access 数据库文件”。 已预先选择了“适用于 OLE DB 的 .NET Framework 数据提供程序”选项。 选择 “确定”

    “选择数据源”对话框的屏幕截图

  8. 选择“数据库文件名”旁边的“浏览”,然后导航到 .accdb 文件并选择“打开”

    注意

    如果 Microsoft Office 和 Visual Studio 的位数(32 位或 64 位)不匹配,你将在连接到 Access 数据库时看到错误。 在 Visual Studio 2019 中,你将收到指出数据库提供程序未注册的错误。 在 Visual Studio 2022 中,你将看到指出无法连接到 32 位数据提供程序的错误。 若要解决此错误,请确保在使用 32 位版本的 Office 时使用 Visual Studio 2019 或更低版本;对于 64 位版本的 Office,则需要 Visual Studio 2022 或更高版本。

  9. 输入用户名和密码(如有必要),然后选择“确定”。

  10. 在“选择你的数据连接”页上,选择“下一步”。

    你可能会看到一个对话框,告知你当前项目中没有数据文件。 选择“是”或“否” 。

  11. 在“将连接字符串保存到应用程序配置文件中”页上,选择“下一步” 。

    页面的屏幕截图

  12. 在“选择数据库对象”页面上展开“表”节点

    “选择数据库对象”页面的屏幕截图

  13. 选择要包含在数据集中的表或视图,然后选择“完成”。

    数据集将添加到项目中,并且“数据源”窗口中将显示表和视图

    “数据源”窗口的屏幕截图,其中填充了数据库对象

  14. 在具有 64 位 Access 数据库引擎的 64 位计算机上,需要确保应用程序作为 64 位应用程序运行。 打开项目属性(按 Alt+回车键 (Enter) 或者右键单击项目节点,然后选择“属性”)。 在“生成”选项卡中,清除“首选 32 位”复选框。

为 .accdb 文件创建数据集

通过使用以下过程,可以连接到使用 Microsoft 365、Access 2016、Access 2013、Access 2010 或 Access 2007 创建的数据库。

  1. 在 Visual Studio 中打开 Windows 窗体或 WPF 应用程序项目。

  2. 若要打开“数据源”窗口,请按 Ctrl+Q,在搜索框中输入“数据”,然后选择“数据源”窗口 。 或在“查看”菜单中,选择“其他窗口”>“数据源”。 或在键盘上按 Shift+Alt+D 。

    查看其他 Windows 数据源

  3. “数据源” 窗口中,单击 “添加新数据源”

    “数据源配置”向导随即打开。

  4. 在“选择数据源类型”页上,选择“数据库”,然后选择“下一步”。

  5. 在“选择数据库模型”页上,选择“数据集”,然后选择“下一步”。

    “选择数据库模型”页面的屏幕截图

  6. 在“选择数据连接”页面上选择“新建连接”,配置一个新的数据连接

    “选择数据连接”页面的屏幕截图

    随即会打开“添加连接”对话框

    “添加连接”对话框的屏幕截图

  7. 如果“数据源”未设置为“Microsoft Access 数据库文件”,则选择“更改”按钮。

    “选择数据源”对话框随即打开。 在数据源列表中,选择“Microsoft Access 数据库文件”。 已预先选择了“适用于 OLE DB 的 .NET Framework 数据提供程序”选项。 选择 “确定”

    “选择数据源”对话框的屏幕截图

  8. 选择“数据库文件名”旁边的“浏览”,然后导航到 .accdb 文件并选择“打开”

    注意

    如果 Microsoft Office 和 Visual Studio 的位数(32 位或 64 位)不匹配,你将在连接到 Access 数据库时看到错误。 在 Visual Studio 2019 中,你将收到指出数据库提供程序未注册的错误。 在 Visual Studio 2022 中,你将看到指出无法连接到 32 位数据提供程序的错误。 若要解决此错误,请确保在使用 32 位版本的 Office 时使用 Visual Studio 2019 或更低版本;对于 64 位版本的 Office,则需要 Visual Studio 2022 或更高版本。

  9. 输入用户名和密码(如有必要),然后选择“确定”。

  10. 在“选择你的数据连接”页上,选择“下一步”。

    你可能会看到一个对话框,告知你当前项目中没有数据文件。 选择“是”或“否” 。

  11. 在“将连接字符串保存到应用程序配置文件中”页上,选择“下一步” 。

    页面的屏幕截图

  12. 在“选择数据库对象”页面上展开“表”节点

  13. 选择要包含在数据集中的表或视图,然后选择“完成”。

    数据集将添加到项目中,并且“数据源”窗口中将显示表和视图

为 .mdb 文件创建数据集

通过使用以下过程,连接到使用 Access 2000 - 2003 创建的数据集。

  1. 在 Visual Studio 中打开 Windows 窗体或 WPF 应用程序项目。

  2. 在“查看”菜单中,选择“其他窗口”>“数据源”。

  3. “数据源” 窗口中,单击 “添加新数据源”

    “数据源配置”向导随即打开。

  4. 在“选择数据源类型”页上,选择“数据库”,然后选择“下一步”。

  5. 在“选择数据库模型”页上,选择“数据集”,然后选择“下一步”。

  6. 在“选择数据连接”页面上选择“新建连接”,配置一个新的数据连接

  7. 如果数据源不是“Microsoft Access 数据库文件(OLE DB)”,请选择“更改”以打开“更改数据源”对话框,并选择“Microsoft Access 数据库文件”,然后选择“确定”。

  8. 在“数据库文件名”中,指定要连接到的 .mdb 文件的路径和名称,然后选择“确定”。

    添加连接访问数据库文件

  9. 在“选择你的数据连接”页上,选择“下一步”。

  10. 在“将连接字符串保存到应用程序配置文件中”页上,选择“下一步” 。

  11. 在“选择数据库对象”页面上展开“表”节点

  12. 选择要包含在数据集中的任何表或视图,然后选择“完成”。

    数据集将添加到项目中,并且“数据源”窗口中将显示表和视图

查看生成的代码

根据数据工具的配置,在窗体设计器中执行某些操作时会自动生成大量代码。 例如,将表拖放到窗体上时,会添加一个 DataGridView,并创建代码来将数据与控件挂钩。 可以在 *.Designer.cs 文件中查看此代码。 Visual Studio 增加了多个私有成员:

private Database11DataSet database11DataSet;
private System.Windows.Forms.BindingSource ordersBindingSource;
private Database11DataSetTableAdapters.OrdersTableAdapter ordersTableAdapter;
private Database11DataSetTableAdapters.TableAdapterManager tableAdapterManager;
private System.Windows.Forms.BindingNavigator ordersBindingNavigator;
private System.Windows.Forms.ToolStripButton bindingNavigatorAddNewItem;
private System.Windows.Forms.ToolStripLabel bindingNavigatorCountItem;
private System.Windows.Forms.ToolStripButton bindingNavigatorDeleteItem;
private System.Windows.Forms.ToolStripButton bindingNavigatorMoveFirstItem;
private System.Windows.Forms.ToolStripButton bindingNavigatorMovePreviousItem;
private System.Windows.Forms.ToolStripSeparator bindingNavigatorSeparator;
private System.Windows.Forms.ToolStripTextBox bindingNavigatorPositionItem;
private System.Windows.Forms.ToolStripSeparator bindingNavigatorSeparator1;
private System.Windows.Forms.ToolStripButton bindingNavigatorMoveNextItem;
private System.Windows.Forms.ToolStripButton bindingNavigatorMoveLastItem;
private System.Windows.Forms.ToolStripSeparator bindingNavigatorSeparator2;
private System.Windows.Forms.ToolStripButton ordersBindingNavigatorSaveItem;
private System.Windows.Forms.DataGridView ordersDataGridView;
private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn1;
private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn2;
private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn3;
private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn4;
private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn5;
private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn6;
private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn7;
private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn8;
private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn9;
private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn10;
private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn11;
private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn12;

如果展开隐藏区域,则可以看到 Visual Studio 还增加了大量代码来设置 DataGridView 控件,并将数据绑定到拖动到窗体的表。

此外,在主窗体代码隐藏文件中,Visual Studio 增加了处理保存操作的代码以保存对数据的交互式更改,以及将表加载到表适配器中的代码。

private void ordersBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
    this.Validate();
    this.ordersBindingSource.EndEdit();
    this.tableAdapterManager.UpdateAll(this.database11DataSet);

}

private void Form1_Load(object sender, EventArgs e)
{
   // TODO: This line of code loads data into the 'database11DataSet.Orders' table. You can move, or remove it, as needed.
   this.ordersTableAdapter.Fill(this.database11DataSet.Orders);
}

祝贺你! 借助 Visual Studio 提供的一些帮助,你已为 Access 数据表创建了基于窗体的编辑体验。

后续步骤

刚刚创建的数据集出现在“数据源”窗口中。 现在可以执行以下任何任务: