Software Engineering I
Lecture 9
Software Engineering I Lecture 9 Reminder of what is left for this - - PowerPoint PPT Presentation
Software Engineering I Lecture 9 Reminder of what is left for this semester Today: Databases 11-23: Asynchronous programming 11-30: Web services 12-07: Middleware, filters, testing 12-14: UI 12-21: Design patterns with .net
Software Engineering I
Lecture 9
Reminder of what is left for this semester
.NET 5.0 is released!
us/dotnet/core/dotnet-five
Socrative – how are we doing?
Overview
– Overview – Working with database
– Connection – Command – DataSet, DataTable, and DataAdapter
Why?
(login), any specific data (books that are in library, people/cars that are missing, people that are in your social network)
today we’re going to explore it
Intro to MSSQL
– Procedural programming – Local variables – Functions for working with strings – Math functions – Changes to UPDATE and DELETE statements
Turing complete
can be written that will find an answer (although with no guarantees regarding runtime or memory).
– So, if somebody says "my new thing is Turing Complete" that means in principle (although often not in practice) it could be used to solve any computation problem.
can be written that will find an answer.
T-SQL few notes
Database creation
– Microsoft Access Database file – Microsoft ODBC Data source – Microsoft SQL server – Microsoft SQL Server Compact – Microsoft SQL Server Database file (good for tests) – Oracle Database – Other (live demo).
ADO.NET
allows to work with databases while using:
– DML commands: insert/update/select/delete. – DDL commands: create tables, functions, etc.
Objects
Connection
program and database.
(namespace: System.Data.SqlClient).
– Derives System.Data.Common. DBConnection.
Connection
has many methods (MSDN), but main ones:
Method Description Close Closes connection type obejct (disconnect) Open Opens Connection type object (connect)
Connection
Property Description ConnectionString Gets or sets the string used to open a connection to a database. Example: Server=myServerAddress; Database=myDataBase; User Id=myUsername; Password=myPassword More: http://www.ConnectionStrings.com ConnectionTimeout Gets the time in seconds that the system should wait while establishing a connection to the database before generating an error. Database Gets the name of the database DataSource Gets the name of the database server ServerVersion Gets the server version for the database State Gets a string that represents the state of the connection such as Open
Command
– ExecuteNonQuery – ExecuteReader – ExecuteScalar – ExecuteXmlReader
Command: ExecuteNonQuery
results.
Command: ExecuteNonQuery
Command: ExecuteNonQuery
Command: ExecuteReader
database (SELECT)
is a forward-only result collection, which remains connected to database while reader is open.
Command: ExecuteReader
Command: ExecuteScalar
a single value (a row).
Common rules
Connection.
Close()
connections might remain, which might be very painful.
DEMO – look at SqlDemo.rar
DataSet/DataTable
– Not constantly connected to DB. Meaning that it is possible to connect, load data, disconnect, modify, connect, update. – Data can be manipulated in more directions.
DataAdapter
– Load data to DataSet (meanig to DataTable(s)) – Perform CRUD operations
DataTable
query.
has Rows propery, which has collection
DataRow type objects. – Rows can be iterated or looked at through index. – Rows has property Count.
DataAdapter: Select
DataAdapter: Insert
DataAdapter: Update
DataAdapter: Delete
DataAdapter
– New values are added via column names – And then DataTable.Rows.Add() method is used.
– Look at what operations were performed – Call needed commands (in this case - InsertCommand)
DataAdapter
will execute corresponding commands, depending on what happened with DataTable object.
make sense.
DataGridView
– Via code: DataGridView.cs – Visual Studio: MSDN: How to: Display Related Data in a Windows Forms Application
When should you use ADO.NET?
Well, actually..
to manipulate data;
can be gained with ORM too, but it is difficult);
ORM
– SqlCommand, SqlDataReader, SqlDataAdapter and DataSet.
– Low (however, not too low) level work with data should be handled via ORM (Object-Relational Mapper).
type systems using object-oriented programming languages.
– ORM hides work with ADO.NET data providers. – Creates "virtual object database" that can be used from within the programming language.
ORM
– Instructor and Course tables – Many-to-Many saved in CourseInstructor table.
ORM way
– DBContext type object – Namespace: System.Data.Entity – Works with DB.
Some ORMs
– micro ORM such as Dapper or Massive, – Enterprise Library Data Access Application Block.
ORM advantages
– Entity Framework allows to migrate between databases without changing application code (-ish).
– However, maybe a bit slower.
– Migrations – Entities – Relations...
Tip of the day
Database first
– If file with name [fileName].dbml – Then DBContext will be [fileNameDataContext]
Database first
– Code is generated from DB, which sometimes is not OK. – If we create DB by ourselves as well, we do double job. – POCO (plain old CLD objects) classes can be changed only in partial classes or T4 files.
– Very OK, if DB is created by DBA or similar. – EDM (entity data model) wizard takes care about hierarchies and relationships – less error prone. – DB is open for changes – model can be updated afterwards.
T4 files
– Text Transformation Template Toolkit – Called "T4 template".
entity-related code.
Context: HashSet class
– Unique values – Insert returns false, if it failed. – Inner structure allows faster search (unique) – Set operations possible:
– Insert order not saved – To select value one iterates or uses Contains()
Model first
– On your own:
Model first
– Everything is aimed at point'n'click.
– Hard to control both – entity and DB code (updates are extra complicated).
– DB changes are not welcomed, because DB is updated according to model. – POCO classes changed through partial classes or T4 files only.
Code first
Lazy Loading.
Code first
– Most flexible – No magic which is hard to control (model and DB first) – OK to use with having DB as well – Powerfull migration mechanism
– No manual changes in DB.
DEMO - Look at EntityFramework.rar
Instructions for using code first with entity framework core
– Microsoft.EntityFrameworkCore – Microsoft.EntityFrameworkCore.Relational – Microsoft.EntityFrameworkCore.SqlServer – Microsoft.EntityFrameworkCore.Tools
– Must inherit from DbContext – Must have constructor without parameters, if you are using OnConfigruing method to configure database (no DI)
Instructions for using code first with entity framework core
create your tables
defined as default one and type command add-migraiton YourMigrationName
then run update-database in package manager console
Lazy vs Eager
When ORM speed is not enough
– A table that has many rows. – Update command needed for a single column (all values in it) – EF would do that for each line, SQL would have the option doing it at once.
When ORM speed is not enough
– EF determines, which fields have changed in the program (meaning which ones have to be updated in DB), when silently checking them with original ones (!!!)
– property AutoDetectChangesEnabled in DbContext.Configuration
Literature
– Chapter 9: Working with ADO.NET (DataSet/ DataTable/ DataAdapter)
– How to: Bind Data to the Windows Forms DataGridView Control – ASP.NET Data Access Options – Extra: How to: Display Related Data in a Windows Forms Application
– http://www.ConnectionStrings.com
Next time
– Thread – Task – Async/await – Real examples
Questions