Download ASP Beginning

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

DBase wikipedia , lookup

Commitment ordering wikipedia , lookup

Relational algebra wikipedia , lookup

Global serializability wikipedia , lookup

IMDb wikipedia , lookup

Serializability wikipedia , lookup

Oracle Database wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

SQL wikipedia , lookup

PL/SQL wikipedia , lookup

Microsoft Access wikipedia , lookup

Functional Database Model wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Ingres (database) wikipedia , lookup

Database wikipedia , lookup

Concurrency control wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

ContactPoint wikipedia , lookup

Clusterpoint wikipedia , lookup

Relational model wikipedia , lookup

Versant Object Database wikipedia , lookup

Database model wikipedia , lookup

Transcript
CSCI 6962:
Server-side Design and Programming
Database Manipulation
in ASP
Outline
•
•
•
•
•
•
•
Adding a database to an ASP project
The SqlDataSource object
Linking GridView components to a database
Binding queries to visual components
Reading query results
Programmatic database access
Optimistic concurrency
Access Database
• Simplest idea: Use Access database inside MS
database server built into Visual Studio
Adding a Database to a Project
• Add to App_Data subfolder of project
– Create if necessary
– Copy database into that folder
– Load into solution explorer using Add  Existing Item
SQLDataSource Object
• Control representing database connection
– Can set up as part of GridView (like ObjectDataSource)
– Can load separately from Data toolbox to create
connection without specific visual tool
GridViews and SQLDataSource
• Create GridView
• Choose new data source
• Choose SqlDataSource
GridViews and SQLDataSource
• In connection string
window choose
“new connection”
• Browse to the
database
• Set user name,
password if necessary
• Good idea to
test connection
Creating Connection
• Choose Microsoft Access DatabaseFile
Creating Connection
• Can browse for database file (easier if already in App_Data)
• Can set name/password for connection if password protected
Creating Connection
• Added to list of connection strings
• Can save in web.config for use by other pages
SqlDataSource Properties
• SqlDataSource control has properties
– SQL statements used for queries
– SQL statements used for updates (insert, delete, etc.)
– Can also set at any time in its properties
GridViews and Databases
• Table automatically populated from database
– Can edit columns to display in desired format
Forms and Databases
• Example:
Separate form to update price of widget in database
Forms and Databases
• Add SqlDataSource to page
– Drag from toolbox
– Configure as before
– If have existing connection in web.config, can choose it
from menu
Binding Queries to Controls
• Must use values entered in form elements to create
the query/update statement
– update Widget set price = priceBox.Text
where ID = IDBox.Text
• Modify specific query
type in properties menu
Binding Queries to Controls
• Enter query into Command and Parameter editor
– Table and field names in [ ]
– ? where field values will be inserted
Executing Updates
• Can execute update in code behind by calling method
– Might then transfer to another page
• Syntax: SqlDataSourceObject.TypeOfQuery()
– Type of query: Select(), Update(), Delete()
Reading Query Results
•
•
•
•
Will sometimes need code to manipulate results of query
Reading one line at a time
Determining whether any results exist
Example: Validating widget with given ID exists in database
– Add query to SQLDataSource object for SELECT
Reading Query Results
• Manually invoke Select method of SqlSource object
– Use Empty object as parameter to use query set in object
– Cast Enumerable object returned to some container object
(such as DataView)
Reading Query Results
• Good tool for accessing results: DataView object
–
–
–
–
DataViewObject.Table.Rows = rows of results
DataViewObject.Table.Rows.Count = number of results
DataViewObject.Table.Rows[i] = ith result
DataViewObject.Table.Rows[i][ field] = value in field field
in ith result
Programmatic Database Access
• May need to construct query dynamically
– Example: When user selects product, need to create SELECT
query from ID in CommandArguments
• Must set SelectCommand property of SqlDataSource to
desired SQL
– Often set up separate SqlDataSource
for this specific purpose
Programmatic Database Access
• Can retrieve results of query in terms of rows and
fields in those rows
– Extract Description and Price fields from row 0 of results
(first and only row), converting to proper types
– Pass to Widget constructor to add new Widget to cart
Optimistic Concurrency
• Multiple processes may attempt to simultaneously access
same record, causing concurrency problems
• ASP allows use of optimistic concurrency on updates
– Keeps track of field values at start of update
– Compares to field values at point update committed
– If not same, record has
been changed by another
process
– Do not do update if that
is the case