How To Update A Database From A DataSet Object by Using Visual Basic
How To Update A Database From A DataSet Object by Using Visual Basic
NET
Store
Store home
Devices
Microsoft Surface
PCs & tablets
Xbox
Accessories
Windows phone
Microsoft Band
Software
Office
Windows
Additional software
Apps
All apps
Windows apps
Windows phone apps
Games
Xbox One games
Xbox 360 games
PC games
Windows games
Windows phone games
Entertainment
All Entertainment
Movies & TV
Music
Sale
Sale
Father's Day Gift Guide
Find a store
Gift cards
https://support.microsoft.com/en-us/kb/301248 1/10
6/16/2016 How to update a database from a DataSet object by using Visual Basic .NET
Products
For business
Cloud Platform
Microsoft Azure
Microsoft Dynamics
Windows for business
Office for business
Skype for business
Surface for business
Enterprise solutions
Small business solutions
Find a solutions provider
Volume Licensing
OneNote in classroom
Shop PCs & tablets perfect for students
Microsoft in Education
Support
Sign in
Cart
Cart
This article has been archived. It is offered "as is" and will no longer be updated.
For a Microsoft Visual C++ .NET version of this article, see 815660.
For a Microsoft Visual Basic 6.0 version of this article, see 190727.
IN THIS TASK
SUMMARY
Requirements
How to update a database from a DataSet object
Complete code listing
REFERENCES
https://support.microsoft.com/en-us/kb/301248 3/10
6/16/2016 How to update a database from a DataSet object by using Visual Basic .NET
Summary
DataSet objects, a key part of data access in the Microsoft .NET Framework, are in-memory objects that
can hold tables, views, and relationships. This article demonstrates how to take a DataSet that contains data
(which is loaded from a database), modify that data, and then send it back to the database to update the
original source.
Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs
that you need:
Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server, or
Windows NT 4.0 Server
Microsoft SQL Server version 7.0 or 2000 or Microsoft Data Engine (MSDE) with the PUBS sample
database installed
Microsoft Visual Studio .NET
The appropriate permissions and the correct values for connecting to your computer that is running
SQL Server
This article assumes that you are familiar with the following topics:
Database terminology
Structured Query Language (SQL)
This section demonstrates how to use the DataSet object to update data in a database. It is important to
remember that you can also use a SqlCommand object to insert, update, and delete data in a database
directly.
To help you understand this article, click the article number below to view the article in the Microsoft
Knowledge Base:
301216 How to populate a DataSet object from a database by using Visual Basic .NET
Some of the topics that are covered in 301216 include how to retrieve data from a database and into a
DataSet, and how the DataSet is separate and distinct from the database.
After the DataSet is loaded, you can modify the data, and the DataSet will track the changes. The DataSet
object can be considered an in-memory cache of data that is retrieved from a database and consists of a
collection of tables, relationships, and constraints.
https://support.microsoft.com/en-us/kb/301248 4/10
6/16/2016 How to update a database from a DataSet object by using Visual Basic .NET
To update a DataSet and send those updates back to the database, follow these steps:
Imports System
Imports System.Data
Imports System.Data.SqlClient
4. Before you can modify the data and submit the changes back to the database, you must load the
information into the DataSet. For the detailed procedure, refer to 301216. To avoid duplication, the
code in this step is not presented in detail.
The connection string in the following code points to a SQL Server that is located on the local
computer (or the computer where the code is running). Replace this string with your own settings, if
required. To summarize, a connection is created, and then a data adapter is created, which is used to
fill the DataSet with data.
Note In the sample code that appears in this article, you must change UID=UserName and
Password=StrongPassword to the correct values. Make sure that the user ID has the appropriate
permissions to perform this operation on the database.
' Modify the following code to correctly connect to your SQL Server.
sConnectionString = "Password=StrongPassword;User ID=UserName;" & _
"Initial Catalog=pubs;" & _
"Data Source=(local)"
' Create an instance of a DataSet, and retrieve data from the Authors table.
Dim dsPubs As New DataSet("Pubs")
daAuthors.FillSchema(dsPubs, SchemaType.Source, "Authors")
daAuthors.Fill(dsPubs, "Authors")
5. Now that the data is loaded, you can modify it. There are many ways to add a row (or record). This
code sample uses a three step procedure:
a. Obtain a new DataRow object from the DataTable.
Set the DataRow field values as necessary.
Pass that new object into the Add method of the DataTable.Rows collection.
https://support.microsoft.com/en-us/kb/301248 5/10
6/16/2016 How to update a database from a DataSet object by using Visual Basic .NET
'*****************
'BEGIN ADD CODE
' Create a new instance of a DataTable.
Dim tblAuthors As DataTable
tblAuthors = dsPubs.Tables("Authors")
'Pass that new object into the Add method of the DataTable.Rows collection.
tblAuthors.Rows.Add(drCurrent)
MsgBox("Add was successful.")
6. To edit existing rows, obtain the appropriate DataRow object, and then provide new values for one or
more columns. You must first find the correct row, which is much easier because you loaded the
schema of the table as well as the data (the call to FillSchema in step 4). With the schema in place, the
table knows which column is its primary key, and the Find method of the Rows collection is available.
The Find method returns the DataRow object with a specific value in its primary key (in this case,
au_id). After you have that DataRow, you can modify the columns. You do not have to wrap the
modifications in BeginEdit and EndEdit, but this simplifies the work that the DataSet has to do and
allows the DataSet to perform its validation checks all at once upon the EndEdit call. Paste the
following code after the ADD code:
'*****************
'BEGIN EDIT CODE
drCurrent = tblAuthors.Rows.Find("213-46-8915")
drCurrent.BeginEdit()
drCurrent("phone") = "342" & drCurrent("phone").ToString.Substring(3)
drCurrent.EndEdit()
MsgBox("Record edited successfully")
7. To update the original database with all of these changes, pass the DataSet into the Update method of
the DataAdapter object.
However, before you can call Update, you must set the InsertCommand, UpdateCommand, and
DeleteCommand properties of the DataAdapter object. You can manually write SQL and populate
these three properties with corresponding SqlCommand objects, but you can also use Visual Studio
https://support.microsoft.com/en-us/kb/301248 6/10
6/16/2016 How to update a database from a DataSet object by using Visual Basic .NET
To generate the required commands when they are needed, you must create an instance of the
SqlCommandBuilder object and use the DataAdapter in the constructor. If you want to use this
method, which is illustrated in the code sample to follow, you must have primary key information
available for your table. To access primary key information, call FillSchema, and then set the
MissingSchemaAction property of your DataAdapter to AddWithKey, or manually set the primary
key in your code. Paste the following code after the EDIT code:
'*****************
'BEGIN SEND CHANGES TO SQL SERVER
8. To delete a row completely, use the Delete method of the DataRow object. Note that the Rows
collection contains two methods, Remove and RemoveAt, which seem to delete the row but instead
just remove the row from the collection. Only the Delete method sends your deletion back to the
source database. Paste the following code after the SEND CHANGES TO SQL SERVER code:
'*****************
'BEGIN DELETE CODE
drCurrent = tblAuthors.Rows.Find("993-21-3427")
drCurrent.Delete()
MsgBox("Record deleted successfully")
9. Send the changes to SQL Server to remove the record that you added earlier. Paste the following
code after the DELETE code:
'*****************
' CLEAN UP SQL SERVER
daAuthors.Update(dsPubs, "Authors")
MsgBox("SQL Server updated successfully" & Chr(13) & Chr(13) & "Check Server Explorer to
see changes")
https://support.microsoft.com/en-us/kb/301248 7/10
6/16/2016 How to update a database from a DataSet object by using Visual Basic .NET
Imports System
Imports System.Data
Imports System.Data.SqlClient
Module Module1
Sub Main()
Dim sConnectionString As String
' Modify the following code to correctly connect to your SQL Server.
sConnectionString = "Password=StrongPassword;User ID=UserName;" & _
"Initial Catalog=pubs;" & _
"Data Source=(local)"
' Create an instance of a DataSet, and retrieve data from the Authors table.
Dim dsPubs As New DataSet("Pubs")
daAuthors.FillSchema(dsPubs, SchemaType.Source, "Authors")
daAuthors.Fill(dsPubs, "Authors")
'*****************
'BEGIN ADD CODE
' Create a new instance of a DataTable
Dim tblAuthors As DataTable
tblAuthors = dsPubs.Tables("Authors")
'Pass that new object into the Add method of the DataTable.Rows collection.
tblAuthors.Rows.Add(drCurrent)
MsgBox("Add was successful.")
drCurrent = tblAuthors.Rows.Find("213-46-8915")
drCurrent.BeginEdit()
drCurrent("phone") = "342" & drCurrent("phone").ToString.Substring(3)
drCurrent.EndEdit()
MsgBox("Record edited successfully")
drCurrent = tblAuthors.Rows.Find("993-21-3427")
drCurrent.Delete()
MsgBox("Record deleted successfully")
End Module
References
For more information about using ADO.NET, DataSet objects, and SQL, see the following Microsoft Web
sites:
http://msdn2.microsoft.com/en-us/library/ms810293.aspx
Properties
Article ID: 301248 - Last Review: 12/06/2015 03:16:58 - Revision: 8.0
Applies to
https://support.microsoft.com/en-us/kb/301248 9/10
6/16/2016 How to update a database from a DataSet object by using Visual Basic .NET
Keywords:
Support
Account support
Supported products list
Product support lifecycle
Security
Contact Us
Terms of use
Privacy & cookies
Trademarks
2016 Microsoft
https://support.microsoft.com/en-us/kb/301248 10/10