Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "ASP.NET - accessing the database - datareader vs dataset - datagrid vs datalist - brief look at web matrix."— Presentation transcript:

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

2 Accessing the database 1.Open a connection to a database 2.Create a recordset to store retrieved data 3.Send SQL query to database 4.Store answer to SQL query in recordset 5.Close connection to database 6.Retrieve data from recordset fields to display onscreen 7.Close recordset

3 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”)

4 Open connection to database (2) Give the string containing driver name & database location to the OleDbConnection Dim objConn as OleDbConnection objConn = New OleDbConnection(strConn) objConn.Open You could do both these steps on one line

5 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”

6 Send SQL query to database & store answer in recordset Dim strQuery as OleDbCommand strQuery = New OleDbCommand(querysql, objConn) objRS = strQuery.ExecuteReader Close connection to database objConn.Close

7 Retrieve data from recordset to display onscreen JobDataGrid.DataSource()=objRS JobDataGrid.DataBind() Close recordset strQuery.Dispose objRS.Close

8 DataReader vs DataSet

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

10 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

11 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

12 DataGrid vs DataList Extremely simplified summary: DataGrid – table DataList – List – You design the layout yourself

13 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

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

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

16 DataList templates Header template Item template – Displayed once per row from recordset – Can be set to repeat using or HTML tags Alternating Item Template Edit Item Template Selected Item Template Separator Template Footer Template

17 Initial View

18 Data Tab – click here to establish connection to database

19 DataGrid HTML tab - Shows HTML code only Code tab - Shows ASP.NET code only All code at once Drag & drop

20 basic query builders This menu only visible in code tab

21 Auto-generated SELECT function Links datagrid to function when page is loaded

22 TO DO Oasisplus – ASP.NET : Activities 1 & 2


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

Similar presentations


Ads by Google