Programming with Visual Basic.NET An Object-Oriented Approach  Chapter 8 Introduction to Database Processing.

Slides:



Advertisements
Similar presentations
Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control.
Advertisements

Chapter 10 Database Applications Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Chapter 12: Using ADO.NET 2.0 Programming with Microsoft Visual Basic 2005, Third Edition.
Introduction to Database Processing with ADO.NET.
VB.NET Database Access ISYS546. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
Chapter 10 Accessing Database Files Programming In Visual Basic.NET.
Chapter 12: ADO.NET and ASP.NET Programming with Microsoft Visual Basic.NET, Second Edition.
VB.NET Database Tools ISYS Net Applications OLE DB Provider OLE DB Data Source OLE DB Provider ODBC Data Source SQL Server Data Source SQL Server.Net.
Using ADO.NET Chapter Microsoft Visual Basic.NET: Reloaded 1.
Introduction to Data Adapter. A Simplified View of ADO.Net Objects Ado.Net Data Provider Connection Adapter Command Reader Dataset Data Consumer WinForm.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
ASP.NET Database Connectivity I. 2 © UW Business School, University of Washington 2004 Outline Database Concepts SQL ASP.NET Database Connectivity.
VB.NET Database Access ISYS 812. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
1 Pertemuan 09 Database Matakuliah: D0524 / Algoritma dan Pemrograman Komputer Tahun: 2005 Versi:
Introduction to ADO.Net and Visual Studio Database Tools ISYS 512.
VB.NET Database Access ISYS546. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS546.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
VB.NET Database Access ISYS546. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
Some Basic Database Terminology
Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
Chapter 11 Introduction to Database Processing. Class 11: Database Processing Use a Visual Studio Wizard to establish a database connection used to load.
Introduction to ADO.Net and Visual Studio Database Tools ISYS 512.
1 Lesson 6 — Database Programming Microsoft Visual Basic.NET, Introduction to Programming.
10-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances.
Navigating database with windows forms.. Tiered applications  Provide a means to develop many presentations of the same app  Makes changes to the back.
ADO.NET A2 Teacher Up skilling LECTURE 3. What’s to come today? ADO.NET What is ADO.NET? ADO.NET Objects SqlConnection SqlCommand SqlDataReader DataSet.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
Needs for Accessing Database To make your web site more dynamic and maintainable, you can display information on your web pages that are retrieved from.
CHAPTER EIGHT Accessing Data Processing Databases.
Objectives In this lesson, you will learn to: *Identify the need for ADO.NET *Identify the features of ADO.NET *Identify the components of the ADO.NET.
CSCI 6962: Server-side Design and Programming Database Manipulation in ASP.
ASP.NET Rina Zviel-Girshin Lecture 5
Session 8: ADO.NET. Overview Overview of ADO.NET What is ADO.NET? Using Namespaces The ADO.NET Object Model What is a DataSet? Accessing Data with ADO.NET.
Module 7: Accessing Data by Using ADO.NET
ADO.NET Data Access. Page  2 SQL  When we interact with the datasource through ADO.NET we use the SQL language to retrieve,modify,update information.
Christopher M. Pascucci.NET Programming: Databases & ADO.NET.
Windows Forms Navigating database with windows forms.
Using Adapter Wizard ISYS 512. Data Adapter Wizard – 2 nd Level of Using ADO.Net Configure Data Adapter and generating a dataset: –From the Data tab of.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
Chapter Thirteen Working with Access Databases and LINQ Programming with Microsoft Visual Basic th Edition.
 It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data.
