Presentation is loading. Please wait.

Presentation is loading. Please wait.

FEN 2014-05-011 Data connection DataReader DataSet Bonus info: Concurrency and Database Transactions Embedded SQL.

Similar presentations


Presentation on theme: "FEN 2014-05-011 Data connection DataReader DataSet Bonus info: Concurrency and Database Transactions Embedded SQL."— Presentation transcript:

1 FEN 2014-05-011 Data connection DataReader DataSet Bonus info: Concurrency and Database Transactions Embedded SQL

2 2 Architecture FEN 2014-05-01 DB DBMS DB Server We need something to make it fit together! SQL Management Tool Client SQL Table OO programme ?????

3 3 Architecture FEN 2014-05-01 DB DBMS DB Server DB Access Layer Client – OO programme SQL

4 FEN 2014-05-014 DBConnection Connection: Open Connection Execute DB operations Close Connection Working on actual (live) data Other applications can not access data.

5 FEN 2014-05-015 Overview of using databases 4 steps: 1.Open connection to database 2.Execute SQL for updating DB or fetching records (rows in the result table) 3.Handle data 4.Close connection

6 FEN 2014-05-016 Step 1: Open Connection Connection are opened according to connection string info here is a connection to a MS SQL Server database opened The database is located here: @"Data Source=NO122065\MSSQLSRV2012;” (my SQL Server) (‘@’ means that escape characters are discarded) Name of the database: Initial Catalog=MiniBankAK connection // Create and open a connection. SqlConnection cn = new SqlConnection(); cn.ConnectionString = @"Data Source=NO122065\MSSQLSRV2012;” +”Initial Catalog=MiniBankAK;Integrated Security=True"; cn.Open(); ShowConnectionStatus(cn); Connection See ado\ReaderExample01ado\ReaderExample01

7 FEN 2014-05-017 Connection Strings Connection strings are product specific (DB specific) and often very well-documented. Help may be found at: www.connectionstrings.com www.able-consulting.com/ADO_conn.htm If you are on the.NET platform using Visual Studio and MS SQL Server, VS can help. See ado\ReaderExample01ado\ReaderExample01

8 Short exercise: Change the connection string in ado\ReaderExample01, so that the example works on our PC. ado\ReaderExample01 FEN 2014-05-018

9 9 Two ways of DB access Connected: Open connection. Read/Write access (select, insert, update and delete) using a Command object. When reading (select) a DataReader object is returned. A DataReader is an iterator (cursor) into the result table. Close Connection. Disconnected: Fill a DataSet object (a copy of a part of the database) using a DataAdapter. DataAdapter wraps SQL-statement(s). A DataSet object contains DataTable objects. DataTable objects contain collections of rows and columns.

10 FEN 2014-05-0110 Data are fetched by creating a Command object and use it to execute a SQL statement. Data can be stored in either a DataReader object or a DataSet object. Using DataReader the result table lives server side, and you look at live data. Using DataSet the result table(s) is (are) copied to the client, and live data may be changed by other users. Step 2: Get / fetch data from the database

11 FEN 2014-05-0111 Connection vs. Connectionless Connection: Open Connection Execute DB operations Close Connection Working on actual (live) data Other applications can not access data.

12 FEN 2014-05-0112 Connection vs. Connectionless Connectionless: –Create a copy of a part of the database –Execute DB operations on the copy –Other applications may change date –The copy may be come inconsistent. Data are changed in the local copy: –at update it is checked if the data in the database have been modified by others –in that case the update is rejected (ConcurrencyException).

13 FEN 2014-05-0113 Differences between DataReader and DataSet/DataAdapter DataReader can only be used for reading data. It can only be traversed once (forward). DBCommand can update the database by ExecuteNonQuery. This update is executed immediately. DataAdapter is the connection between DataSet and database. Data are fetched to the DataSet, might be modified and sent back to the database. Updates are executed on a local copy. Concurrency problems must be handled. Possible to traverse forward and backward. A DataSet can contain multiple tables. We will focus on DataReader.

14 FEN 2014-05-0114 Step 2: Get records using DataReader Get records via SQL Select query read-only access to the database string strSQL = "SELECT * FROM Customer"; SqlCommand myCommand = new SqlCommand(strSQL, cn); SqlDataReader myDataReader; myDataReader = myCommand.ExecuteReader(); data reader record

15 FEN 2014-05-0115 What is achieved so far? We have created a connection to a database. The connection is placed in the connection object. We have done a search by using a SQL-statement. The search was executed by using a command object. The result of the search was stored in a DataSet or as here a DataReader object. Now it is possible to get the data from this object for viewing, passing on to client or handle in other ways.

