Transcript SqlCommand
Introduction to
Database Processing
with ADO.NET
The ASP.NET / ADO.NET
Conundrum
The tools and wizards are wonderful for
simple applications
As applications become more complex and
“real”
You need to write code between the controls
and the data providers
Nested controls and one-to-many views become
complex
At some point, the visual designers become
useless
Lecture Overview
Introduce ASP.NET data sources
Introduce the ASP.NET data bound controls
Show a few SIMPLE examples to get us
started
History of ADO
Open Database Connectivity (ODBC) was
created to provide a uniform API to access
SQL database servers
OLE DB evolved from ODBC and provided a
COM based interface
Next came the first versions of ADO, which
encapsulated OLE DB
ADO.NET is the current generation of ADO
and uses .NET Managed Providers
History of ADO:
While not a problem, Microsoft has stepped
away from all MSAccess support
The data controls are gone
Necessary Jet versions are not usually installed
You can make them work though
So we will use SQL Server Express in this
course
I’ll show how to connect to SQL Server
.NET and SQL Server
Use the Server Explorer to manage
connections
Table Designer
Use it to create tables and manage schema
elements
Three views
GUI editor
Script
Summary
Table
Designer
ADO.NET -The Role of
Managed Providers
Managed providers interact with a data
source and return data to an application
Data sources are typically (almost always)
disconnected
After data is returned to the application,
connections are closed
ADO / ASP.NET NOTE
The capabilities of ASP and ADO are vast
There are many ways to accomplish the same
task
DataReaders vs. DataAdapters
DataSource controls vs. manual binding
ADO.NET Namespaces /
Classes (Intro)
There are many of these
Some you don’t need to work with directly
Mastery of the topic requires that you
understand what the controls do and how
they interact with the underlying classes
ADO.NET Namespaces (1)
Here’s the fabric
System.Data
System.Data.SqlClient
System.Data.OleDb
System.Data.SqlTypes
ADO.NET Classes (1)
Connection – creates the connection with the data
source (database)
Support is provided for ORACLE, SQL Server,
Access, etc.
Transaction – allows for transactional processing
Command – typically an SQL statement executed
against a database server
Could be a stored procedure
Parameter – a parameter passed to a command
ADO.NET Classes (2)
DataAdapter – a database object that
returns a disconnected set of records
(DataSet and DataTable)
DataReader – a forward-only reader to get
database records
Introduction to Connections
Executing a database command is generally a twostep process
Create a connection
Prepare and execute a command
The ASP.NET controls help automate much of the
process
There are specific connection classes for OLE DB,
SQL Server and others
They all work the same way
Connection Strings
There are tools to build these for you based
on the database provider
Connection strings are typically stored in the
web.config file
They store all information needed to establish
a database connection
More later
Connection Strings (Example)
Connection strings are usually stored in
web.config
The following reads the connection string
from web.config
Connection Strings (Example)
The SqlConnection object represents the
connection itself. Its properties depict the
connection status
Executing Commands
(Introduction)
Use the SqlCommand object to
Return one record one field
(ExecuteNonQuery)
Execute SQL INSERT, UPDATE, DELETE
statements
Return records in the form of a DataTable
Return records one row at a time
(DataReader)
SqlCommand Object
(Using 1 row)
The OleDbCommand or SqlCommand objects
work the same way
Set the CommandText property to the SQL
statement to execute
Set the CommandType property to
CommandType.Text
Set the Connection property to an existing
Connection object
SqlCommand Object
(Using 1 row)
To return one field from one row, call
ExecutScalar() on the SqlCommand
object
SqlCommand Object
(Returning Nothing)
Use the ExecuteNonQuery method to
execute SQL statements that do not return
values
INSERT / UPDATE / DELETE
As before, the CommandText property stores
the SQL statement
Two ways to do INSERT / UPDATE /DELETE
With parameters
Roll the statement by hand
SqlCommand Object
(Returning Nothing - Manual)
Use create the CommandText,
CommandType and Connection as before
The INSERT / UPDATE / DELETE statements
get contorted
SqlCommand Object
(Parameters 1 )
Parameters provide the preferred way to pass
data to INSERT / UPDATE / DELETE and
other SQL statements
Parameters begin with an @ in the SQL
statement
SqlCommand Object
(Parameters 1 )
Then we create the parameters as part of the
command object
We are adding items to the parameters
collection
Using the DataReader
This is straight out of the book
It’s a forward only reader
There are much better ways to read data but
here is the first cut
Call Read to read the first and subsequent
records
Create an SqlCommand object as before
Call ExecuteReader to initialize
Call Read to read each record
Index contains the field name
Using the DataReader
(example)
Reading Multiple Records
Here, we use the DataSet and DataTable
objects along with the DataAdapter
Steps
Create SqlCommand with the SELECT statement
as before
Create a new SqlDataAdapter
Create a DataSet
Call Fill on the DataAdapter to populate the
DataSet
Reading Multiple Records
Introduction to Data Sources
Data sources establish the connection between an
ASP.NET application and a database
These are not the same controls as the desktop
controls
Data Source Controls
(Categories)
Tabular
SqlDataSource – For SQL databases
ObjectDataSource – For custom business
objects
Hierarchical
SiteMapDataSource – For menus
XmlDataSource – To bind XML documents
Data Sources (Core Concepts)
The ASP.NET data source controls are bound to a
page rather than the application
They belong to System.Web rather than
System.Data so they are again unique to ASP.NET
Underneath the hood, there is a DataSet
You can use the System.Data classes from an
ASP.NET application
Visual Studio does a great job configuring the data
source controls
Data Sources
(Under the Hood 1)
The Wizards configure properties to
Write SELECT statements and customize them
Write INSERT, UPDATE, and DELETE
statements that are executed by other bound
controls
Data Sources
(Under the
Hood 2)
The
Properties
window tells the
tale