Tải bản đầy đủ (.pdf) (6 trang)

Tài liệu The DataSet Class pptx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (38.64 KB, 6 trang )

The DataSet Class
You use an object of the DataSet class to represent a local copy of the information stored
in the database. You can make changes to that local copy in your DataSet and then later
synchronize those changes with the database through a DataAdapter. A DataSet can
represent database structures such as tables, rows, and columns. You can even add
constraints to your locally stored tables to enforce unique and foreign key constraints.
Figure 10.1
shows the DataSet and its relationship to some of the objects you can store
within it. As you can see from this figure, you can store multiple DataTable objects in a
DataSet, and so on.

Figure 10.1: Some of the DataSet objects
Table 10.4
shows some of the DataSet properties.
Table 10.4: DataSet PROPERTIES
PROPERTY TYPE DESCRIPTION
CaseSensitive bool Gets or sets a bool value that indicates
whether string comparisons within
DataTable objects are case-sensitive.
DataSetName string Gets or sets the name of the current
DataSet object.
DefaultViewManager DataViewManager Gets a custom view of the data stored in
Table 10.4: DataSet PROPERTIES
PROPERTY TYPE DESCRIPTION
the DataSet object. You use a view to
filter, search, and navigate the DataSet.
EnforceConstraints bool Gets or sets a bool value that indicates
whether constraint rules are followed
when updating information in the DataSet
object.
ExtendedProperties PropertyCollection Gets a collection (PropertyCollection) of


user information. You can use the
PropertyCollection to store strings with
any additional information you want. You
use the Add() method through
ExtendedProperties to add a string.
HasErrors bool Gets a bool value that indicates whether
there are errors in any of the rows in the
tables of the DataSet object.
Locale CultureInfo Gets or sets a CultureInfo object for the
DataSet. A CultureInfo object contains
information about a specific culture
including its name, writing system, and
calendar.
Namespace string Gets or sets the namespace for the
DataSet object. The namespace is a string
that is used when reading and writing an
XML document using the ReadXml(),
WriteXml(), ReadXmlSchema(), and
WriteXmlSchema() methods. The
namespace is used to scope the XML
attributes and elements.
Prefix string Gets or sets the XML prefix for the
DataSet namespace. The prefix is used in
an XML document to identify the
elements that belong to the DataSet
object's namespace.
Relations DataRelationCollection Gets the collection of relations
(DataRelationCollection) that allows
navigation from a parent table to a child
table. A DataRelationCollection consists

of DataRelation objects.
Table 10.4: DataSet PROPERTIES
PROPERTY TYPE DESCRIPTION
Tables DataTableCollection Gets the collection of tables
(DataTableCollection) that contains the
DataTable objects stored in the DataSet.
Table 10.5 shows some of the DataSet methods.
Table 10.5: DataSet METHODS
METHOD RETURN
TYPE
DESCRIPTION
AcceptChanges() void Commits all the changes made to the DataSet object
since it was loaded or since the last time the
AcceptChanges() method was called.
BeginInit() void Used by the Visual Studio .NET designer to
initialize a DataSet used in a form or component.
Clear() void Removes all rows from all tables in the DataSet
object.
Clone() DataSet Clones the structure of the DataSet object and
returns that clone. The clone contains all the
schemas, relations, and constraints.
Copy() DataSet Copies the structure and data of the DataSet object
and returns that copy. The copy contains all the
schemas, relations, constraints, and data.
EndInit() void Used by the Visual Studio .NET designer to end
initialization of a DataSet used in a form or
component.
GetChanges() DataSet Overloaded. Gets a copy of all the changes made to
the DataSet object since it was last loaded or since
the last time the AcceptChanges() method was

