Presentation on theme: "By Chris Pascucci and FLF"— Presentation transcript:
1 By Chris Pascucci and FLF Lecture 14 C newIntroduction to Databases - DB Processing: The Disconnected Model (Using DataAdapters, DataSets, and DataTables)By Chris Pascucci and FLF
2 The Disconnected Architecture – Data Sets ADO.NET provides classes that are used to create applications that utilize a database.You can create instances of these classes and use them in your application to manage and use database connections.To use these classes in the .NET Framework you must import the namespace System.Data that contains all these useful classes.When working in the disconnected mode the data retrieved from a database and utilized by your program is stored in a DataSet.The DataSet contains one or more DataTables.Data in the DataSet is independent of the database that was used to retrieve it.A data set can contain the result of multiple queriesThe connection to the database is typically closed after data is retrieved. The connection is opened again when it’s needed.
3 ADO.NET – The Data Adapter Manages the exchange of data between the DataSet and a database.It issues an SQL Command (Select, Insert, Update or Delete) that is stored in the Command object.The Command object uses a Connection object to connect to the database and retrieve or update the data.The data is transferred back to the DataAdapter, which then stores it in a DataSet that can be used by the application.Note carefully – everything we manipulate is an object
6 ADO.NET Explained (skip this slide) The DataSet Object Model:
7 ADO.NET Explained (skip) The DataSet Object Model:
8 ADO.NET ExplainedAn ADO.NET DataProvider connects to a data source such as SQL Server, Oracle, or an OLE DB data source, and provides a way to execute commands against that data source.The ADO.NET classes responsible for working directly with a database are provided by the NET DataProviders:The .NET Framework includes data providers for SQL Server, Oracle, OLE DB, ODBC, etc…The Data Providers included in the .NET Framework contain the same objects, although their names and some of their properties and methods are different.For example, the SQL Server version of a database connection is the SqlConnection, while the OLE DB version is an OleDbConnection.ODBC = Open Database Connectivity (Relational/Procedural/First Abstract Model)OLE = Object Linking and Embedding (OO Based – Microsoft)
9 Connection (Review 1)The connection component is used to establish a connection to a database and manage communications between the data source and your program.Use the appropriate connection class.OleDbConnection, SqlConnection, OracleConnection, …Example:
10 Connection (Review 2)The connection string is a simple string that is used to create a connection object.The connection string contains key-value pairsThe assignment operator (=) is used to separate the key and the value.A semi-colon (;) is used to separate each key-value pair.The connection string is different for each DataProvider.
12 DataAdapterExecutes commands against a database and manages the transfer of information from the database to the DataSet.Use the appropriate DataAdapter class.OleDbDataAdapter, SqlDataAdapter, OracleDataAdapter, …Example:Note similarity with ExecuteQuery in connected mode
13 Using a Command with a DataAdapter The command component is used to take an SQL command you provide, prepare it for transport through the connection object, and for processing in the DBMS.For our purposes, this is optional and can be easily replaced with a simple string.The command object is created by passing it a simple string with SQL statements, and the connection object.Example:
15 ADO.NET Programming Example: Dim strSQL As String Dim strConnection As String Dim objDataSet As New DataSet()Dim objAdapter As SqlDataAdapterDim objConnection As SqlConnectionstrConnection = "server=dwarf.cis.temple.edu;Database=4376nn;” & _ “User id=4376nn;Password=yourpassword“strSQL = "SELECT * FROM Product“objConnection = New SqlConnection(strConnection)objAdapter = New SqlDataAdapter(strSQL, objConnection)objAdapter.Fill(objDataSet)DataSet used to hold data retrieved from the database.SQL Server DataProviders for working with an SQL Server data source.Connection object used to establish a connection.DataAdapter object used to manage the flow of data from the data source to the DataSet.Fills the DataSet object with data retrieved from the database.
16 ADO.NETAll elements of a database, tables, rows (set of values that make up a record), columns (database fields), and individual cells (actual field values) are all represented as abstractions (classes) in .NET.Basically, every table, row, and column are represented as instances of these abstractions (classes) that are part of the ADO.NET support structure.
17 Disconnected vs. Connected The major advantage of the disconnected approach is that it improves system performanceUses less resources for maintaining connections when working with a data source.Work is done on local data: DataSets, DataTables, etcHowever, there is a disadvantage to this approach that occurs when two or more users try to update the same row of a table, which is called a concurrency problem.
18 Disconnected vs. Connected ADO.NET contains two different approaches to work with data in a database: Connected Data Architecture and the Disconnected Data Architecture.Connected Data Architecture:Represents the objects that insist on having an open connection available for them to work and interact with the data source.ADO.NET provides classes to communicate directly with a data source.Disconnected Data Architecture:Represents the objects that open and close a connection to the data source as needed.Your application works with data that was copied from the data source. Any changes made to the “copied” data in the Dataset will be later reconciled with the data source.Connected applications alone don't fulfill the demands of today’s distributed applications.Improved performance, more efficient and more complex than the connected approach.
19 Concurrency IssuesAn old time problem when dealing with shared resources such as memory, files, DBs etcOccurs when multiple users try to access the same resource such as a row in a database tableNot a problem if all users are reading the resourceBecomes a problem if one or more are writingLocking mechanisms need to be implemented to ensure the integrity of the resource
20 Disconnected vs. Connected Modes When using the disconnected mode of DB access concurrency problems occur because once data is retrieved by User A from the database, the connection is dropped. Now User B has can access the DBThere are two ways that ADO.NET can handle concurrency problems:Optimistic Concurrency – The program checks to see if there has been a change to the row since it was retrieved. If it has, the update or deletion will be refused and a concurrency exception will be thrown that your program must handle.“Last In Wins” – Since no checking is done, the row that was updated by the last user will overwrite changes made to the row by the previous user.Another way to avoid concurrency problems is to retrieve and update only one row at a time.
21 Steps to Working with ADO.NET (disconnected) First, a database connection is establishedThen the database is openedSQL commands are sent over the open connectionADO.NET builds an in-memory representation of the returned data from any SELECT command and changes are made to this representationWhen processing is complete, the database connection is closedIt is suggested that you use the Open and Close methods to explicitly open and close your connections. In this way, you keep your connections open only as long as needed to complete the required processing
22 Processing database elements All elements of a data base, tables (entities), rows (sets of values), columns (database fields), and individual cells (database values) are represented as abstractions in .NETIn other words, every table, row, and column are represented as instances of the abstractions (classes) that are part of the ADO.NET support structure
23 How Do the Pieces Fit Together? Study the code example from Chris Pascucci first (see Lecture Set 14 C Ex)Then study the code sample provided by your lab assistant (Jessica Clark) at the end of the Client-Server Word document (also look at the starter code for the Final Project, Part III – ref . HW #12)
24 An Underlying Framework – The Connection Keeping a mental picture of this process in your head is importantIt all begins with the connectionAlready visited this idea for files. Now we revisit it again for databasesRemember …In your programs, you manipulate objects using methods belonging to the class that defines the type of the objectThe class (an abstract data type) is an abstraction (model) of the object being manipulated
25 The Object Framework (drawing/files) Recall …We must have a class to model the entity to be manipulated (the surface to be drawn on)The class is said to define an “abstract data type”We can then create objects (instances of the class we just created)Next comes the tricky part …We now have to connect that object to the actual entity to be drawn on ORHave to connect a stream to a sequential file
26 The Object Framework 2For databases, the object is an instance of a class that provides an abstract view of the database including an abstraction of database tables, rows, and columnsAll of these database features are modeled using ADO.NET classesSo we program against elements of the ADO.NET classes without concern for the underlying structures
27 The Object Framework 3 – The Connection The actual, physical database to be manipulatedThe database abstraction (methods and data stores) that you program against(DB commands, internal datasets and data tables)The connection – via creation of a database connection object‘Example - Connection to a Microsoft Access DatabaseDim myConnectionObj As New OleDbConnection(ConnectString)‘Example - Connection to SQL Server DatabaseDim myConnectionSqlObj As New SqlConnection(SqlConnectString)
28 Remember … Why do this – what is this all about? The abstraction enables us to manipulate database objects in a uniform, consistent way, regardless of the DBMS (Access, MySQL, SQL Server, Oracle)The underlying DBMS is hidden under several layers of ADO.NET software which does all the work
29 Connection Strings – (disconnected mode) All we need to do is connect the program’s ADO.NET database object to the underlying database to be used‘SQL Server ConnectionDim SqlConnectString As String = _ "server=dwarf.cis.temple.edu;Database=fa06_c342132;” & _"User id=fa06_c342132;Password=jimbo“Dim myConnectionSql As New SqlConnection(SqlConnectString)‘Access ConnectionDim ConnectString As String = "provider=Microsoft.Jet.OLEDB.4.0;” _“Data Source=c:\users1\dl\Inventory.mdb“Dim myConnection As New OleDbConnection(ConnectString)Programmer layer – build commands (SELECT, INSERT, DELETE, and UPDATE) for execution against the databaseLogical layer – uses a DataAdapter to send commands from your software (across the data adapter) to the database getting back tables to be processedPhysical layer – manages the actual (physical) execution of the commands on the database
31 ADO.NET Terms Summary (Review) DataSet:Data in the Dataset is independent of the database that was used to retrieve it.DataAdapter:Manages the exchange of data between the Dataset and a database.Allowing a DataSet and DataTable to be filled from the data source and later reconciled with the data source.Connection:Connects to the data source.Command:Executes commands (SQL statements) against the data source.Data Providers:The ADO.NET classes responsible for working directly with a specific database.Data ReaderRetrieves query results in a read-only, forward-only connected result set.