Beruflich Dokumente
Kultur Dokumente
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
10
Basic Data-Access
Techniques
CHAPTER
10
In this chapter, you will
• Access and manipulate data from a Microsoft SQL Server database
• Learn to create and use ad hoc queries and stored procedures
• Access and manipulate data from a data store
• Use XML techniques with ADO.NET
• Handle data errors
In this chapter, we will first look at the basics of ADO.NET, accessing data (using
SQL), and connecting to data providers. Then we’ll look at how this is all done from C#.
ADO.NET
ADO.NET is a group of classes that are used to access data, and it is at the center of the
.NET Framework. As these classes are part of the .NET Framework, they also make use of
the CTS and CLR and common XML support.
One of the purposes of the ADO.NET classes is to provide an architecture for building
scalable robust enterprise applications, without adding too much overhead. ADO.NET
does this by providing an application architecture that provides recordsets that are dis-
connected from the data store, which is a natural for multi-tier Application models. Be-
cause XML is the language that ADO.NET uses to move data between components, and
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:10 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
10
Application Models
In the realm of software engineering, there are a number of ways to describe how an ap-
plication is structured, from an architectural point of view—these are called models. The
most common model used by Microsoft, both in its literature as well as by its consulting
service is called the services model and it is based on the theory that every application can
be broken down into three distinct services: Presentation Service, Business Service, and
Data Service. Table 10-1 outlines which tasks belong in each service.
EXAM TIP The exam will test your understanding of the three services in
the services model, not the software design of the services.
How these services are combined into an application architecture determines the Ap-
plication model, and each has benefits and disadvantages. The model can be one of four
special types: 1-tier, 2-tier, 3-tier, or n-tier. Figure 10-1 shows a graphical view of the Ap-
plication models.
One-Tier
This is the traditional mainframe model; the three services are physically located in one
computer. The 1-tier model can also be used to describe a single computer with all ser-
vices being used by one single user. (See Figure 10-1.)
Service Description
Presentation The Presentation Service is responsible for drawing (rendering) the user
interface (UI). It will request data from the Business Service and send updates
or new data to the Business Service to be forwarded to the Data Service.
The Presentation Service is commonly an application that runs on the client
computer. For example, the web page displaying the corporate phone list is
a Presentation Service, as is the C# Windows Form application displaying the
same information to users on the local network.
Business The Business Service is responsible for ensuring that the data and processing
meets the requirements of the business (enforcing business rules). An example
of a business rule would be “Don’t ship goods to a customer who has an overdue
account.” Some business rules are very changeable, and can be modified almost
on a daily basis. This is one of the challenges of the system architect who must
identify the business rules and correctly place them within the architecture.
Data The Data Service is the encapsulation of the data source, allowing the Business
Service access to any data without the Business Service having to know how
that data is stored. This service is the realm of ADO.NET.
Table 10-1 Services in the Service Model
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:10 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
10
PART II
One-Tier Advantages Because all the services are in one place, this type of appli-
cation is fairly simple to develop.
One-Tier Disadvantages Any update to any of the services will interfere with the
other services and the application as a whole. This type of application is typically totally
rebuilt when even a small change is needed.
Two-Tier
This is the classic client/server model as it was introduced to offload the Presentation
and sometimes the Business Services from the mainframe. The two-tier model usually
involves one computer for the client and a central server that is shared among clients.
See Figure 10-2 depicting the Intelligent Client model and Figure 10-3 that shows the In-
telligent Server model.
Two-Tier Advantages Some of the services are separated. If designed properly,
changes will be trivial when requirements change.
Two-Tier Disadvantages This model is not very scalable if the client contains
some of the business rules; even if the business rules are all on the server, there are still
scalability issues. This model also requires more hands-on management, costing more
to maintain than other models.
Three Tier
In this model, the services are each in their own layer or tier, and the business rules are in
a new middle tier. See Figure 10-4.
Figure 10-2
Intelligent Client
2-tier Application
model
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:11 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
10
Three-Tier Advantages This model separates the functions of the services into
tiers, providing a “thin client” that is separated from the Business and Data Services.
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:11 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
10
-Tier
This model is the neural network of Application models. The Business and Data Services
are spread out among numerous systems that provide a scalable environment. See Fig-
ure 10-5.
PART II
n-Tier Advantages This model makes it possible for different applications on
different platforms and operating systems to interact with both the data and the user.
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:11 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
10
ADO.NET Architecture
ADO.NET has evolved from DAO (Data Access Objects), VBSQL (Visual Basic SQL),
RDO (Remote Data Objects), and ADO (ActiveX Data Objects), but it does not share
the same programming model, even though most of the functionality is the same. The
different data-access technologies represent the history of how Microsoft has sup-
ported database developers over the different versions of development tools and oper-
ating systems. DAO was introduced with VB 3 to support Access development, VBSQL
was a technology that allowed VB programmers to access SQL Server data, RDO pro-
vided for disconnected recordsets, and ADO gave us COM and data.
Microsoft defines ADO.NET as being “A set of classes for working with data.” In other
words the ADO.NET “package” is an object model that helps us work with data: any
data, from anywhere, using any storage technology.
These are some of the advantages of ADO.NET:
• Interoperability The language used to transfer data between the data source
and the in-memory copy of the data is the standard XML document, which
allows seamless data interoperability between dissimilar systems.
• Maintainability ADO.NET maintains local in-memory caches of the data,
making it possible to spread the application logic between many tiers in an
n-tier application. This makes the application more scalable.
• Programmability ADO.NET is based on the .NET Framework, which uses
strongly typed data types. Strongly typed data makes the source code more
concise and less prone to “undocumented features” (bugs).
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:12 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
10
PART II
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:12 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
10
To use ADO.NET, you need to use its related namespaces, listed in Table 10-2.
Namespace Description
System.Data Contains the core classes of ADO.NET, including the classes
that enable disconnected data (such as the DataSet class).
System.Data.Common Contains utility classes and interfaces that the data providers
inherit and implement.
System.Data.SqlClient Contains the SQL Server .NET data provider.
System.Data.OleDb Contains the OLE-DB .NET data provider.
System.Data.SqlTypes Contains classes and structures that encapsulate the native SQL
Server data types. This is a type-safe faster alternative to native
data types.
System.Xml Contains the support for the XML standard, including classes
for processing and encapsulating an XML document (such as
the XmlDataDocument class).
Table 10-2 ADO.NET Namespaces
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:13 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
10
PART II
DataSet). The data binding provides the underlying services needed to build data
forms easily.
.NET Data Providers The ADO.NET classes contain .NET data providers that en-
capsulate a connection to a data source and the functionality to read, change, and up-
date data in the data source. The .NET data providers are designed to be lightweight and
include a minimal abstraction layer between the data source and your code. Microsoft
supplies three .NET data providers for you to use, as listed in Table 10-4.
There are four objects in each of the .NET data providers, as listed here (the prefix re-
placing the Xxx for each of these objects is specific to the provider):
Object in
Collection Description
Columns DataColumn The DataColumn object contains data that describes the
data in the column (metadata), such as the column name,
the data type, whether the column can be NULL, and so on.
Rows DataRow DataRow encapsulates a row of data in the table. The
DataRow object also includes the original row data
before any changes were made.
Constraints Constraint Constraint is an abstract class. It represents the
constraint on one or more DataColumn objects.
The collection can use any derived class or the two
concrete subclasses: UniqueConstraint and
ForeignKeyConstraint.
ChildRelations DataRelation DataRelation objects are used to represent
relationships between columns in different tables.
Use a DataRelation object to link (join) two
tables on the primary and foreign keys.
Table 10-3 Collections in the DataTable Object
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:13 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
10
EXAM TIP The different providers and the products they service will be
tested in the exam.
The XxxDataAdapter lets you manage the disconnected nature of the ADO.NET
environment by acting as the manager of the XxxConnection and DataSet objects.
You use the XxxDataAdapter to populate the DataSet and to update the data source
with any changes that have been made to the DataSet.
Some objects also have child objects associated with them. For example, the
XxxConnection object has an XxxTransaction object and an XxxError object
that expose underlying functionality.
Object Description
XxxConnection The XxxConnection object is used to encapsulate the connection
between the code and a specific data source.
XxxCommand XxxCommand objects are used to execute commands on the data source.
In the case of SQL Server, the SqlCommand is used to execute a stored
procedure on the server.
XxxDataReader The XxxDataReader provides a forward-only read-only data stream
from the data source. You can access the data stream through the
ExecuteReader method of the XxxCommand object. The
xxxCommand object is usually the result of an SQL SELECT
statement or a stored procedure call.
XxxDataAdapter The XxxDataAdapter provides the services to connect a DataSet
to an XxxCommand. It populates the DataSet and resolves any updates
with the data source.
Table 10-5 The Objects of the .NET Data Provider
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:13 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
10
PART II
standard that can be used for many different purposes. The following list identifies just
some of the things you can do with XML support in ADO.NET:
The most exciting fact about XML is that it is the standard format for exchanging data
between dissimilar environments. XML is the basis for B2B (business-to-business)
e-commerce and is rapidly replacing proprietary protocols for data exchange.
EXAM TIP XML is such an important technology for the .NET Framework
that you can expect XML to be part of many exam questions.
Data-Access Basics
Before we get into the details of using ADO.NET, we should take a look at the basics of
data access, namely at SQL (structured query language) and transactions. The ADO.NET
environment uses the standard ANSI92 SQL language for DML (data modification lan-
guage). DML is the three commands that modifies data in SQL (INSERT, UPDATE, and
DELTE), and it exposes the transaction model of the underlying data source, making it
possible to take advantage of those database managers that provide transactions.
SQL
SQL is a language, even though Microsoft calls their database server SQL Server, and in
this section we will look at the DML elements of the language (SELECT, INSERT,
UPDATE, and DELETE) that are used to manipulate data stored in a Relational Database
Manager system (RDBMS). We will start with the SELECT statement, which returns in-
formation from a database, and then look at how to modify the content of the tables
in a database by using INSERT, UPDATE, and DELETE statements.
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:14 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
10
EXAM TIP The SQL statements will be used in many different questions. It is
very important to have mastery over the SQL language.
SELECT
You use SELECT statements to retrieve data from tables in a database. The SELECT
statement is the basic command for querying the database. In the statement, you specify
the columns and tables you want data from, and you can optionally specify conditions
and sorting instructions. The full syntax for the SELECT statement is rather complex; we
will look at a shorter syntax listing with the most commonly used options:
The columns to be returned are listed in the select_list parameter. Use a comma
to separate the column names or use the column wildcard character (*) to select all col-
umns in the table. The ALL argument specifies that all rows in the table_source
should be returned, even if there are duplicate rows. The DISTINCT argument removes
all duplicates in the returned data. ALL is the default.
The FROM clause specifies the tables that the columns will be returned from. The
FROM clause is mandatory, and you must provide at least one table name.
The following example returns all the staff from the Northwind Trading database
(the query is executed against an SQL Server 2000 database):
The SELECT statement returned all the rows in the table. If you only want the staff
working in London, you can include a WHERE clause. The WHERE clause limits the number
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:14 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
10
PART II
The result of this SELECT statement is as follows:
The WHERE clause can compare columns against literal values using the logical opera-
tors listed in Table 10-6. String literals in SQL are enclosed in single quotes (').
The WHERE clause has some additional tricks we can take advantage of. For example,
to search for records where we only know part of the data in a column, we can use the
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:14 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
10
The percent sign (%) is the wildcard character that is used with all string and character
comparisons in the SQL language, so 'An%' translates to any string that starts with “An”.
If you are looking for a substring, you can use multiple percent signs.
The result of the preceding query is that only records that match the LIKE argument
are returned:
In our next example, we want to list all employees that have “ll” in their last names:
The other clause we haven’t looked at yet is the ORDER BY clause. If you look back at
the first result we received in this section, when we selected all the staff, you will find
that it is not sorted on any of the columns, and it seems to be returned in a random order.
If we go back again and run the same query, we might get our results in the same order, but
more likely we will not. Unless we specify an order, there is no guarantee as to what order
the data will be returned in.
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:15 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
10
PART II
ORDER BY LastName
You can combine these SELECT clauses as you need them. Here are some recommen-
dations for working with SELECT statements:
• Never use the column name wildcard (*) in the SELECT statement;
list all the columns you need instead.
• Always include a WHERE clause to limit the number of rows returned.
• If you need the data sorted, use the ORDER BY clause.
JOIN You will often need to combine data from two or more tables, and the JOIN
clause allows you to perform this task. JOIN statements are used to query any number
of tables and return a single result set that contains merged data from these tables. Joins
are a central part of relational database theory and are used in the real world to imple-
ment relations between entities in a normalized data model.
There are three types of joins in SQL: inner joins, outer joins, and cross joins. These joins
are described in Table 10-7.
The syntax for an inner join is as follows:
SELECT select_list
FROM first_table_name
[INNER] JOIN join_table_name
ON join_condition
The ON keyword defines the comparison that must be true for the inner join to return the
row. The INNER keyword is optional, as it is the default join in the ANSI92 SQL standard.
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:15 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
10
Let’s look at an example. Figure 10-7 shows the relationships between three tables.
The relationship is set up to enable us to join the three tables together. The
EmployeeID column is used to connect the Employees and EmployeeTerritories tables, and
the TerritoryID column is used to connect the EmployeeTerritories and Territories tables.
If we needed to query this database and return TerritoryDescription, FirstName,
and LastName for an employee with a last name of Buchanan, we could use the follow-
ing query:
USE Northwind
SELECT TerritoryDescription, FirstName, LastName
FROM Employees
JOIN EmployeeTerritories
ON Employees.EmployeeID = EmployeeTerritories.EmployeeID
JOIN Territories
ON EmployeeTerritories.TerritoryID = Territories.TerritoryID
WHERE LastName = 'Buchanan'
This query will return all records for employees named Buchanan where there is an en-
try for a territory.
Let us take a look at what happened. The SELECT line specifies the columns that we
need; notice that we used the name of the column from the Territories table without
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:15 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
10
PART II
Figure 10-7 Table relationships for an inner join example
specifying what table it came from. As long as the column names are unique we do not
have to specify the table name as well. In the FROM clause we added the JOIN clause to
specify that we want the tables on either side of the JOIN clause to be connected. The ON
statement sets the rules of the connection; in our case, we want the Employees table
joined to the EmployeeTerritories table using the EmployeeID column in both tables.
When there are columns in two tables that have the same name, we use a syntax that
specifies the table and the column names in a dotted format: table.column (for ex-
ample, Employees.EmployeeID). You must use this format in the ON clause unless
the two columns have unique names.
Finally we join the Territories table to the result of the first JOIN. This results in the
preceding output. The default behavior of the JOIN clause is to return all records that
match the ON clause from the two tables, and this is known as an inner join.
EXAM TIP Remember the syntax for the JOIN operation, and remember
that the inner join is the default JOIN.
In the next example, we will use aliasing to make the code easier to read. Figure 10-8
shows the model for the example. We want a query that returns the CategoryName,
ProductName, and Supplier for the beverages category, and we want to sort the output
on the ProductName. The following query performs that task:
USE Northwind
SELECT CategoryName, ProductName, CompanyName
FROM Categories c
JOIN Products p
ON c.CategoryID = p.CategoryID
JOIN Suppliers s
ON p.SupplierID = s.SupplierID
WHERE CategoryName = 'Beverages'
ORDER BY ProductName
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:15 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
10
The biggest difference between this example and the previous one is that we used
aliases to identify the tables. The following code segment defines c as the alias for the
Categories table and p as the alias for the Products table:
FROM Categories c
JOIN Products p
We can now use c and p to refer to the tables, simplifying the query.
The result of the preceding query is as follows:
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:16 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
10
PART II
INSERT [INTO] table_name [(column1, column2, …, column)]
VALUES (value1, value2, …, value3)
The column list following the table_name allows you to specify the order in which
data is inserted. If the column list is not used, the values must be listed in the column or-
der of the table.
For example, to insert a new employee in the Employees table, you could use the fol-
lowing statement:
USE Northwind
INSERT Employees (FirstName, LastName)
VALUES ('Robert', 'Burns')
TIP The column list of the INSERT statement is optional. If it is not used,
the order of the values in the VALUE clause must match the column order
of the table.
To insert data from a query into an existing table, you can use the INSERT … SELECT
statement. The syntax is as follows:
INSERT table_name
SELECT select_list
FROM table_source
[WHERE condition]
The result set from the SELECT statement will be added to the table_name table.
There are some rules that you need to consider when using this technique:
• The data type of the columns in the result set should match the data types of
the columns in the table.
• The result set must have data for all required columns in the destination table.
The following example takes all our employees and adds them to the Customers ta-
ble so our staff can also be our customers. We will build the CustomerID column data
by taking the first three characters from the first name and the first two characters from
the last name and concatenating them. The employee’s first name is used as the contact
name, and the last name as the company name.
USE Northwind
INSERT Customers
SELECT substring(FirstName, 1, 3) + substring(LastName, 1, 2),
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:16 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
10
To create a new table from the query, you use this syntax:
SELECT select_list
INTO new_table
FROM table_source
[WHERE condition]
The new_table can be either a local temporary table (#table_name), global tem-
porary table (##table_name), or a permanent table (table_name). One pound sign
(#) indicates a local table that will be available as long as the session that created it is
open; two pound signs (##) is a globally available table that will exist until it is no lon-
ger used in any session. In order to be able to create a permanent table, the administra-
tor of the database must have enabled SELECT INTO.
The select_list is commonly used to alias column names to new names for the
new table. The AS keyword is used to change the name of a column. In the following ex-
ample, we will retrieve the pricelist from the Products table and save the product and the
price in a new table. We will also calculate a 25 percent sales tax on the price.
USE Northwind
SELECT ProductName AS Product
, UnitPrice AS Price
, (UnitPrice * 0.25) AS SalesTax
, UnitPrice + (UnitPrice * 0.25) AS NewPrice
INTO #SalesTaxTable
FROM Products
The preceding example created a new local table named #SalesTaxTable. To query the
new table you could execute this query:
USE Northwind
SELECT *
FROM #SalesTaxTable
UPDATE
You can use the UPDATE statement to make changes to one or more rows at a time.
The syntax for the UPDATE statement is as follows:
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:16 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
10
As you use the UPDATE statement, you should be aware of some rules and recom-
mendations:
• Use the WHERE condition to control which rows are updated; if you don’t use
a WHERE condition, every row in the table is updated.
PART II
• Use the SET keyword to specify the new value for a column in the row.
• The UPDATE statement will only work on one table at a time.
If we wanted to increase the unit price for the products that were supplied by New Or-
leans Cajun Delights (SupplierID = 2) by 25 percent, we could use the following:
USE Northwind
UPDATE Products
SET UnitPrice = UnitPrice * 1.25
WHERE SupplierID = 2
DELETE
Use the DELETE statement to remove rows from a table. The DELETE statement has the
following syntax:
DELETE table_name
[WHERE condition]
If you issue the DELETE statement without a WHERE clause, the statement will remove
all the rows in the table.
To remove rows representing products that were shipped before November 1, 2001,
you could use this code:
USE Northwind
DELETE Orders
WHERE shippeddate < '11/1/2001'
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:17 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
10
The ACID properties ensure predictable behavior and the all-or-nothing nature of a
transaction. A database system that does not provide transactions or can’t meet the
ACID properties is considered unsuitable for anything beyond personal use.
In SQL you can control transactions using the transaction control statements shown
in Table 10-8. They can be used as part of any SQL process.
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:17 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
10
BEGIN TRANSACTION
INSERT INTO BankAccount (AccountNUM, Amount, Type)
VALUES (424242, 100, 'debit')
INSERT INTO BankAccount (AccountNUM, Amount, Type)
VALUES (121212, 100, 'credit')
PART II
IF (@@ERROR > 0) ROLLBACK TRANSACTION
ELSE COMMIT TRANSACTION
There are two data-modification statements in this example that insert debit and credit
rows in the specific accounts. If there are any errors during this processing, the global
@@ERROR variable will be set to a non-zero value. A non-zero value will cause a rollback,
otherwise we commit.
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:17 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
10
• Windows Authentication uses the user’s login credentials from the Windows
Active Directory to authenticate against the database server. The resulting
connection is called a trusted connection because SQL Server trusted the
Windows credentials to be safe.
• SQL Server Authentication requires the user to log in to SQL Server with an
additional username and password. The resulting connection is called an
untrusted connection because SQL Server did not trust anyone but itself to
provide the authentication.
When the server is configured, the administrator can enable either Windows Authenti-
cation or Mixed Mode, which adds the SQL Server Authentication.
Depending on the type of application you are writing and the type of client that will
use the application, there can be reasons to use either method to authenticate. Windows
Authentication is the preferred method when all clients are in the same company and
you are building an internal application. SQL Server Authentication is normally used
when building Web Services, or when you have non-Windows clients using the application.
Connection Strings
In order to connect to a data source, you need to build a connection string that will de-
fine the context of the connection. The parameters of the connection string will differ
somewhat depending on the data provider and are listed in Table 10-10.
Connections
The .NET data providers give us the three connection classes: SqlConnection (for use
with SQL Server 7.0 or higher), OleDbConnection (for use with data sources through
OLE-DB providers), and OdbcConnection (for use with legacy ODBC drivers).
Let’s look at a few example connection strings and connections for a number of dif-
ferent data sources. Suppose we want to make a connection to an SQL Server 2000 data
provider with the following parameters:
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:17 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
10
PART II
strCn = "User ID=sa;Password=42;Initial Catalog=Marvin;";
strCn = strCn + "Data Source=Hjalmar;Connection TimeOut=60;";
// now we define the connection object and open the connection
SqlConnection sqlCn = new SqlConnection(strCn);
sqlCn.Open();
c:\data\Marvin.mdb
Finally we’ll connect to the following SQL Server 6.5 data provider:
Description
Provider The Provider property is used to specify the OLE-DB provider
to use; this property is only used with the OLE-DB .NET data
provider.
Connection Timeout The number of seconds to wait for a server to reply; 15 seconds
is the default.
Initial Catalog The name of the database to connect to.
Data Source The name (or address) of the database server to connect to.
User ID The username if connecting using SQL Server Authentication.
Password The password to use if connecting using SQL Server
Authentication.
Trusted Connection True or False; specifies whether the connection is going to be
encrypted.
Persist Security Information Specifies whether sensitive security information is to be resent if a
connection is reopened. The default is False; changing this property
to True can be a security risk.
Table 10-10 Connection String Parameters
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:18 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
10
sqlCn.Close();
It is permissible to let the connection object go out of scope and have the garbage col-
lector deal with it, but that would leave the connection active on the data source until
the garbage collector runs. However, that would destroy any scalability dreams we had
for this application, so it is important to manage the connections to keep the load on the
server as low as possible.
The Close() method closes down the connection to the data source, but it will not
release any memory object, like the connection object itself. To remove the object from
memory, you can call the Dispose() method of the object:
sqlCn.Dispose();
The following code segment shows the flow of creating and disposing of objects:
string strCn;
strCn = "Provider=SQLOLEDB.1;Data Source=SalesServer03;Initial catalog=Pubs;";
OleDbConnection oleCn = new OleDbConnection(strCn);
oleCn.Open();
// perform some database operations
Connection Pooling
Data providers have the ability to do connection pooling. Connection pooling is the
mechanism of keeping connections open and reusing one unused connection for more
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:18 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
10
PART II
increasing the number of connections.
The key to making connection pooling possible is having connection strings that are
absolutely identical, including the security settings; if the strings are identical, the pro-
vider creates a pool. When a connection is attempted in a pool and there is an unused
connection, it will be used; if there are no available unused connections, a new connec-
tion is created and added to the pool.
The connection string can contain a number of parameters that are used to control
the behavior of connection pooling. These parameters are listed in Table 10-11.
Error Handling
Whenever you access resources that are remote from your application, such as a data
source, you must include error handling to ensure that unhandled errors never reach the
user. In Chapter 7 you were introduced to exception handling using the try … catch …
finally structure; we will use that exception handling here.
Description Default
Connection Lifetime When a connection is returned to the pool, this parameter 0
is compared to the creation time of the connection. If the
connection has been active longer than the parameter
allows, it is destroyed. The default of 0 translates to
unlimited lifetime.
Connection Reset If this is set to True, the connection is reset to its initial True
context when removed from the pool.
Enlist When this is set to True, the pool controller will True
automatically join a transaction if one is present.
Max Pool Size This parameter specifies the maximum number of 100
connections that are allowed in the pool.
Min Pool Size This parameter specifies the minimum number of 0
connections in the pool.
Pooling This parameter turns connection pooling on (True) True
or off (False).
Table 10-11 Connection String Parameters for Connection Pooling
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:18 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
10
catch( Xcp e)
{
Console.WriteLine(Xcp.ToString());
}
finally
{
odbcCn.Close();
odbcCn.Dispose();
odbccn = Null;
}
The try block contains all the database operations we need to perform. If we fail to
create the connection object, the first catch block will execute. Any other error will be
handled by the second handler, and finally we close the database.
TIP In real life, the code that deals with potential database errors will always
be longer than the code that performs the action.
You need to define catch blocks for all the possible exceptions that can be thrown in
your data-access application. Handle the exceptions or throw them up the calling chain
until they are handled, but never let an unhandled exception reach the end user.
The data source can raise events to inform the user of potentially important informa-
tion, and each data source will have different messages. The SqlConnection has an
InfoMessage event that we can use to get access to these messages. The SqlException
class contains the exception that is thrown when SQL Server returns an error or a warning.
The SqlException class always contains at least one instance of SqlError. By using
the severity level of the SqlError object, you can determine how big a problem the server
has, and what has happened to the connection. Table 10-12 lists the severity properties.
The SqlException object holds a collection of SqlError objects. To work with
them, you can use code similar to the following sample:
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:18 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
10
PART II
Table 10-12 Severity Levels in SQL Server Messages
The command object has a number of properties and methods that are used to ma-
nipulate the object. Table 10-13 lists the members of the command object.
The command object contains a parameters collection that can be used when calling
stored procedures on the database server (a stored procedure is a database object that is
used as a function). Suppose we have been given the following specifications for the
stored procedure: the name is GetProcCat, @CatID is an int as an input, @CatName
Member Description
CommandText The text that defines the command object
CommandType The type of the command, either command text (generic), SQL
statements, stored procedure, or undefined; the undefined type
indicates to the command object that the command type is unknown
Connection The connection to use for this command object
ExecuteNonQuery Executes the command that performs some data processing; returns
the number of rows affected
ExecuteReader Executes the command, returning a rowset
ExecuteScalar Executes the command, returning a single value
ExecuteXmlReader Executes the command, returning an XML result
Parameters If the CommandType is a stored procedure, the Parameters
collection can hold the parameters for the command
Table 10-13 The Members of the Command Object
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:18 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
10
In this example, we created the parameters and added them to the collection in the
command object. The direction of the parameter is set with the assignment to the Di-
rection parameter. Before we can execute the command object, we need to look at the
specification of the stored procedure to see what is returned from the stored procedure.
If the command object is not configured to accept return values we will not be able to re-
trieve those values. For us to access these return values we must use a different execute
method.
The following example executes the command and assigns the output to a variable:
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:19 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
10
PART II
sqlCn.Open();
SqlDataReader sqlDr;
sqlDr = sc.ExecuteReader(CommandBehavior.CloseConnection);
// The reader will close the connection when done
// now iterate through the DataReader
do while (sqlDr.Read())
{
Console.WriteLine(sqlDr.GetString(0) + " " + sqlDr.GetString(1));
}
sqlDr.Close();
We are now able to get and display data from our data sources. This is only part of the
story, though. The next section will explore the DataSet and the related objects that
give us the in-memory cache of data.
This line creates a new DataSet that we called Northwind using dsNw as the vari-
able name.
DataTable objects are added to the DataSet object’s Tables collection by using
the Add method of the collection:
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:19 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
10
After the structure of the DataTable has been defined, you can start adding data to
the DataSet by creating DataRow objects for the data:
DataRow dr = dtEmployees.NewRow();
dr("EmployeeID") = 42;
dr("LastName") = "Smith";
dr("FirstName") = "Bob";
dtEmployees.Rows.Add(dr);
The preceding five lines of code could also be written as one line:
By using the Object() class, we can create new rows in one step rather than five.
The order of the column data must be the same as it was defined in DataRow.
EXAM TIP We can’t state too strongly the fact that XML is central to all
data movement in ADO.NET.
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:19 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
10
PART II
The XmlReadMode parameter can be any of the values listed in Table 10-14.
The following code example loads a schema into the DataSet and then loads the
XML documents:
XmlReadMode
Description
Auto Examines the XML document and selects the action accordingly from
these choices:
If the DataSet already has a schema, or the document contains an
inline schema, it sets XmlReadMode to ReadSchema.
If the DataSet does not already have a schema and the document does
not contain an inline schema, it sets XmlReadMode to InferSchema.
The use of an XmlReadMode of Auto does not give the best performance;
specifying a mode will always be better. Auto is the default value.
DiffGram Reads a DiffGram (a format that contains both the original and the
current values of the data) applying changes from the DiffGram to
the DataSet.
Fragment Reads XML documents, such as those generated by executing FOR XML
queries against an instance of SQL Server. When XmlReadMode is set
to Fragment, the default namespace is read as the inline schema.
IgnoreSchema Ignores any inline schema and reads data into the existing DataSet
schema. If any data does not match the existing schema, it is discarded
(including data from differing namespaces defined for the DataSet).
Table 10-14 XmlReadMode Values
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:19 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
10
The resulting DataSet (xmlDataS) now holds the content of the Order.xml file and
the schema is set by Order.xsd. Any data that does not match the Order.xsd schema
will be discarded.
In the next example, we will read the XML data and the inline schema of the document:
// read in the Order.xml document using the inline schema of the file.
xmlDataS.ReadXml("Order.xml", XmlReadMode.ReadSchema);
}
catch(Exception e)
{
Console.WriteLine("Exception: " + e.ToString());
}
}
The ReadSchema mode will read the inline schema from the XML document; the
schema and the data are then loaded into the DataSet.
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:19 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
10
EXAM TIP The schema is usually reused during a read operation when the
data is to be imported into another application.
PART II
In the following example, we will write out the schema resulting from a ReadXml
call with the mode set to ReadSchema:
// read in the Order.xml document using the inline schema of the file.
xmlDataS.ReadXml("Order.xml", XmlReadMode.ReadSchema);
After the preceding method executes, the schema of the Order.xml document is writ-
ten into the Order.xsd file, which can be used in further processing.
To save the data from a DataSet, we use the WriteXml method, which can save to
either a file, Stream, TextWriter, or XmlWriter. The syntax is as follows:
The XmlWriteMode parameter specifies how the schema of the DataSet should be
dealt with. Table 10-15 details the values.
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:20 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
10
// read in the Order.xml document using the inline schema of the file.
xmlDataS.ReadXml("Order.xml", XmlReadMode.ReadSchema);
The resulting file (Orders.xml) contains the inline schema of the DataSet and the data.
• The XmlDataDocument gives you the ability to work with any data by using
the Document Object Model (DOM).
• An XmlDataDocument can be synchronized with a DataSet so that any
changes in one will be reflected in the other.
• When an XML document is loaded into an XmlDataDocument, the schema
is preserved; the DataSet does not preserve the entire schema, only the parts
that are used in that particular DataSet.
XmlDataDocument xmlDoc;
xmlDoc = new XmlDataDocument(theDataSet);
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:20 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
10
PART II
3. Load the XML data into the XmlDataDocument object.
The result of the preceding segment is that the two objects (theDS and theDoc) are
synchronized and theDS provides the relational view of the hierarchical data in theDoc.
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:20 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
10
• OleDbDataAdapter Use this adapter with any data source that can be
accessed through an OLE-DB provider.
• SqlDataAdapter Use this adapter to access Microsoft SQL Server 7.0
or higher.
The DataAdapter classes expose some properties that are used to configure the op-
eration of the adapter, and they are listed in Table 10-16.
The SQL statement that are stored in the properties can be an SQL sentence or a stored
procedure call; the properties are used to define the behavior of the DataAdapter.
When you create a DataAdapter, you do not have to create all four commands—to
create a read-only DataAdapter, only the SelectCommand is needed; if inserts are re-
quired, define the InsertCommand; and so on.
The DataAdapter classes also expose some methods used to work with the data, as
listed in Table 10-17.
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:20 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
10
PART II
Merge Merges two DataSet objects, commonly used in middle-tier
applications to include client data changes into the database
The following code segment performs these steps using the SqlDataAdapter:
In the preceding example, the SelectCommand was created using a SELECT state-
ment; you can, however, bind the command object to a stored procedure. The following
code segment shows that technique:
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:20 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
10
The DataTable enforces all constraints defined for it during the Fill operation.
To streamline the operation, you should turn off constraint checking by calling the
BeginLoadData() method before the Fill call, and EndLoadData() after the
operation. To do so, the last line in the preceding example would be replaced with
the following:
To fill a DataSet from a DataAdapter using the commands of the adapter, use the
following code:
dsEmp.Employees.BeginLoadData();
sqlDa.Fill(dsEmp.Employees);
dsEmp.Employees.EndLoadData();
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:20 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
10
PART II
DataRowState.Modified The row has been modified since the last call to the
AcceptChanges() method.
DataRowState.Unchanged The row has not been modified since the last call to
the AcceptChanges() method.
Table 10-18 RowState Values
the data modifications. To accept all the changes you call the AcceptChanges()
method.
In order to access either the original or the current version of the data as it is main-
tained by the DataSet, you have access to the DataRowVersion.Current and
DataRowVersion.Original parameters. This allows you to compare and base deci-
sions on the way the data has changed.
Resolving Conflicts
Whenever we use disconnected DataSets, there is a potential for conflict when the
data in the DataSet is saved back to the data source. The conflicts are caused by the fact
that ADO.NET is using optimistic locking, meaning that the locks in the database are re-
leased as soon as the create operation of the DataSet is completed, letting other appli-
cations access and modify the data. You need to be aware that conflicts can (and will)
occur, and how to resolve them.
The DataSet, DataTable, and DataRow objects each have a HasErrors property
that you can use to determine the success of your data updates, and to find out if there
were any conflicts.
To resolve conflicts, you can adopt a couple of strategies: last one wins ensures that all
conflicts will be won by the client that performed the last modification, and a business
rule involves the client in the resolution of any conflicts. Conflict resolution must be de-
signed based on the requirements of the application—the business rule can be a compo-
nent that tries to resolve the conflict automatically, based on the rules defined for the
transaction, but in most cases the final arbiter will be an administrator or the user.
The following code segment shows a method for working with the HasErrors
property:
// try the update, check for any exceptions and deal with the
// conflicts that might be there
try
{
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:21 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
10
NOTE The DataAdapter will also be used in Chapters 22 and 25, which
deal with the Web and Windows Forms.
Summary
In this chapter, you were exposed to the core concepts of one of the largest topic areas to
be covered in the exam. Additional Web and Windows-based information will be pre-
sented in greater detail later in the book, but we’ve established our groundwork here.
Data is the root of all applications, and the ways you access data determines the suc-
cess of your application. You were exposed to the ADO.NET concepts and the object
models that make it possible to treat data in the same way irrespective of where it origi-
nated. The connection object encapsulates the data source to the point where we no lon-
ger need to know the specifics of the vendor’s implementation. The DataSet makes the
data available in a disconnected table, and it has moved the data architecture forward by
leaps and bounds.
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:21 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
10
Test Questions
PART II
1. What does the following SQL statement return, assuming that all tables and
column names are correct?
SELECT FirstName, StreetAddress
FROM Employees
JOIN AddressBook
ON Employees.EmpID = AddressBook.EmpID
A. Nothing, the JOIN syntax is wrong.
B. All the records from the Employees table, and only the matching ones from
the StreetAddress table.
C. All the records from the StreetAddress table, and only the matching records
from the Employees table.
D. Only the matching records from the two tables.
2. What is a transaction?
A. A banking term.
B. A concept used to describe a step in the business process.
C. A combination of DML steps that must succeed or the data is returned to its
initial state.
D. A combination of DDL steps that must succeed or the data is returned to its
initial state.
3. What object is used to encapsulate a data source?
A. XxxConnection
B. XxxCommand
C. XxxDataAdapter
D. DataSet
4. What object is used to encapsulate a rowset?
A. DataSet
B. DataAdapter
C. DataRowSet
D. DataTable
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:21 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
10
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:21 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
10
PART II
12. What combination of methods are used to improve the speed of the Fill()
method of the DataAdapter?
A. BeginFillData() and EndFillData()
B. StartFillData() and EndFillData()
C. BeginLoadData() and EndLoadData()
D. StartLoadData() and EndLoadData()
13. The following SQL INSERT statement fails. What is the most probable reason
for the failure?
INSERT INTO Employees VALUES (42,'Bob','Carol', 12)
A. Syntax error in the INSERT statement.
B. The columns in the Employees table are not in the indicated order
(int, char, char, int).
C. The Employees database does not have a default table defined.
D. The SELECT INTO permission is not set.
14. In the following code, what is the result of compilation?
using System;
using System.Data;
using System.Xml;
class question14
{
public static void Main()
{
DataSet dsNw = new DataSet();
string strCn = "data source=(local);user id=sa;" +
"initial catalog=northwind;";
SqlConnection cnNw = new SqlConnection(strCn);
string strSQL = "SELECT * FROM Employees";
SqlDataAdapter daNw = new SqlDataAdapter(strSQL, cnNw);
daNw.Fill(dsNw, "Employees");
XmlDataDocument doc = new XmlDataDocument(dsNw);
doc.Save(Console.Out);
}
}
A. No errors
B. One error
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:21 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
10
Test Answers
1. D. The syntax is correct so all the matching rows will be returned.
2. C. Transaction work with DML (INSERT, UPDATE, DELETE) statements.
3. A. The XxxConnection defines how the application will connect and
authenticate to the data source.
4. D. The DataTable represents a rowset.
5. B. The DataTable object uses HasError to indicate conflicts.
6. A. DiffGrams are XML documents.
7. C.
8. A. The SqlDbConnection object is used with Microsoft SQL Server 7.0
and higher.
9. A. The SqlDbConnection object is used with Microsoft SQL Server 7.0
and higher.
10. C. Transactions are focused on the connection.
11. D.
12. C.
13. B. INSERT statements must match the data types of the inserted columns.
14. D. The namespace System.Data.SqlClient is missing resulting in an error
on the definition of cnNw and dawn, as well as an error when dawn.Fill()
is called.
15. A. The lack of a WHERE clause empties the table.
P:\010Comp\All-in-1\443-6\ch10.vp
Monday, August 26, 2002 11:48:21 AM