16 FEN 2014-05-0116 Step 3: Getting the result and handle data Loop through the result table row by row. For each row: get the attribute value: SqlDataReader myDataReader; myDataReader = myCommand.ExecuteReader(); // Loop over the results. while (myDataReader.Read())//while(more rows) { Console.WriteLine("Custno: {0}, Name: {1} ", myDataReader.GetInt32(0), myDataReader.GetString(1).Trim()); } data reader record

17 FEN 2014-05-0117 This is very flexible, one need to the details of the result table. This is more flexible: // Loop over the results. while (myDataReader.Read())//while(more rows) { //Display each attribute in current row for (int i = 0; i < myDataReader.FieldCount; i++) { Console.Write("{0} = {1} ", myDataReader.GetName(i), myDataReader.GetValue(i).ToString().Trim()); } Console.WriteLine(); } See ado\ReaderExample02ado\ReaderExample02 Step 3: Getting the result and handle data

18 FEN 2014-05-0118 myDataReader.Close(); cn.Close(); Step 4: Close the connection DataReaders work on live data, locking other user out, so remember: Close DataReader and connection when you are done:

19 Short exercise: Change the connection string in ado\ReaderExample02, so that the example works on our PC. ado\ReaderExample02 Change the example (connections string and SQL statement), so the VW-database is accessed and this SQL query (query 4) is executed: FEN 2014-05-0119 select distinct model from Car, spc where Car.cNo = spc.cNo and sNo='s1'

20 FEN 2014-05-0120 public class Customer { private int custNo; private string name; private List accounts; public Customer(int cNo, string n) { this.custNo= cNo; this.name= n; accounts = new List (); } //--- Building objects You can do more than just printing the result. For instance, assume that you have this class:

21 FEN 2014-05-0121 SqlDataReader myDataReader; myDataReader = myCommand.ExecuteReader(); List customers = new List (); // Loop over the results. // Create objects and add them to a list while (myDataReader.Read())//while(more rows) { int custNo = myDataReader.GetInt32(0); string custName = myDataReader.GetString(1).Trim(); Customer c = new Customer(custNo, custName); customers.Add(c); } Building objects Then objects may build from the database: See ado\ReaderExample03ado\ReaderExample03

22 FEN 2014-05-0122 Console.WriteLine(); Console.Write("Close connections?"); Console.ReadLine(); Console.WriteLine(); myDataReader.Close(); cn.Close(); Console.WriteLine(); Console.Write("Show customer list?"); Console.ReadLine(); foreach(Customer c in customers) Console.WriteLine(c); Console.WriteLine(); Building objects Then the connection may be closed, and you can use the objects in your application: DB Connection is closed Objects are used as POCOs (”Plain Old C# Objects”) Objects are used as POCOs (”Plain Old C# Objects”) See ado\ReaderExample03ado\ReaderExample03

23 FEN 2014-05-0123 //get new value Console.Write("Enter customer number: "); int custNo = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter new customer name: "); string custName = Console.ReadLine(); cn.Open(); //Build SQL statement: strSQL = "UPDATE Customer SET name = '" + custName + "' WHERE custNo = " + custNo; myCommand = new SqlCommand(strSQL, cn); myCommand.ExecuteNonQuery(); cn.Close(); Updating the database Update (or insert or delete): ExecuteNonQuery See ado\UpdatingReaderado\UpdatingReader

24 FEN 2014-05-0124 Bonus info: Database Transactions A database normally has many users at the same time (concurrency): My bank account: I am at my wine merchant trying to buy 12 bottles of Chambertin using my card. At the same time my wife using home banking is trying to move my money to our savings account. Who wins? Many database operations requires more than SQL statement to be executed: Transferring an amount from my account to our savings account: the amount must be withdrawn from my account (one SQL update) and inserted to our savings account (another SQL update). We want both update to complete (or non), but nor just one of the updates. This is known as an transaction: Both updates or non!

25 FEN 2014-05-0125 Bonus info: Database Transactions ACID: Atomic Consistent Isolation Durability Problems: lost update “dirty read”, uncommitted dependency, temporary update inconsistent analysis (incorrect summary) Synchronization is needed

26 FEN 2014-05-0126 Bonus info: Database Transactions T1:Transfers N DKKs from account X to account Y: read_item(X); X:= X-N; write_item(X); read_item(Y); Y:= Y+N; write_item(Y); T2:Deposits M DKK on account Y: read_item(Y); Y:= Y+M; write_item(Y); Any possible problems? time

27 FEN 2014-05-0127 Bonus info: Lost Update

28 FEN 2014-05-0128 Bonus info: Dirty Read (Uncommitted dependency)

29 FEN 2014-05-0129 Bonus info: Inconsistent Analysis

30 FEN 2014-05-0130 Database Transactions: C#/ADO.NET/MS SQL Server ado\BankTransactions


Download ppt "FEN 2014-05-011 Data connection DataReader DataSet Bonus info: Concurrency and Database Transactions Embedded SQL."

Similar presentations


Ads by Google