Date post: | 16-Jan-2016 |
Category: |
Documents |
Upload: | magnus-garrett |
View: | 225 times |
Download: | 0 times |
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
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”)
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
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”
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
Retrieve data from recordset to display onscreen
JobDataGrid.DataSource()=objRSJobDataGrid.DataBind()
• Close recordset
strQuery.DisposeobjRS.Close
DataReader vs DataSet
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)
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
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
DataGrid vs DataList
• Extremely simplified summary:• DataGrid
– table
• DataList– List– You design the layout yourself
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
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)
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)
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
Initial View
Data Tab – click here to establish connection to database
DataGrid
HTML tab- Shows HTML code only
Code tab- Shows ASP.NET code only
All code at once
Drag & drop
Drag & dropbasic query buildersThis menu only visible in code tab
Auto-generated SELECT function
Links datagrid to function when page is loaded
TO DO
• Oasisplus– ASP.NET : Activities 1 & 2