Presentation is loading. Please wait.

Presentation is loading. Please wait.

VB.NET Using Database.

Similar presentations


Presentation on theme: "VB.NET Using Database."— Presentation transcript:

1 VB.NET Using Database

2 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

3 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

4 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

5 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

6 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

7 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

8 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

9 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

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

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

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

13 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

14 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

15 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

16 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

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

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

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

20 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

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

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

23 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

24 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

25 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

26 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

27 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

28 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

29 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

30 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

31 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

32 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

33 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

34 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

35 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

36 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

37 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

38 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

39 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

40 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

41 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

42 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

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


Download ppt "VB.NET Using Database."

Similar presentations


Ads by Google