VB.NET Using Database.

Slides:



Advertisements
Similar presentations
17. Data Access ADO.Net Architecture New Features of ADO.NET
Advertisements

Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control.
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.
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.
Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.
Chapter 11 Introduction to Database Processing. Class 11: Database Processing Use a Visual Studio Wizard to establish a database connection used to load.
Programming with Visual Basic.NET An Object-Oriented Approach  Chapter 8 Introduction to Database Processing.
Introduction to ADO.Net and Visual Studio Database Tools ISYS 512.
1 Lesson 6 — Database Programming Microsoft Visual Basic.NET, Introduction to Programming.
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.
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.
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)
 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.
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.
Programming the Web Using ASP.Net
Lecture 6 VB.Net SQL Server.
IS444: Modern tools for applications development
Active Data Objects Binding ASP.NET Controls to Data
IS444: Modern tools for applications development
ADO.NET Accessing Databases in VS.NET
By Chris Pascucci and FLF
Tonga Institute of Higher Education
Brief description on how to navigate within this presentation (ppt)
CIS16 Application Programming with Visual Basic
Chapter 10 ADO.
Database Applications
Chapter 10 Accessing Database Files
Active Data Objects Binding ASP.NET Controls to Data
M S COLLEGE OF ART’S, COMM., SCI. & BMS Advance Web Programming
Introduction to ADO.Net and Visual Studio Database Tools.
Presentation transcript:

VB.NET Using Database

Objectives Understand how VB .NET uses a database Understand the basics of ADO.NET Define a connection to a database Use a DataAdapter to retrieve data from a database Use the DataSet class Bind controls to a DataSet Use the ControlBindingsCollection Understand how users navigate between records Modify a database records Count records 11/8/2018 CIS371-1

Understanding How VB .NET Uses a Database A database consists of information related to a particular topic or purpose Information is organized into one or more tables A table consists of rows and columns Prefix for a table is "tbl" A row is called a record Each record contains multiple pieces of information Each piece of information resides in a field Prefix for a field is "fld" 11/8/2018 CIS371-1

Characteristics of a Database Many vendors supply databases Oracle Microsoft SQL Server (More scaleable. Use with a large number of users) Access (Use with just a few users) Database are typically relational Developers can relate information stored in tables in different ways 11/8/2018 CIS371-1

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 11/8/2018 CIS371-1

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 11/8/2018 CIS371-1

OleDBConnection Class 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 11/8/2018 CIS371-1

OleDbConnection Class 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.) 11/8/2018 CIS371-1

OleDbConnection Class Methods and Events 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 11/8/2018 CIS371-1

Connection Your solution ADO.NET connection Database provider database Sends commands Gets data 11/8/2018 CIS371-1

Creating a Connection (Step 1) Select Microsoft Jet 4.0 OLE DB Provider for Access databases 11/8/2018 CIS371-1

Creating a Connection (Step 2) Select database 11/8/2018 CIS371-1

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 . . . 11/8/2018 CIS371-1

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 11/8/2018 CIS371-1

The DataAdapter Class Properties Methods 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 11/8/2018 CIS371-1

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 11/8/2018 CIS371-1

Creating a Data Adapter (Step 2) Add tables to the database Select table(s) to use 11/8/2018 CIS371-1

Creating a Data Adapter (Step 3) Define the SQL statement Select columns Sort columns SELECT statement 11/8/2018 CIS371-1

Creating a Data Adapter (Step 4) Completed SQL SELECT statement 11/8/2018 CIS371-1

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 11/8/2018 CIS371-1

The Role of the DataSet database DataSet Your solution DataAdapter sends command Connection Provider database DataAdapter creates DataSet DataSet 11/8/2018 CIS371-1

Creating a DataSet Enter DataSet name Select table 11/8/2018 CIS371-1

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 GetChanges method gets rows containing changed records HasChanges method determines if there are changed records 11/8/2018 CIS371-1

The DataTable Class Columns property returns a collection of columns DataSet property returns a reference to the parent DataSet object Rows property returns the collection of rows in the DataTable TableName property contains a string defining the table name AcceptChanges, Clear, and NewRow methods are the same as the properties of the DataSet itself 11/8/2018 CIS371-1

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) 11/8/2018 CIS371-1

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 11/8/2018 CIS371-1

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 11/8/2018 CIS371-1

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 11/8/2018 CIS371-1

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") 11/8/2018 CIS371-1

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 11/8/2018 CIS371-1

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 11/8/2018 CIS371-1

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, _ 11/8/2018 CIS371-1

Record Navigation 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 11/8/2018 CIS371-1

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 11/8/2018 CIS371-1

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 11/8/2018 CIS371-1

Record Navigation (Previous Record) Decrement the Position property of the BindingContext by 1 to locate the previous record Me.BindingContext(DsEmployees1, "tblEmployees").Position -= 1 11/8/2018 CIS371-1

Record Navigation (Next Record) Increment the Position property of the BindingContext by 1 to locate the next record Me.BindingContext(DsEmployees1, "tblEmployees").Position += 1 11/8/2018 CIS371-1

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 11/8/2018 CIS371-1

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 11/8/2018 CIS371-1

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 11/8/2018 CIS371-1

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 11/8/2018 CIS371-1

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() 11/8/2018 CIS371-1

Canceling an Update Call the CancelCurrentEdit method on the BindingContext Me.BindingContext(DsEmployees1, _ "tblEmployees").CancelCurrentEdit 11/8/2018 CIS371-1