Download presentation
Presentation is loading. Please wait.
Published byGeraldine McGee Modified over 9 years ago
1
2314 – Programming Language Concepts Introduction to ADO.NET
2
Overview of Database Terminology Fields = attributes –Each field has a type –Some optional, some required Rows = instances (record) Common operations –INSERT –DELETE –UPDATE
3
Multi-Tiered Client-Server Model "Thin" client Code/work on server Ultimate in portability Makes upgrades easier Connectivity necessary Scalability important Client Server TCP/IP DB1 DB2 Data Services
4
Server-Side Processing Server-Backend Communication Server analyzes client request Realizes it needs the backend Server requests a DB transaction with backend Backend formulates response and sends to server Server formats information into HTML Server sends information to client
5
Sample ASP Code Verifying User Informaiton <% dim objConn, objRS, strSQL, strUser, strPass, strBID, strBook set objConn = Server.CreateObject("ADODB.Connection") objConn.Open "ITLibrary", "user", "nothing" strUser = Request.Form("User") strPass = Request.Form("Pass") strSQL="SELECT SID FROM Student WHERE SEMAIL='" & strUser & "' and SPASS='" & strPass & "'" set objRS=objConn.Execute(strSQL) %>
6
Sample ASP Code (cont) Username or Password is not correct or not found Click here to try again Navigate("booklist.asp")
7
Sample ASP DB Table First Name Last Name Phone <% do until rs.EOF Response.Write " " Response.Write " " & rs("au_fname") & " " Response.Write " " & rs("au_lname") & " " Response.Write " " & rs("phone") & " " Response.Write " " rs.MoveNext loop %>
8
Sample ASP.NET DB Table void Page_Load(Object Src, EventArgs E) { DataSet DS; SQLConnection MyConnection; SQLDataAdapter MyCommand; MyConnection = new SQLConnection("server=localhost;uid=sa;pwd=;database=pubs"); MyCommand = new SQLDataAdapter("select au_fname as 'First Name', au_lname as 'Last Name', Phone from Authors", MyConnection); DS = new DataSet(); MyCommand.Fill(ds, "Authors"); grdAuthors.DataSource=ds.Tables["Authors"].DefaultView; grdAuthors.DataBind(); }
9
Adding the DataGrid Object to HTML Utilize the DataGrid System control: Authors Information is current as of
10
Important ASP.NET Classes/Methods SqlConnection / OleDbConnection SqlDataAdapter / OleDbDataAdapter –Fill OleDbDataReader & OleDbCommand DataSet DataGrid –DataSource –DataBind –Columns, BoundColumn, HyperLinkColumn
11
C# Data Binding OLE vs. SQL –Just different means to connect –SQL for SQL Server –OLE for all other DBs –We’ll use OLE to connect to an Access DB In Studio.NET, use the “Data” panel of the Toolbox to access the DB controls In WebMatrix, drag/drop table onto form
12
How the Connection Works Three key non-visual controls (for OLE): –OleDbDataAdapter –OleDbConnection –DataSet DB Connection Data Adapter DataSet Visual Controls The user only cares about (and only sees) these Behind the scenes “overhead”
13
DataAdapter Creates a link to the DB connection Passes the SQL to the DB connection Manages the DB connection - internally DB DataAdapter ASP.NET
14
DataTable Columns = fields (names & data types) Rows = records Local, in-memory copy of DB records Can move forward, backward through data
15
DataSet Collection of DataTables In-memory object DataTable 1DataTable 2DataTable 3 DataSet 1
16
Binding & Filling DataTables Connect to DB Configure DataAdapter (SQL statement) Execute DataAdapter’s SQL and fill a DataTable w/ the results
17
ASP.NET Example OleDbDataAdapter dap; OleDbConnection conn; DataSet dset; dap = new OleDbDataAdapter(“select * from members order by memberid”, conn); dset = new DataSet(); dap.Fill(dset, “Members”); grid.DataSource = dset.Tables(“Members”); grid.DataBind;
18
Accessing & Updating Accessing items/fields is quite easy: dset.Tables(TABLE).Rows(N).Item(X); DataSets & DataTables are LOCAL copies of the DB – so changes are not persistent! Updating the DB is easy too: dap.Update(dset, TABLE);
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.