|
1 | 1 | ---
|
2 | 2 | title: Connect to Access database in .NET Framework apps
|
3 | 3 | 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 |
5 | 5 | ms.topic: how-to
|
6 | 6 | helpviewer_keywords:
|
7 | 7 | - data [Visual Studio], connecting
|
@@ -30,7 +30,11 @@ You can connect to an Access database (either an `.mdb` file or an `.accdb` file
|
30 | 30 |
|
31 | 31 | ## Prerequisites
|
32 | 32 |
|
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. |
34 | 38 |
|
35 | 39 | :::moniker range=">=vs-2022"
|
36 | 40 |
|
@@ -94,6 +98,8 @@ Connect to databases created with Microsoft 365, Access 2016, Access 2013, Acces
|
94 | 98 | The dataset is added to your project, and the tables and views appear in the **Data Sources** window.
|
95 | 99 |
|
96 | 100 | 
|
| 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. |
97 | 103 | :::moniker-end
|
98 | 104 |
|
99 | 105 | :::moniker range="vs-2019"
|
@@ -189,6 +195,65 @@ Connect to databases created with Access 2000-2003 by using the following proced
|
189 | 195 |
|
190 | 196 | The dataset is added to your project, and the tables and views appear in the **Data Sources** window.
|
191 | 197 |
|
| 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 | + |
192 | 257 | ## Next steps
|
193 | 258 |
|
194 | 259 | 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