23
CSCI 6962: Server-side Design and Programming Database Manipulation in ASP

CSCI 6962: Server-side Design and Programming Database Manipulation in ASP

Embed Size (px)

Citation preview

Page 1: CSCI 6962: Server-side Design and Programming Database Manipulation in ASP

CSCI 6962: Server-side Design and Programming

Database Manipulation in ASP

Page 2: 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

Page 3: CSCI 6962: Server-side Design and Programming Database Manipulation in ASP

Access Database

• Simplest idea: Use Access database inside MS database server built into Visual Studio

Page 4: CSCI 6962: Server-side Design and Programming Database Manipulation in ASP

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

Page 5: CSCI 6962: Server-side Design and Programming Database Manipulation in ASP

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

Page 6: CSCI 6962: Server-side Design and Programming Database Manipulation in ASP

GridViews and SQLDataSource

• Create GridView• Choose new data source• Choose SqlDataSource

Page 7: CSCI 6962: Server-side Design and Programming Database Manipulation in ASP

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

Page 8: CSCI 6962: Server-side Design and Programming Database Manipulation in ASP

Creating Connection

• Choose Microsoft Access DatabaseFile

Page 9: CSCI 6962: Server-side Design and Programming Database Manipulation in ASP

Creating Connection

• Can browse for database file (easier if already in App_Data)• Can set name/password for connection if password protected

Page 10: CSCI 6962: Server-side Design and Programming Database Manipulation in ASP

Creating Connection

• Added to list of connection strings• Can save in web.config for use by other pages

Page 11: CSCI 6962: Server-side Design and Programming Database Manipulation in ASP

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

Page 12: CSCI 6962: Server-side Design and Programming Database Manipulation in ASP

GridViews and Databases

• Table automatically populated from database– Can edit columns to display in desired format

Page 13: CSCI 6962: Server-side Design and Programming Database Manipulation in ASP

Forms and Databases

• Example:Separate form to update price of widget in database

Page 14: CSCI 6962: Server-side Design and Programming Database Manipulation in ASP

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

Page 15: CSCI 6962: Server-side Design and Programming Database Manipulation in ASP

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

Page 16: CSCI 6962: Server-side Design and Programming Database Manipulation in ASP

Binding Queries to Controls

• Enter query into Command and Parameter editor– Table and field names in [ ]– ? where field values will be inserted

Page 17: CSCI 6962: Server-side Design and Programming Database Manipulation in ASP

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()

Page 18: CSCI 6962: Server-side Design and Programming Database Manipulation in ASP

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

Page 19: CSCI 6962: Server-side Design and Programming Database Manipulation in ASP

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)

Page 20: CSCI 6962: Server-side Design and Programming Database Manipulation in ASP

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

Page 21: CSCI 6962: Server-side Design and Programming Database Manipulation in ASP

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

Page 22: CSCI 6962: Server-side Design and Programming Database Manipulation in ASP

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

Page 23: CSCI 6962: Server-side Design and Programming Database Manipulation in ASP

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