called.
GetXml() string Returns the XML representation of the data stored
in the DataSet object.
GetXmlSchema() string Returns the XML representation of the schema for
the DataSet object.
HasChanges() bool Overloaded. Returns a bool value that indicates
whether the DataSet object has changes that haven't
been committed.
Table 10.5: DataSet METHODS
METHOD RETURN
TYPE
DESCRIPTION
Merge() void Overloaded. Merges this DataSet with another
specified DataSet object.
ReadXml() XmlReadMode Overloaded. Loads the data from an XML file into
the DataSet object.
ReadXmlSchema() void Overloaded. Loads a schema from an XML file into
the DataSet object.
RejectChanges() void Undoes all the changes made to the DataSet object
since it was created or since the last time the
AcceptChanges() method was called.
Reset() void Resets the DataSet object to its original state.
WriteXml() void Overloaded. Writes out the data from the DataSet
object to an XML file.
WriteXmlSchema() void Overloaded. Writes out the schema of the DataSet
object to an XML file.
Table 10.6 shows one of the DataSet events.
Table 10.6: DataSet EVENT
EVENT EVENT HANDLER DESCRIPTION
MergeFailed MergeFailedEventHandler Fires when an attempt is made add a DataRow

to a DataSet when a DataRow with the same
primary key value already exists in that
DataSet.
In the next section, you'll learn how to create a DataSet object.
Creating a DataSet Object
You create a DataSet object using one of the following DataSet constructors:
DataSet()
DataSet(string dataSetNameString)
where dataSetNameString is the string assigned to the DataSetName property of your
DataSet object. The setting of the DataSetName property is optional.
The following example uses the DataSet() constructor to create a DataSet object:
DataSet myDataSet = new DataSet();
The next example uses the DataSet(string dataSetNameString) constructor to create a
DataSet object:
DataSet myDataSet = new DataSet("myDataSet");
Populating a DataSet Object
In this section, you'll learn how to populate a DataSet using the Fill() method of a
DataAdapter. Specifically, you'll see how to populate a DataSet using
• A SELECT statement
• A range of rows
• A stored procedure
Using a SELECT Statement
Before you populate a DataSet you first need a Connection, a Command, and a
DataAdapter:
SqlConnection mySqlConnection =
new SqlConnection(
"server=localhost;database=Northwind;uid=sa;pwd=sa"
);
SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
mySqlCommand.CommandText =

"SELECT TOP 5 ProductID, ProductName, UnitPrice " +
"FROM Products " +
"ORDER BY ProductID";
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
mySqlDataAdapter.SelectCommand = mySqlCommand;
DataSet myDataSet = new DataSet();
mySqlConnection.Open();
Notice the mySqlCommand object contains a SELECT statement that retrieves the
ProductID, ProductName, and UnitPrice columns of the top five rows from the Products
table.

RETRIEVING FROM MULTIPLE TABLES
Of course, you're not limited to a SELECT statement that retrieves from a single table.
You can use a SELECT statement that retrieves from multiple tables using a join,
however, you should typically avoid doing that because a DataTable is meant to be used
to store rows from a single database table.

Next, to populate myDataSet with the rows from the Products table, you call the Fill()
method of mySqlDataAdapter. For example:
int numberOfRows = mySqlDataAdapter.Fill(myDataSet, "Products");
The int returned by the Fill() method is the number of rows synchronized between the
DataSet and the database via the DataAdapter. In the previous example, the int is the
number of rows copied from the Products table to myDataSet and is set to 5-the number
of rows retrieved by the SELECT statement shown earlier.
The first parameter to the Fill() method is your DataSet, and the second parameter is a
string containing the name you want to assign to the DataTable created in your DataSet.

N
ote The name you assign to your DataTable doesn't have to be the same as the name of
the database table. You can use any string of text, though typically you should still

use the same name, since it will help you keep track of what database table was
used to populate the DataTable.
When you call the Fill() method for the first time, the following steps are performed by
ADO.NET:
1. The SELECT statement in your SqlCommand is executed.
2. A new DataTable object is created in your DataSet.
3. Your DataTable is populated with the result set returned by the SELECT
statement.
If you're finished with the database after calling the Fill() method, you should close your
Connection object using the Close() method:
mySqlConnection.Close();

×