Download presentation
Presentation is loading. Please wait.
Published byEmory Randall Modified over 9 years ago
1
CSCI 6962: Server-side Design and Programming Database Manipulation in ASP
2
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
3
Access Database Simplest idea: Use Access database inside MS database server built into Visual Studio
4
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
5
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
6
GridViews and SQLDataSource Create GridView Choose new data source Choose SqlDataSource
7
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
8
Creating Connection Choose Microsoft Access DatabaseFile
9
Creating Connection Can browse for database file (easier if already in App_Data) Can set name/password for connection if password protected
10
Creating Connection Added to list of connection strings Can save in web.config for use by other pages
11
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
12
GridViews and Databases Table automatically populated from database – Can edit columns to display in desired format
13
Forms and Databases Example: Separate form to update price of widget in database
14
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
15
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
16
Binding Queries to Controls Enter query into Command and Parameter editor – Table and field names in [ ] – ? where field values will be inserted
17
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()
18
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
19
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)
20
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] = i th result – DataViewObject.Table.Rows[i][ field] = value in field field in i th result
21
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
22
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
23
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.