Our Study• Attendance 5% • But not come to loud• Mid-term 15% • Theory and simply code • You can do it if you understand software and programming • Can open the book, laptop, internet, … to answer • DO NOT ask other• Assignment + Homework 20% (4 per group)• Semester Exam 60%
Introduce Yourself &What did you knowabout Programmingand Database ?
This Course• Purpose: You will • Understand the more detail of C# with Database • Have ability to develop .NET applications with Visual Studio with SQL Server Database• Requirement • Logic • Try bit harder • Understanding of C# • Database
This Course (2)• The first step to be a programmer with .NET Technology• Can be • Software Developer • Web Developer • Mobile Developer in Windows Phone and Android
Setup Your PC• Visual Studio 2010• Microsoft SQL Server 2008 Express R2• If you have low capacity PC you can setup Visual Studio 2008Note• Visual Studio 2010 can create project with .NET 4.0 or lower• Visual Studio 2008 can create project with .NET 3.5 or lower
What is Database?A database is a collection of information that isorganized so that it can easily beaccessed, managed, and updated. In oneview, databases can be classified according totypes of content: bibliographic, full-text, numeric, and images.
What is Database?(Cont.)In computing, databases are sometimes classifiedaccording to their organizational approach. The mostprevalent approach is the relational database, a tabulardatabase in which data is defined so that it can bereorganized and accessed in a number of different ways.A distributed database is one that can be dispersed orreplicated among different points in a network. Anobject-oriented programming database is one that iscongruent with the data defined in object classes andsubclasses.
What is Database?(Cont.)Computer databases typically containaggregations of data records or files, such assales transactions, product catalogs andinventories, and customer profiles. Typically, adatabase manager provides users thecapabilities of controlling read/writeaccess, specifying report generation, andanalyzing usage.
What is Database?(Cont.)Databases and database managers are prevalent inlarge mainframe systems, but are also present insmaller distributed workstation and mid-range systemssuch as the AS/400 and on personal computers. SQL(Structured Query Language) is a standard language formaking interactive queries from and updating adatabase such as IBMs DB2, Microsofts SQLServer, and database products from Oracle, Sybase, andComputer Associates.
DBMSA Database Management System (DBMS) is a set ofprograms that enables storing, modifying, andextracting information from a database, it alsoprovides users with tools toadd, delete, access, modify, and analyze data storedin one location. A group can access the data by usingquery and reporting tools that are part of the DBMSor by using application programs specifically writtento access the data.
DBMS (Con.t)DBMS’s also provide the method for maintainingthe integrity of stored data, running security andusers access, and recovering information if thesystem fails.
List of DBMS • MS SQL Server (Microsoft) • MS Access (Microsoft) • Oracle (Oracle) • My SQL (Oracle) • DB2 (IBM) • Sybase (SAP)
What is Data Access?The main goal of all APIs is to provide universal data access bymeans of having a single code base for accessing data fromany source, from any language.Having universal data access is important for four reasons:1. Developers can easily work on applications targeting different data stores.2. Developers can have a common framework for data access when switching between programming languages.3. Developers are more easily write a single application that can be deployed against multiple data stores.4. It provides a level of abstraction between the application and direct communication to the database to simplify the code the average developer needs to write.
What is Data Access? (Cont.)At first, there were no common interfaces for accessingdata. Each data provider exposed an API or othermeans of accessing its data. The developer only had tobe familiar with the API of the data provider used.When companies switched to a new databasesystem, any knowledge of how to use the old systembecame worthless and the developer had to learn anew system from scratch. Something needed to bedone to standardize the way in which data wasretrieved from various sources.
History of Data AccessFollowing is a short history of the evolution ofMicrosoft Data Access APIs in four parts :1. ODBC, DAO, RDO, ODBCDirect2. OLEDB, ADO3. ADO.NET4. ADO.NET Entities, LINQ
ODBCOpen Database Connectivity (ODBC)helped address the problem of needing toknow the details of each DBMS used. ODBCprovides a single interface for accessing anumber of database systems. Toaccomplish this, ODBC provides a drivermodel for accessing data. Any databaseprovider can write a driver for ODBC toaccess data from their database system.
ODBC (Cont.)This enables developers to access thatdatabase through the ODBC drivers instead oftalking directly to the database system. Fordata sources such as files, the ODBC driverplays the role of the engine, providing directaccess to the data source. In cases where theODBC driver needs to connect to a databaseserver, the ODBC driver typically acts as awrapper around the API exposed by thedatabase server.
ODBC (Cont.)ODBC was a huge leap forward and helped togreatly simplify database-driven applicationdevelopment. It does have some shortfalls, though.• First, it is only capable of supporting relational data. If you need to access a hierarchical data source such as LDAP, or semi-structured data, ODBC can’t help you.• Second, it can only handle SQL statements, and the result must be represent able in the form of rows and columns.
DAOWith the release of Visual Basic 2.0, developers wereintroduced to a new method for accessing data, knownas Data Access Objects (DAO). This was Microsoft’s firstattempt to create a data consumer API.DAO was based on the JET engine, which was largelydesigned to help developers take advantage of thedesktop database application Microsoft was about torelease, Microsoft Access. It served to provide anotherlayer of abstraction between the application and dataaccess, making the developer’s task simpler.
DAO (Cont.)With release of DAO 1.0, which supported directcommunication with Microsoft Access databaseswithout using ODBC.The main problem with DAO is that it can only talkto the JET engine. The JET engine thencommunicates with ODBC to retrieve the data.Going through this extra translation layer addsunnecessary overhead and makes accessing datathrough DAO slow
RDORemote Data Objects (RDO) wasMicrosoft’s solution to the slowperformance created by DAO. For talkingto databases other than MicrosoftAccess, RDO did not use the JET enginelike DAO; instead, it communicateddirectly with the ODBC layer.
RDO (Cont.)Removing the JET engine from the callstack greatly improved performance toODBC data sources! The JET engine wasonly used when accessing a MicrosoftAccess Database. In addition, RDO hadthe capability to use client-side cursorsto navigate the records, as opposed tothe server-side cursor requirements ofDAO.
RDO (Cont.)This greatly reduced the load on the databaseserver, enabling not only the application toperform better, but also the databases on whichthat application was dependant.
RDO (Cont.)RDO was primarily targeted toward larger, commercialcustomers, many of whom avoided DAO due to the performanceissues. Instead of RDO replacing DAO, they largely co-existed. Thisresulted for several reasons: First, users who developed smallerapplications, where performance wasn’t as critical, didn’t want totake the time to switch over to the new API. Second, RDO wasoriginally only released with the Enterprise Edition of Visual Basic, sosome developers didn’t have a choice. Third, with the release ofODBCDirect, a DAO add-on that routed the ODBC requests throughRDO instead of the JET engine, the performance gap between thetwo became much smaller. Finally, it wasn’t long after the release ofRDO that Microsoft’s next universal access API was released.
OLE DBObject Linking and Embedding Database (OLE-DB)was the next big step forward in data providers, andit is still widely used today. With OLE-DB, Microsoftapplied the knowledge learned from developingODBC to provide a better data access model. OLE-DB marked Microsoft’s move to a COM-basedAPI, which made it easily consumable by mostprogramming languages, and the migration to a 32-bit OS with the release of Windows 95.
ADOMicrosoft introduced ActiveX Data Objects (ADO)primarily to provide a higher-level API for working withOLE-DB. With this release, Microsoft took many of thelessons from the past to build a lighter, moreefficient, and more universal data access API. UnlikeRDO, ADO was initially promoted as a replacement forboth DAO and RDO. At the time of its release, it (alongwith OLE-DB) was widely believed to be a universalsolution for accessing any type of data—from databasesto e-mail, flat text files, and spreadsheets.
ADO (Cont.)ADO was primarily released to complement OLE-DB;however, ADO was not limited to just communicating with OLE-DB data sources. ADO introduced the provider model, whichenabled software vendors to create their own providersrelatively easily, which could then be used by ADO tocommunicate with a given vendor’s data source and implementmany of the optimizations specific to that data source. The ODBCprovider that shipped with ADO was one example of this. Whena developer connected to an ODBC data source, ADO wouldcommunicate through the ODBC provider instead of throughOLE-DB. More direct communication to the data source resultedin better performance and an easily extensible framework.
ADO (Cont.)In addition to being a cleaner objectmodel, ADO also offered a wider feature set tohelp lure developers away from DAO and RDO.These included the following:❑ Batch Updating—For the first time, usersenjoyed the capability to make changes to anentire recordset in memory and then persistthese changes back to the database by using theUpdateBatch command.
ADO (Cont.)❑ Disconnected Data Access—Although this wasn’tavailable in the original release, subsequent releasesoffered the capability to work with data in adisconnected state, which greatly reduced the loadplaced on database servers.❑ Multiple Recordsets—ADO provided the capabilityto execute a query that returns multiple recordsetsand work with all of them in memory. This featurewasn’t even available in ADO.NET until thisrelease, now known as Multiple Active Result Sets(MARS).
ADO.NETWith the release of the .NETFramework, Microsoft introduced a new dataaccess model, called ADO.NET. The ActiveX DataObject acronym was no longer relevant, asADO.NET was not ActiveX, but Microsoft keptthe acronym due to the huge success of ADO. Inreality, it’s an entirely new data access modelwritten in the .NET Framework.
ADO.NET (Cont.)ADO.NET supports communication to data sourcesthrough both ODBC and OLE-DB, but it also offers anotheroption of using database-specific data providers. Thesedata providers offer greater performance by being able totake advantage of data-source-specific optimizations. Byusing custom code for the data source instead of thegeneric ODBC and OLE-DB code, some of the overhead isalso avoided. The original release of ADO.NET included aSQL provider and an OLE-DB provider, with the ODBC andOracle providers being introduced later. Many vendorshave also written providers for their databases since.
ADO.NET (Cont.)With ADO.NET, the days of the recordset and cursor aregone. The model is entirely new, and consists of five basicobjects:❑ Connection—The Connection object is responsible forestablishing and maintaining the connection to the datasource, along with any connection-specific information.❑ Command—The Command object stores the querythat is to be sent to the data source, and any applicableparameters.❑ DataReader—The DataReader object providesfast, forward-only reading capability to quickly loopthrough the records.
ADO.NET (Cont.)❑ DataSet—The DataSet object, along with its childobjects, is what really makes ADO.NET unique. It provides astorage mechanism for disconnected data. The DataSetnever communicates with any data source and is totallyunaware of the source of the data used to populate it. Thebest way to think of it is as an in-memory repository tostore data that has been retrieved.❑ DataAdapter—The DataAdapter object is what bridgesthe gap between the DataSet and the data source. TheDataAdapter is responsible for retrieving the data from theCommand object and populating the DataSet with the datareturned. The DataAdapter is also responsible for persistingchanges to the DataSet back to the data source.
ADO.NET (Cont.)ADO.NET made several huge leaps forward.Arguably, the greatest was the introduction of trulydisconnected data access. Maintaining a connectionto a database server such as MS SQL Server is anexpensive operation. The server allocates resources toeach connection, so it’s important to limit the numberof simultaneous connections. By disconnecting fromthe server as soon as the data is retrieved, instead ofwhen the code is done working with that data, thatconnection becomes available for anotherprocess, making the application much more scalable.
ADO.NET (Cont.)Another feature of ADO.NET that greatly improvedperformance was the introduction of connectionpooling. Not only is maintaining a connection to thedatabase an expensive operation, but creating anddestroying that connection is also very expensive.Connection pooling cuts down on this. When aconnection is destroyed in code, the Frameworkkeeps it open in a pool. When the next processcomes around that needs a connection with thesame credentials, it retrieves it from thepool, instead of creating a new one.
ADO.NET (Cont.)Several other advantages are made possibleby the DataSet object. The DataSet objectstores the data as XML, which makes it easy tofilter and sort the data in memory. It alsomakes it easy to convert the data to otherformats, as well as easily persist it to anotherdata store and restore it again.
ADO.NET EntitiesADO.NET Entities builds upon our mutual investment inADO.NET by adding the ability to write applicationsagainst a rich conceptual "Entity Data Model"schema, rather than a flat relational database schema.The Entity Data Model (EDM) extends the relational datamodel with Entity-Relationship (ER) constructs formodeling real-world concepts such as Inheritance (carsand trucks are vehicles), Relationships (customers haveorders), and complex members (street, city, region, andpostal code composed as a single "address" propertywithin a customer).
ADO.NET Entities (Cont.)An extended-SQL grammar called "Entity SQL"allows you to directly query your conceptualschema, leveraging inheritance, accessing complexmembers, and navigating relationships. In manycases, building these concepts into the conceptualschema and query language removes the need forcomplex joins, unions, and subqueries to doconceptually simple operations.
LINQ"LINQ" stands for "Language INtegrated Query". As thename implies, LINQ integrates query concepts directlyinto the programming languages, enabling data accesscode to be type-checked by the language complier, anddeveloper tools like Intellisense to make it easier fordevelopers to write queries. This, along with higherlevel conceptual models (like Entities), contributes toreducing the impedance mismatch betweenapplications and data.
LINQ (Cont.)LINQ is supported as a first-class citizen within the ADO.NETEntity Framework through "LINQ to Entities". LINQ toEntities is part of the Object Services layer which enablesyou to build queries through strongly typed languageexpressions and built-in query comprehensions (in C# andVB) as well as textual Entity SQL statements. This meansthat the same conceptual client views are available throughexisting ADO.NET provider programming patterns orthrough consuming Object Services using either ad-hoctextual queries or integrated language queries.
ORMObject-Relational Mapping (ORM, O/RM, and O/RMapping) in computer software is a programmingtechnique for converting data between incompatibletype system in object-oriented programminglanguages. This creates, in effect, a “virtual objectdatabase” that can be used from within theprogramming language. Data management tasks inobject-oriented (OO) programming are typicallyimplemented by manipulating objects that are almostalways non-scalar values.
ORM (Cont.)For example, consider an address book entry thatrepresents a s single person along with zero ormore phone numbers and zero or more address.This could be modeled in an object-orientedimplementation by a “Person object” withattributes/fields to hold each data item that theentry comprises: the person’s name, a list ofphone numbers, and a list of addresses.
ORM (Cont.)The list of phone numbers would itself contain“PhoneNumber objects” and so on. The addressbook entry is treated as a single object by theprogramming language (it can be referenced by asingle variable containing a pointer to theobject, for instance). Various methods can beassociated with the object, such as a method toreturn the preferred phone number, the homeaddress and so on.
SummaryWhat you learn from this chapter are:• Database• DBMS• Data Access• Type of Data Access are using with current .NET Framework (ADO.NET and LINQ)• ORM