You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: Note saying that ADO data technologies are not recommended for new development.
4
+
description: Note saying that ADO data technologies aren't recommended for new development.
5
5
ms.author: ghogen
6
-
ms.date: 04/26/2023
6
+
ms.date: 08/26/2024
7
7
ms.subservice: data-tools
8
8
ms.topic: include
9
9
---
10
10
> [!NOTE]
11
-
> Datasets and related classes are legacy .NET Framework technologies from the early 2000s that enable applications to work with data in memory while the applications are disconnected from the database. They are especially useful for applications that enable users to modify data and persist the changes back to the database. Although datasets have proven to be a very successful technology, we recommend that new .NET applications use [Entity Framework Core](/ef/). Entity Framework provides a more natural way to work with tabular data as object models, and it has a simpler programming interface.
11
+
> Datasets and related classes are legacy .NET Framework technologies from the early 2000s that enable applications to work with data in memory while the applications are disconnected from the database. The technologies are especially useful for applications that enable users to modify data and persist the changes back to the database. Although datasets have proven to be a very successful technology, we recommend that new .NET applications use [Entity Framework Core](/ef/). Entity Framework provides a more natural way to work with tabular data as object models, and it has a simpler programming interface.
title: Insert records into a database (.NET Framework)
3
-
description: Insert new records into a database with .NET Framework application development in Visual Studio, including the ADO.NET TableAdapter.Update method.
4
-
ms.date: 07/10/2024
2
+
title: Insert records into database (.NET Framework)
3
+
description: Insert new records into a database with .NET Framework application development in Visual Studio, including the ADO.NET TableAdapterUpdate method.
4
+
ms.date: 08/26/2024
5
5
ms.topic: how-to
6
6
dev_langs:
7
7
- VB
@@ -16,56 +16,74 @@ author: ghogen
16
16
ms.author: ghogen
17
17
manager: mijacobs
18
18
ms.subservice: data-tools
19
+
20
+
#customer intent: As a developer, I want to develop .NET Framework applications in Visual Studio, so I can insert new records into a database with TableAdapters or command objects.
19
21
---
20
22
21
23
# Insert new records into a database in .NET Framework applications
22
24
25
+
To insert new records into a database with [ADO.NET](/dotnet/framework/data/adonet/) in a .NET Framework project, the common approach is to use [TableAdapter](../data-tools/create-and-configure-tableadapters.md) methods. TableAdapters enable communication between your application and database. They provide different ways to insert new records into a database, depending on the requirements of your application. You can use the `TableAdapter.Update` method or one of the TableAdapter [DBDirect](save-data-with-the-tableadapter-dbdirect-methods.md) methods (specifically, the `TableAdapter.Insert` method).
26
+
27
+
This article describes how to insert records into a database for an application built with ADO.NET and the .NET Framework by using Visual Basic (VB) or C#. If your application configuration uses Entity Framework 6, see [Adding a new entity to the context](/ef/ef6/saving/change-tracking/entity-state#adding-a-new-entity-to-the-context), or for Entity Framework Core, see [Adding data](/ef/core/saving/basic#adding-data).
To insert new records into a database with [ADO.NET](/dotnet/framework/data/adonet/) in a .NET Framework project, you can use the `TableAdapter.Update` method, or one of the TableAdapter's DBDirect methods (specifically the `TableAdapter.Insert` method). For more information, see [TableAdapter](../data-tools/create-and-configure-tableadapters.md).
31
+
## Prerequisites
26
32
27
-
If your application doesn't use TableAdapters, you can use command objects (for example, <xref:System.Data.SqlClient.SqlCommand>) to insert new records in your database.
33
+
- To work with TableAdapter methods, you need to have an available instance. For more information, see [Create and configure TableAdapters in .NET Framework applications](create-and-configure-tableadapters.md).
28
34
29
-
If your application uses datasets to store data, use the `TableAdapter.Update` method. The `Update` method sends all changes (updates, inserts, and deletes) to the database.
35
+
-**.NET security**: You must have access to the database you're trying to connect to, and permission to perform inserts into the desired table.
30
36
31
-
If your application uses objects to store data, or if you want finer control over creating new records in the database, use the `TableAdapter.Insert`method.
37
+
## Choose insertion method
32
38
33
-
If your TableAdapter doesn't have an `Insert` method, it means that either the TableAdapter is configured to use stored procedures, or its `GenerateDBDirectMethods` property is set to `false`. Try setting the TableAdapter's `GenerateDBDirectMethods` property to `true` from within the **Dataset Designer**, and then save the dataset. This action regenerates the TableAdapter. If the TableAdapter still doesn't have an `Insert` method, the table probably doesn't provide enough schema information to distinguish between individual rows (for example, there might be no primary key set on the table).
39
+
There are different approaches for inserting records into a database based on your application scenario. The following table summarizes the options:
34
40
35
-
> [!NOTE]
36
-
> This article applies to ADO.NET and .NET Framework development. For the same task with Entity Framework 6, see [Adding a new entity to the context](/ef/ef6/saving/change-tracking/entity-state#adding-a-new-entity-to-the-context). For Entity Framework Core, see [Adding data](/ef/core/saving/basic#adding-data).
41
+
| Scenario | Approach | Notes |
42
+
| --- | --- | --- |
43
+
| App uses *datasets* to store data | Use the [TableAdapter.Update](#insert-new-records-with-tableadapterupdate-method) method to send all changes to the database | Changes include updates, insertions, and deletions. |
44
+
| App uses *objects* to store data | Use the [TableAdapter.Insert](#insert-new-records-with-tableadapterinsert-method) method to insert new records into the database | This approach enables you to have finer control over creating new records. |
45
+
| App uses TableAdapters, `Insert` method not available | Set the TableAdapter `GenerateDBDirectMethods` property to `true` from within the **Dataset Designer** and save the dataset to regenerate the TableAdapter | If your TableAdapter doesn't have an `Insert` method, the TableAdapter is either configured to use stored procedures or the `GenerateDBDirectMethods` property is set to `false`. <br> If the `Insert` method remains unavailable after regenerating the TableAdapter, the table probably doesn't provide enough schema information to distinguish between individual rows (for example, there might be no primary key set on the table). |
46
+
| App doesn't use TableAdapters | Use [command objects](#insert-new-records-with-command-objects) to insert new records into the database | Example: <xref:System.Data.SqlClient.SqlCommand>|
37
47
38
48
## Insert new records by using TableAdapters
39
49
40
-
TableAdapters provide different ways to insert new records into a database, depending on the requirements of your application.
50
+
If your application uses datasets to store data, you can add new records to the desired <xref:System.Data.DataTable> in the dataset, and then call the `TableAdapter.Update` method. The `TableAdapter.Update` method sends any changes in the <xref:System.Data.DataTable> to the database, including modified and deleted records.
41
51
42
-
If your application uses datasets to store data, you can add new records to the desired <xref:System.Data.DataTable> in the dataset, and then call the `TableAdapter.Update` method. The `TableAdapter.Update` method sends any changes in the <xref:System.Data.DataTable> to the database (including modified and deleted records).
52
+
### Insert new records with TableAdapter.Update method
43
53
44
-
### To insert new records into a database by using the TableAdapter.Update method
54
+
The following procedure demonstrates how to insert new records into a database by using the `TableAdapter.Update` method:
45
55
46
56
1. Add new records to the desired <xref:System.Data.DataTable> by creating a new <xref:System.Data.DataRow> and adding it to the <xref:System.Data.DataTable.Rows%2A> collection.
47
57
48
-
2. After the new rows are added to the <xref:System.Data.DataTable>, call the `TableAdapter.Update` method. You can control the amount of data to update by passing in either an entire <xref:System.Data.DataSet>, a <xref:System.Data.DataTable>, an array of <xref:System.Data.DataRow>s, or a single <xref:System.Data.DataRow>.
58
+
1. After you add the new rows to the <xref:System.Data.DataTable>, call the `TableAdapter.Update` method. You can control the amount of data to update by passing one of the following parameter values:
49
59
50
-
The following code shows how to add a new record to a <xref:System.Data.DataTable> and then call the `TableAdapter.Update` method to save the new row to the database. (This example uses the `Region` table in the Northwind database.)
60
+
- An entire <xref:System.Data.DataSet>
61
+
- A <xref:System.Data.DataTable>
62
+
- An array of <xref:System.Data.DataRow>s
63
+
- A single <xref:System.Data.DataRow>
64
+
65
+
The following code shows how to add a new record to a <xref:System.Data.DataTable> and then call the `TableAdapter.Update` method to save the new row to the database. This example uses the `Region` table in the Northwind database.
### To insert new records into a database by using the TableAdapter.Insert method
77
+
### Insert new records with TableAdapter.Insert method
60
78
61
-
If your application uses objects to store data, you can use the `TableAdapter.Insert` method to create new rows directly in the database. The `Insert` method accepts the individual values for each column as parameters. Calling the method inserts a new record into the database with the parameter values passed in.
79
+
If your application uses objects to store data, you can use the `TableAdapter.Insert` method to create new rows directly in the database. The `Insert` method accepts the individual values for each column as parameters. When you call the method, a new record is inserted into the database with the passed parameter values.
62
80
63
-
- Call the TableAdapter's `Insert` method, passing in the values for each column as parameters.
81
+
- Call the TableAdapter's `Insert` method, and pass the values for each column as parameters.
64
82
65
-
The following procedure demonstrates using the `TableAdapter.Insert` method to insert rows. This example inserts data into the `Region` table in the Northwind database.
83
+
The following procedure demonstrates how to use the `TableAdapter.Insert` method to insert rows. This example inserts data into the `Region` table in the Northwind database.
66
84
67
85
> [!NOTE]
68
-
> If you do not have an instance available, instantiate the TableAdapter you want to use.
86
+
> If you don't have an instance available, instantiate the TableAdapter that you want to use.
69
87
70
88
### [C#](#tab/csharp)
71
89
@@ -74,17 +92,16 @@ The following procedure demonstrates using the `TableAdapter.Insert` method to i
You can insert new records directly into a database using command objects.
98
+
## Insert new records with command objects
82
99
83
-
### To insert new records into a database by using command objects
100
+
You can insert new records directly into a database by using command objects.
84
101
85
102
- Create a new command object, and then set its `Connection`, `CommandType`, and `CommandText` properties.
86
103
87
-
The following example demonstrates inserting records into a database using command object. It inserts data into the `Region` table in the Northwind database.
104
+
The following procedure demonstrates how to insert records into a database by using command object. This example insert data into the `Region` table in the Northwind database.
88
105
89
106
### [C#](#tab/csharp)
90
107
@@ -93,12 +110,10 @@ The following example demonstrates inserting records into a database using comma
0 commit comments