2314 – Programming Language Concepts Introduction to ADO.NET
Overview of Database Terminology Fields = attributes –Each field has a type –Some optional, some required Rows = instances (record) Common operations –INSERT –DELETE –UPDATE
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
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
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 S ='" & strUser & "' and SPASS='" & strPass & "'" set objRS=objConn.Execute(strSQL) %>
Sample ASP Code (cont) Username or Password is not correct or not found Click here to try again Navigate("booklist.asp")
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 %>
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(); }
Adding the DataGrid Object to HTML Utilize the DataGrid System control: Authors Information is current as of
Important ASP.NET Classes/Methods SqlConnection / OleDbConnection SqlDataAdapter / OleDbDataAdapter –Fill OleDbDataReader & OleDbCommand DataSet DataGrid –DataSource –DataBind –Columns, BoundColumn, HyperLinkColumn
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
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”
DataAdapter Creates a link to the DB connection Passes the SQL to the DB connection Manages the DB connection - internally DB DataAdapter ASP.NET
DataTable Columns = fields (names & data types) Rows = records Local, in-memory copy of DB records Can move forward, backward through data
DataSet Collection of DataTables In-memory object DataTable 1DataTable 2DataTable 3 DataSet 1
Binding & Filling DataTables Connect to DB Configure DataAdapter (SQL statement) Execute DataAdapter’s SQL and fill a DataTable w/ the results
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;
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);