HOW TO: Update a Database from a DataSet Object Using Visual Basic .NET
This article discusses a Beta release of a Microsoft product. The information in this article is provided as-is and is subject to change without notice.
No formal product support is available from Microsoft for this Beta product. For information about obtaining support for a Beta release, please see the documentation included with the Beta product files, or check the Web location from which you downloaded the release.
--------------------------------------------------------------------------------
The information in this article applies to:
Microsoft Visual Basic .NET Beta 2
--------------------------------------------------------------------------------
IN THIS TASK
SUMMARY
Requirements
How to Update a Database from a DataSet Object
Complete Code Listing
REFERENCES
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.
back to the top
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
This article assumes that you are familiar with the following topics:
Database terminology
Structured Query Language (SQL)
back to the top
How to Update a Database from a DataSet Object
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.
Understanding the concepts in the following Microsoft Knowledge Base article will help you understand this article:
Q301216 HOW TO: Populate a DataSet Object from a Database
Some of the topics that are covered in Q301216 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.
To update a DataSet and send those updates back to the database, follow these steps:
Open Visual Studio .NET
Create a new Console Application in Visual Basic .NET. By default, Visual Studio creates a Static Module and an empty Main() procedure.
Make sure that the project contains a reference to the System and System.Data namespaces. Use the Imports on the System , SystemData , and System.Data.SqlClient namespaces so that you are not required to qualify declarations from these namespaces later in your code. You must use these statements prior to any other declarations.
Imports System
Imports System.Data
Imports System.Data.SqlClient
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 Q301216 . 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) that has a blank password fo