Presentation is loading. Please wait.

Presentation is loading. Please wait.

Overview of Data Access

Similar presentations


Presentation on theme: "Overview of Data Access"— Presentation transcript:

1 Overview of Data Access
MIS Professor Sandvig 7/5/2018 Overview of Data Access MIS 324 Professor Sandvig

2 MIS 324 -- Professor Sandvig
7/5/2018 Overview ADO.NET overview Data Access Displaying data

3 MIS 324 -- Professor Sandvig
7/5/2018 ADO.NET Overview “Any web application worth writing involves data access.” Stephen Walther ADO ActiveX Data Objects Microsoft’s data access technologies Set of classes that provide data access services Class Library System.Data

4 MIS 324 -- Professor Sandvig
7/5/2018 ADO.NET Overview Support for most relational databases Sql Server Oracle MySql Any ODBC compliant database Open Database Connectivity standard

5 MIS 324 -- Professor Sandvig
7/5/2018 .NET Data options Programmatically Use connection & command objects Pass SQL to database Very flexible approach Similar to PHP Entity Framework VS examines database Creates classes representing database structure Query with LINQ (language integrated query) Intellisense support Added to .NET 3.5 Proprietary Microsoft product

6 MIS 324 -- Professor Sandvig
7/5/2018 Retrieving Data MIS 324 Will use programmatic approach Benefits Flexible Uses SQL Standard OOP programming techniques MIS 424 Entity Framework Fast & convenient

7 MVC Approach Separation of duties: Model DataRepository class
Describes application data DataRepository class Sql statements and data access classes Controller Handles requests View Displays data and forms

8 Repository All data access logic Goal: Loose coupling Sql statements
Database classes Connection Etc. Goal: Loose coupling Different data source affects only repository

9 ORM Will use Object Relational Mapper Maps model to sql parameters
Query results to model

10 ORM Dapper ORM Developed by StackOverflow For its site “King of ORMs”

11 ORM //Get one employee with Dapper
public CrudModel GetOneEmployee(int id) { using (IDbConnection db = connection) string sql = "select Id, Fname, Lname from EmployeeDetails where id CrudModel employee = db.Query<CrudModel>(sql, new { id }).SingleOrDefault(); return employee; } //Get one employee without Dapper public CrudModel GetOneEmployeeNoDapper(int id) { //each sql parameter must be added as a parameter //each return value must be assigned to data object string sql = "select Id, Fname, Lname from EmployeeDetails where id SqlDataReader reader; using (SqlCommand command = new SqlCommand(sql)) //add input parameters. SqlParameter parm = new id); command.Parameters.Add(parm); //execute reader = command.ExecuteReader(); } //create data object and assign values from reader CrudModel employee = new CrudModel(); while (reader.Read()) employee.Id = Convert.ToInt32(reader["id"]); employee.Fname = Convert.ToString(reader["Fname"]); employee.Lname = Convert.ToString(reader["Lname"]); }; return employee;

12 Retrieving Data .NET MVC
MIS Professor Sandvig 7/5/2018 Retrieving Data .NET MVC Create: Model Database (Sql Server) DataRepository Controller Views

13 MIS 324 -- Professor Sandvig
7/5/2018 Example – CRUD

14 Crud Example Controller contains 7 action methods Need 5 views

15 CRUD example Example (see handout for source)

16 MIS 324 -- Professor Sandvig
7/5/2018 Summary ADO.NET Classes that handle data access Provides consistent programming interface to data sources MVC Database access handled in DataRepository Controller calls repository methods Next: SQL Server Express Creating databases & using with ASP.NET


Download ppt "Overview of Data Access"

Similar presentations


Ads by Google