Mauricio Featherman, Ph.D. Washington St. University
Introduction to ADO.Net and VS Database Tools and Data Binding ISYS 350.
Databases and ADO.NET Programming Right from the Start with Visual Basic.NET 1/e 11.
HNDIT Rapid Application Development
ADO.NET FUNDAMENTALS BEGINNING ASP.NET 3.5 IN C#.
ADO.NET Objects Data Adapters Dr. Ron Eaglin. Agenda Builds on Information in Part I Should have working knowledge of creating a database connection Continuation.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Address Book Application Introducing Database Programming.
Module 5 Data Classes DataView – DataGridView Control 1.
Module 2: Using ADO.NET to Access Data. Overview ADO.NET Architecture Creating an Application That Uses ADO.NET to Access Data Changing Database Records.
Introduction to ADO.Net and Visual Studio Database Tools ISYS 350.
Common SQL keywords. Building and using CASE Tools Data Base with Microsoft SQL-Server and C#
ASP.NET Programming with C# and SQL Server First Edition
Introduction to Database Processing with ADO.NET
Copyright Scott-Jones Publishing, All rights reserved.
Introduction to Database Processing with ADO.NET
ADO.NET Framework.
Lecture 6 VB.Net SQL Server.
Active Data Objects Binding ASP.NET Controls to Data
VB.NET Using Database.
Tonga Institute of Higher Education
Chapter 10 ADO.
Chapter 10 Accessing Database Files
Active Data Objects Binding ASP.NET Controls to Data
Introduction to ADO.Net and Visual Studio Database Tools.
Presentation transcript:

Programming with Visual Basic.NET An Object-Oriented Approach  Chapter 8 Introduction to Database Processing

Programming with Visual Basic.NET An Object-Oriented Approach  Slide 2 Objectives (1) Understand how VB.NET uses a database Use ADOc to manage table data Understand the basics of ADO.NET Define a connection to a database Use a DataAdapter to retrieve data from a database

Programming with Visual Basic.NET An Object-Oriented Approach  Slide 3 Objectives (2) Use the DataSet class Bind controls to a DataSet Use the ControlBindingsCollection Understand how users navigate between records Modify a database records Count records

Programming with Visual Basic.NET An Object-Oriented Approach  Slide 4 Introduction to ADO.NET The System.Data namespace provides access to database data Collectively referred to as ADO.NET ADO.NET is designed to work well with desktop and Internet applications Works with different databases Access, SQL Server Uses a disconnected architecture Note that ADO.NET and ADO are very different

Programming with Visual Basic.NET An Object-Oriented Approach  Slide 5 Establishing a Database Connection ADO.NET Collection class forms a pipeline between your application and a data source by means of a provider Two controls supply the connection OleDbConnection control works with many databases SQLConnection control works only with SQL server Faster access

Programming with Visual Basic.NET An Object-Oriented Approach  Slide 6 OleDBConnection Class (Introduction) Belongs to the System.Drawing.OleDB namespace When creating an instance of the OleDbConnection control, VB.NET writes code to create an instance of the OleDbConnection class and configure it

Programming with Visual Basic.NET An Object-Oriented Approach  Slide 7 OleDbConnection Class (Properties) Properties ConnectionString property contains a string defining the database provider, database name, and security information ConnectionTimeout property defines the number of seconds to wait for a connection to be established DataSource property gets location and file name of the database Provider property specifies property that will be used to establish the database connection State property defines connection status (open, closed, etc.)

Programming with Visual Basic.NET An Object-Oriented Approach  Slide 8 OleDbConnection Class (Methods and Events) Methods Close method closes an open connection Open method opens a closed connection Uses the contents of ConnectionString property to open the connection Events StateChanged event fires when the connection state (State property) changes

Programming with Visual Basic.NET An Object-Oriented Approach  Slide 9 Connection (Illustration) Your solution ADO.NET connection Database provider database Sends commands Gets data

Programming with Visual Basic.NET An Object-Oriented Approach  Slide 10 Creating a Connection (Step 1) Select Microsoft Jet 4.0 OLE DB Provider for Access databases

Programming with Visual Basic.NET An Object-Oriented Approach  Slide 11 Creating a Connection (Step 2) Select database

Programming with Visual Basic.NET An Object-Oriented Approach  Slide 12 Code Behind a Database Connection Provider key specifies the database provider User ID and Password keys supply authentication information Data Source key supplies database file Mode key defines how database will be shared among connected users Provider=Microsoft.Jet.OLEDB.4.0;Password="";User ID=Admin; Data Source=D:\VBOOP7\Chapter.08\Data\Employees.mdb; Mode=Share Deny None...

Programming with Visual Basic.NET An Object-Oriented Approach  Slide 13 The DataAdapter Use the OleDBDataAdapter control to create a DataAdapter Control supports a Wizard to help you configure the DataAdapter Wizard writes code to create an instance of the OleDbDataAdapter class Wizard writes SQL statements to select, update, and delete records

