Skip to content

Commit 6f58a91

Browse files
Merge pull request #12548 from ghogen/access-feedback-may-2024
Add link to Access and show the generated code
2 parents 1be3488 + 93a7725 commit 6f58a91

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

docs/data-tools/add-new-connections.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ ms.subservice: data-tools
1313

1414
The steps in this article show how to connect to a data source in the Visual Studio IDE. The data source can be a local database, online data service, or a database opened from an [`.mdf` file](#open-an-mdf-database-file). You can work directly with your data in Visual Studio. You can execute queries, edit data, create and edit tables and other schema properties, edit stored procedures and functions, triggers, and so on. These functions are independent of the programming language or .NET version you are using.
1515

16+
If you're working with an Access database (`.accdb` file), see [Connect to an Access database in .NET Framework applications](./connect-to-data-in-an-access-database-windows-forms.md).
17+
1618
## Server Explorer and SQL Server Object Explorer
1719

1820
:::moniker range="<=vs-2019"

docs/data-tools/connect-to-data-in-an-access-database-windows-forms.md

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Connect to Access database in .NET Framework apps
33
description: Connect to data stored in an Access database (.mdb file or .accdb file) from .NET Framework applications with ADO.NET in Visual Studio.
4-
ms.date: 10/17/2023
4+
ms.date: 05/02/2024
55
ms.topic: how-to
66
helpviewer_keywords:
77
- data [Visual Studio], connecting
@@ -30,7 +30,11 @@ You can connect to an Access database (either an `.mdb` file or an `.accdb` file
3030
3131
## Prerequisites
3232

33-
To use these procedures, you need a Windows Forms or WPF project and either an Access database (`.accdb` file) or an Access 2000-2003 database (`.mdb` file). Follow the procedure that corresponds to your file type.
33+
To use these procedures, you need:
34+
35+
- [Visual Studio](../install/install-visual-studio.md)
36+
- A Windows Forms or WPF project
37+
- Either an Access database (`.accdb` file), or an Access 2000-2003 database (`.mdb` file). Follow the procedure that corresponds to your file type.
3438

3539
:::moniker range=">=vs-2022"
3640

@@ -94,6 +98,8 @@ Connect to databases created with Microsoft 365, Access 2016, Access 2013, Acces
9498
The dataset is added to your project, and the tables and views appear in the **Data Sources** window.
9599

96100
![Screenshot of Data Sources Window, populated with database objects](media/vs-2022/data-sources-window-populated.png)
101+
102+
14. On 64-bit machines with the 64-bit Access database engine, you need to ensure that the application runs as a 64-bit application. Open the project properties (press **Alt**+**Enter** or right-click on the project node, and select **Properties**). In the **Build** tab, clear the **Prefer 32-bit** checkbox.
97103
:::moniker-end
98104

99105
:::moniker range="vs-2019"
@@ -189,6 +195,65 @@ Connect to databases created with Access 2000-2003 by using the following proced
189195

190196
The dataset is added to your project, and the tables and views appear in the **Data Sources** window.
191197

198+
## View the generated code
199+
200+
The data tools are configured to generate a lot of code automatically when you perform certain operations in the Form Designer. For example, when you drag and drop a table onto the form, a `DataGridView` is added and code is created to hook up the data with the control. You can view this code in the `*.Designer.cs` file. Visual Studio adds a number of private members:
201+
202+
```csharp
203+
private Database11DataSet database11DataSet;
204+
private System.Windows.Forms.BindingSource ordersBindingSource;
205+
private Database11DataSetTableAdapters.OrdersTableAdapter ordersTableAdapter;
206+
private Database11DataSetTableAdapters.TableAdapterManager tableAdapterManager;
207+
private System.Windows.Forms.BindingNavigator ordersBindingNavigator;
208+
private System.Windows.Forms.ToolStripButton bindingNavigatorAddNewItem;
209+
private System.Windows.Forms.ToolStripLabel bindingNavigatorCountItem;
210+
private System.Windows.Forms.ToolStripButton bindingNavigatorDeleteItem;
211+
private System.Windows.Forms.ToolStripButton bindingNavigatorMoveFirstItem;
212+
private System.Windows.Forms.ToolStripButton bindingNavigatorMovePreviousItem;
213+
private System.Windows.Forms.ToolStripSeparator bindingNavigatorSeparator;
214+
private System.Windows.Forms.ToolStripTextBox bindingNavigatorPositionItem;
215+
private System.Windows.Forms.ToolStripSeparator bindingNavigatorSeparator1;
216+
private System.Windows.Forms.ToolStripButton bindingNavigatorMoveNextItem;
217+
private System.Windows.Forms.ToolStripButton bindingNavigatorMoveLastItem;
218+
private System.Windows.Forms.ToolStripSeparator bindingNavigatorSeparator2;
219+
private System.Windows.Forms.ToolStripButton ordersBindingNavigatorSaveItem;
220+
private System.Windows.Forms.DataGridView ordersDataGridView;
221+
private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn1;
222+
private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn2;
223+
private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn3;
224+
private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn4;
225+
private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn5;
226+
private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn6;
227+
private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn7;
228+
private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn8;
229+
private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn9;
230+
private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn10;
231+
private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn11;
232+
private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn12;
233+
```
234+
235+
If you expand the hidden region, you can see that Visual Studio also adds a large amount of code to set up the `DataGridView` control with data binding to the table you dragged to the form.
236+
237+
Also, in the main form code-behind file, Visual Studio adds code that processes the save action to save interactive changes to the data, and the code that loads the table into the table adapter.
238+
239+
```csharp
240+
private void ordersBindingNavigatorSaveItem_Click(object sender, EventArgs e)
241+
{
242+
this.Validate();
243+
this.ordersBindingSource.EndEdit();
244+
this.tableAdapterManager.UpdateAll(this.database11DataSet);
245+
246+
}
247+
248+
private void Form1_Load(object sender, EventArgs e)
249+
{
250+
// TODO: This line of code loads data into the 'database11DataSet.Orders' table. You can move, or remove it, as needed.
251+
this.ordersTableAdapter.Fill(this.database11DataSet.Orders);
252+
}
253+
```
254+
255+
Congratulations! With a little help from Visual Studio, you've created a form-based editing experience for an Access data table.
256+
192257
## Next steps
193258

194259
The dataset that you just created is available in the **Data Sources** window. You can now perform any of the following tasks:

0 commit comments

Comments
 (0)