asp.net - accessing the database - datareader vs dataset - datagrid vs datalist - brief look at web...

22
ASP.NET - accessing the database - datareader vs dataset - datagrid vs datalist - brief look at web matrix

Upload: magnus-garrett

Post on 16-Jan-2016

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ASP.NET - accessing the database - datareader vs dataset - datagrid vs datalist - brief look at web matrix

ASP.NET- accessing the database- datareader vs dataset

- datagrid vs datalist- brief look at web matrix

Page 2: ASP.NET - accessing the database - datareader vs dataset - datagrid vs datalist - brief look at web matrix

Accessing the database

1. Open a connection to a database2. Create a recordset to store retrieved data3. Send SQL query to database4. Store answer to SQL query in recordset5. Close connection to database6. Retrieve data from recordset fields to

display onscreen7. Close recordset

Page 3: ASP.NET - accessing the database - datareader vs dataset - datagrid vs datalist - brief look at web matrix

Open connection to database (1)

• Work out which connection driver you need

Dim strConn as String

strConn = “Provider=Microsoft.Jet.OLEDB.4.0;Data Source = “ & Server.MapPath(“recruitment.mdb”)

Page 4: ASP.NET - accessing the database - datareader vs dataset - datagrid vs datalist - brief look at web matrix

Open connection to database (2)

• Give the string containing driver name & database location to the OleDbConnection

Dim objConn as OleDbConnectionobjConn = New OleDbConnection(strConn)objConn.Open

• You could do both these steps on one line

Page 5: ASP.NET - accessing the database - datareader vs dataset - datagrid vs datalist - brief look at web matrix

Create a recordset to store retrieved data

Dim objRS as OleDbDataReader

• Store SQL query in a String

Dim querysql as String

querysql = “SELECT * FROM JobList”

Page 6: ASP.NET - accessing the database - datareader vs dataset - datagrid vs datalist - brief look at web matrix

Send SQL query to database &store answer in recordset

Dim strQuery as OleDbCommandstrQuery = New OleDbCommand(querysql,

objConn)

objRS = strQuery.ExecuteReader

• Close connection to databaseobjConn.Close

Page 7: ASP.NET - accessing the database - datareader vs dataset - datagrid vs datalist - brief look at web matrix

Retrieve data from recordset to display onscreen

JobDataGrid.DataSource()=objRSJobDataGrid.DataBind()

• Close recordset

strQuery.DisposeobjRS.Close

Page 8: ASP.NET - accessing the database - datareader vs dataset - datagrid vs datalist - brief look at web matrix

DataReader vs DataSet

Page 9: ASP.NET - accessing the database - datareader vs dataset - datagrid vs datalist - brief look at web matrix

DataReader vs DataSet

• Extremely simplified summary:• DataReader

– Conveyer belt of data from database to .NET application (bridge)

• DataSet– Miniature in-memory copy of the database

(usually just part of it)

Page 10: ASP.NET - accessing the database - datareader vs dataset - datagrid vs datalist - brief look at web matrix

DataReader

• Remains connected to the database– Only contains one row at a time– Can only go forwards– Can’t edit the data directly

• Different types for each provider– Microsoft uses OleDbConnection– SQL uses SQLConnection

• 30 (ish) times faster than a dataset • Can be looped so all data is retrieved and stored in a

table in memory

Page 11: ASP.NET - accessing the database - datareader vs dataset - datagrid vs datalist - brief look at web matrix

DataSet

• Disconnected– Retrieves all info in one go & stores in memory

• Editable• Provider neutral

– can also be used to access XML files

• Can be sorted or searched• Very good for basic data-entry applications• Useful for caching information to be shown on

several pages across your website

Page 12: ASP.NET - accessing the database - datareader vs dataset - datagrid vs datalist - brief look at web matrix

DataGrid vs DataList

• Extremely simplified summary:• DataGrid

– table

• DataList– List– You design the layout yourself

Page 13: ASP.NET - accessing the database - datareader vs dataset - datagrid vs datalist - brief look at web matrix

DataGrid

• Most versatile, but least flexible• Built-in paging, sorting & editing facilities• Each row displays 1 record• Can be nested• Slowest to display on screen

Page 14: ASP.NET - accessing the database - datareader vs dataset - datagrid vs datalist - brief look at web matrix

DataGrid column types• Bound Column

– Value from datasource as plain text

• Button Column• Edit Column• HyperLink Column

– Hyperlink, text & url may come from datasource

• Template Column– Customise html output of the column (allows

nested grids)

Page 15: ASP.NET - accessing the database - datareader vs dataset - datagrid vs datalist - brief look at web matrix

DataList

• No built-in support for paging, sorting & editing

• Can display more than 1 record on each row• Much more customisation of data layout is

possible• Can be nested (not as easy as datagrid)

Page 16: ASP.NET - accessing the database - datareader vs dataset - datagrid vs datalist - brief look at web matrix

DataList templates

• Header template• Item template

– Displayed once per row from recordset– Can be set to repeat using <table> or <span> HTML tags

• Alternating Item Template• Edit Item Template• Selected Item Template• Separator Template• Footer Template

Page 17: ASP.NET - accessing the database - datareader vs dataset - datagrid vs datalist - brief look at web matrix

Initial View

Page 18: ASP.NET - accessing the database - datareader vs dataset - datagrid vs datalist - brief look at web matrix

Data Tab – click here to establish connection to database

Page 19: ASP.NET - accessing the database - datareader vs dataset - datagrid vs datalist - brief look at web matrix

DataGrid

HTML tab- Shows HTML code only

Code tab- Shows ASP.NET code only

All code at once

Drag & drop

Page 20: ASP.NET - accessing the database - datareader vs dataset - datagrid vs datalist - brief look at web matrix

Drag & dropbasic query buildersThis menu only visible in code tab

Page 21: ASP.NET - accessing the database - datareader vs dataset - datagrid vs datalist - brief look at web matrix

Auto-generated SELECT function

Links datagrid to function when page is loaded

Page 22: ASP.NET - accessing the database - datareader vs dataset - datagrid vs datalist - brief look at web matrix

TO DO

• Oasisplus– ASP.NET : Activities 1 & 2