Programming with Visual Basic.NET An Object-Oriented Approach  Slide 14 The DataAdapter Class Properties SelectCommand property contains an SQL SELECT statement InsertCommand, UpdateCommand, and DeleteCommand properties contain SQL INSERT, UPDATE, and DELETE statements Methods Fill method uses a connection to fill a DataSet with data Update method sends changed data back to the DataSet

Programming with Visual Basic.NET An Object-Oriented Approach  Slide 15 Creating a Data Adapter (Step 1) Create an instance of the OleDbDataAdapter control on the form VB.NET activates the Wizard Wizard starts by displaying an introductory screen

Programming with Visual Basic.NET An Object-Oriented Approach  Slide 16 Creating a Data Adapter (Step 2) Add tables to the database Select table(s) to use

Programming with Visual Basic.NET An Object-Oriented Approach  Slide 17 Creating a Data Adapter (Step 3) Define the SQL statement Select columns Sort columns SELECT statement

Programming with Visual Basic.NET An Object-Oriented Approach  Slide 18 Creating a Data Adapter (Step 4) Completed SQL SELECT statement

Programming with Visual Basic.NET An Object-Oriented Approach  Slide 19 The Role of the DataSet The DataAdapter retrieves rows into another object called a DataSet DataSet is NOT connected to the database once the data has been retrieved DataSet contains one or more tables Each table in the DataSet contains one or more rows

Programming with Visual Basic.NET An Object-Oriented Approach  Slide 20 The Role of the DataSet (Illustration) Your solution Provider Connection database DataSet DataAdapter sends command DataAdapter creates DataSet

Programming with Visual Basic.NET An Object-Oriented Approach  Slide 21 Creating a DataSet Enter DataSet name Select table

Programming with Visual Basic.NET An Object-Oriented Approach  Slide 22 The DataSet Class The Tables property gets a list of tables stored in the DataSet For each table VB.NET defines a property having the same name as the underlying table Clear method removes all the data from the DataSet DataSet is not connected so data is not removed from the database AcceptChanges marks any changed rows as unchanged Use to synchronize the DataSet with the database Automatically called after an Update GetChanges method gets rows containing changed records HasChanges method determines if there are changed records

Programming with Visual Basic.NET An Object-Oriented Approach  Slide 23 The DataTable Class Columns property returns a collection of columns Use for schema information DataSet property returns a reference to the parent DataSet object Rows property returns the collection of rows in the DataTable Use for data TableName property contains a string defining the table name AcceptChanges, Clear, and NewRow methods are the same as the properties of the DataSet itself

Programming with Visual Basic.NET An Object-Oriented Approach  Slide 24 Filling a Dataset Call the Fill method of the DataAdapter using the DataSet name as the argument Assume that the DataAdapter is named odbdaEmployees and the DataSet is named DsEmployees1 Note pintRecords contains the number of records returned Dim pintRecords As Integer pintRecords = odbdaEmployees.Fill(DsEmployees1)

Programming with Visual Basic.NET An Object-Oriented Approach  Slide 25 DataBinding Users typically want to see data appearing in a text box or other control instance The process of associating a control instance like a text box with a field in a data source is called data binding Two types of data binding Simple binding – Display 1 row from a data source Complex binding – Display multiple rows from a data source

Programming with Visual Basic.NET An Object-Oriented Approach  Slide 26 The Binding Object Create a Binding object so as to bind a control instance to a data source Note that the Binding object specifies the control property to bind, not the control instance to bind Syntax: Binding constructor Public Sub New( ByVal propertyname As string, ByVal datasource As Object, ByVal datamember As String) As String

Programming with Visual Basic.NET An Object-Oriented Approach  Slide 27 Binding Constructor (Arguments) propertyname contains the name of the control property to bind The Text property of a TextBox for example datasource contains the name of a DataSet or DataTable object datamember provides the navigation path to a field in the DataSet

Programming with Visual Basic.NET An Object-Oriented Approach  Slide 28 Binding Constructor (Example) Create a new binding Text property DataSet named DsEmployees1 Navigation Path is "tblEmployees.fldEmployeeID" Dim pbndTemp As Binding pbndTemp = New Binding("Text, _ DsEmployees1, _ "tblEmployees.fldEmployeeID")

Programming with Visual Basic.NET An Object-Oriented Approach  Slide 29 The ControlBindingsCollection Using with an instance of the Binding class to bind a control Call the Add method to bind the control instance Call the Clear method to remove all the bindings Count property gets the number of elements in the collection Item property gets a specific binding from the collection

Programming with Visual Basic.NET An Object-Oriented Approach  Slide 30 Adding a Control Binding Add method syntax: Overloads Public Function Add ( ByVal binding As Binding ) As Binding Binding argument contains the name of an existing binding Method returns the binding that was added

Programming with Visual Basic.NET An Object-Oriented Approach  Slide 31 Adding a Control Binding (Example) Create a binding and add associate it with the text box named txtEmployeeID Dim pbndTemp As Binding pbndTemp = New Binding("Text", DsEmployees1, _ "tblEmployees.fldEmployeeID") txtEmployeeID.DataBindings.Add(pbndTemp) txtEmployeeID.DataBindings.Add("Text", DsEmployees1, _ "tblEmployees.fldEmployeeID")

Programming with Visual Basic.NET An Object-Oriented Approach  Slide 32 Record Navigation (Overview) Users need a way to navigate from record to record Use the BindingContext of the Form class Multiple BindingContexts can exists CurrencyManager and BindingContext keep control instances synchronized

Programming with Visual Basic.NET An Object-Oriented Approach  Slide 33 BindingManageBase Use with the BindingContext Set the Position property to navigate Count property gets the number of managed rows AddNew method adds a new item EndCurrentEdit method ends the editing process RemoveAt method removes a row

Programming with Visual Basic.NET An Object-Oriented Approach  Slide 34 Record Navigation (First Record) Set the Position property of the Binding Context to 0 to locate the first row Note Position property is 0-based Me keyword references the form Me.BindingContext(DsEmployees1, _ "tblEmployees").Position = 0

Programming with Visual Basic.NET An Object-Oriented Approach  Slide 35 Record Navigation (Previous Record) Decrement the Position property of the BindingContext by 1 to locate the previous record Me.BindingContext(DsEmployees1, "tblEmployees").Position -= 1

Programming with Visual Basic.NET An Object-Oriented Approach  Slide 36 Record Navigation (Next Record) Increment the Position property of the BindingContext by 1 to locate the next record Me.BindingContext(DsEmployees1, "tblEmployees").Position += 1

Programming with Visual Basic.NET An Object-Oriented Approach  Slide 37 Record Navigation (Last Record) Use the underlying DataSet and DataTable to determine the row count Me.BindingContext(DsEmployees1, _ "tblEmployees").Position = _ Me.DsEmployees1.tblEmployees.Rows.Count - 1

Programming with Visual Basic.NET An Object-Oriented Approach  Slide 38 Adding Records When adding a record, the new record (stored in the DataSet) must be saved to the database First get the newly added row from the database Using the DataAdapter, call the Update method to save the changes to the database Call the AcceptChanges method on the DataSet to synchronize the DataSet and database

Programming with Visual Basic.NET An Object-Oriented Approach  Slide 39 Adding Records (Example) End editing, update the database, and then call AcceptChanges Me.BindingContext(DsEmployees1, _ "tblEmployees").EndCurrentEdit pdsInsertedRows = _ DsEmployees1.GetChanges(DataRowState.Added) If Not pdsInsertedRows Is Nothing Then odbdaEmployees.Update(pdsInsertedRows) End If DsEmployees1.AcceptChanges

Programming with Visual Basic.NET An Object-Oriented Approach  Slide 40 Deleting Records Call the RemoveAt method of the BindingContext Call the GetChanges method on the DataSet to get the removed record Call the Update method on the DataAdapter to save the changes Call the AcceptChanges method on the DataSet to synchronize the DataSet and database

Programming with Visual Basic.NET An Object-Oriented Approach  Slide 41 Deleting Records (Example) Dim pdsDeletedRows As DataSet Me.BindingContext(DsEmployees1, "tblEmployees"). _ RemoveAt(Me.BindingContext(DsEmployees1, _ "tblEmployees").Position) pdsDeletedRows = _ DsEmployees1.GetChanges(DataRowState.Deleted) odbdaEmployees.Update(pdsDeletedRows) DsEmployees1.AcceptChanges()

Programming with Visual Basic.NET An Object-Oriented Approach  Slide 42 Canceling an Update Call the CancelCurrentEdit method on the BindingContext Me.BindingContext(DsEmployees1, _ "tblEmployees").CancelCurrentEdit