View
223
Download
0
Category
Preview:
Citation preview
8/10/2019 Vb Net Data Access Sf 10
http://slidepdf.com/reader/full/vb-net-data-access-sf-10 1/38
Introduction to ADO.Net, VB.Net
Database Tools and Data Binding
ISYS 512
8/10/2019 Vb Net Data Access Sf 10
http://slidepdf.com/reader/full/vb-net-data-access-sf-10 2/38
Database Processing
• Querying database
• Updating database:
– Insertion, deletion, modification
8/10/2019 Vb Net Data Access Sf 10
http://slidepdf.com/reader/full/vb-net-data-access-sf-10 3/38
Steps to Retrieve Data
• Establishes a connection to the database.
• Executes commands against the database.
– SQL Select commands
• Store data results.
8/10/2019 Vb Net Data Access Sf 10
http://slidepdf.com/reader/full/vb-net-data-access-sf-10 4/38
A Simplified View of ADO.Net
Objects
Ado.Net
Data Provider
Connection
Adapter
Command
Reader
DatasetData Consumer
WinForm
WebForm
SQL Updates
Results of SQL Selects
8/10/2019 Vb Net Data Access Sf 10
http://slidepdf.com/reader/full/vb-net-data-access-sf-10 5/38
ADO.NET Objects
• Connection Object: Represent a connection to thedatabase.
• Command Object: The command object allows usto execute a SQL statement or a stored procedure.
• DataReader: It is a read-only and forward-only pointer into a table to retrieve records.
• DataSet Object: A DataSet object can hold several
tables and relationships between tables.• DataAdapter: This the object used to pass data
between the database and the dataset.
8/10/2019 Vb Net Data Access Sf 10
http://slidepdf.com/reader/full/vb-net-data-access-sf-10 6/38
Data Providers
• ODBC Provider
– Open Database Connectivity
• A driver manager
• Used for relational databases
• OLE DB Provider
– OLE DB interfaces provide applications with uniform access todata stored in diverse information sources, or data stores.
– Access
• SQL Server Provider
• Oracle Provider
– Microsoft’s .NET data provider for Oracle
• Etc.
8/10/2019 Vb Net Data Access Sf 10
http://slidepdf.com/reader/full/vb-net-data-access-sf-10 7/38
8/10/2019 Vb Net Data Access Sf 10
http://slidepdf.com/reader/full/vb-net-data-access-sf-10 8/38
Demo: SAS Reading ODBC Data
• proc sql;
• connect to odbc (dsn=MySalesDB2007);
• Create table sasuser.TEST
• As select * from connection to ODBC
• (select * from customer);
• disconnect from odbc;
• quit;
• proc sql;
• select * from sasuser.test;
8/10/2019 Vb Net Data Access Sf 10
http://slidepdf.com/reader/full/vb-net-data-access-sf-10 9/38
VB.NET Database Tools
• Add a new connection to database: – Database connection:
• Tools/Connect to database
– Add Connection
– Server Explorer
• Data connections: Right click data connection
– Add Connection
» Tables, Views
• Create new SQL Server Database
• Add a new data source: – Data/Add New Data Source
8/10/2019 Vb Net Data Access Sf 10
http://slidepdf.com/reader/full/vb-net-data-access-sf-10 10/38
Types of Data Source
8/10/2019 Vb Net Data Access Sf 10
http://slidepdf.com/reader/full/vb-net-data-access-sf-10 11/38
Working with SQL Server
• Starting SQL Server:
– Control Panel/Administrative
Tools/Services/SQLServer
• SQL Server database file extension: mdf
• Default database folder:
– A SQL Server file: testSQL08.mdf
– C:\Program Files\Microsoft SQL
Server\MSSQL.1\MSSQL\Data\testSQL08.mdf
8/10/2019 Vb Net Data Access Sf 10
http://slidepdf.com/reader/full/vb-net-data-access-sf-10 12/38
Creating SQL Server Database
• From Server Explorer, right click data connectionand choose:
• Create new SQL Server Database
• Server name:
– LocalServerName\SQLExpress• Add new table: Right click Tables and choose Add
New Table
– Define table’s fields
• Add rows: Right click the table name and chooseShow table data.
• Note: After creating the database, you may createan ODBC DSN to connect to it.
8/10/2019 Vb Net Data Access Sf 10
http://slidepdf.com/reader/full/vb-net-data-access-sf-10 13/38
Create a New SQL Server
Database: EmployeeDB• Add a new table: EmployeeTable
– EID Char 10
– Ename Char 30
– Sex Char 1
– Salary Numeric (10,2)
– HireDate Date
• Enter data
8/10/2019 Vb Net Data Access Sf 10
http://slidepdf.com/reader/full/vb-net-data-access-sf-10 14/38
How to create ADO.Net objects?
• Automatically generated when creatingdata bound form.
– Form wizard• Using Data Adapter Wizard
• Using code:
– Example: – dim strConn as string ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source= c:\sales2k.mdb"
– dim objConn as new OledbConnection(strConn)
– objConn.open()
8/10/2019 Vb Net Data Access Sf 10
http://slidepdf.com/reader/full/vb-net-data-access-sf-10 15/38
Data Binding
• Connect a control or property to one or more data
elements.
• Simple binding: Use simple binding to display afield value in controls that show Data Bindings in
the property window, such as text box or label.
• Complex binding: Use complex binding to bind
more than one field to controls such as DataGrid
and list box. Use the control’s Data Source and
Data Member to bind the data.
8/10/2019 Vb Net Data Access Sf 10
http://slidepdf.com/reader/full/vb-net-data-access-sf-10 16/38
Creating Data Bound Form
• Creating a form with ADO.Net objects and data-
bound controls to display and update information in
a dataset.
• Demo: – Add data source:
• Data/Add New Data Source
• Data/Show Data Source
– Click the dropdown list next to the table’s name: • Datagrid view
• Details
– Drag the table to form.
8/10/2019 Vb Net Data Access Sf 10
http://slidepdf.com/reader/full/vb-net-data-access-sf-10 17/38
Items Added to the Form
• Connection• Table Adapter: click smart tag
– Add query
– Preview data
• Dataset: – Edit in dataset designer
• Binding Source
– Add query: Add a new tool strip.
– Preview data
• Binding navigator
• Code view: Form load event
8/10/2019 Vb Net Data Access Sf 10
http://slidepdf.com/reader/full/vb-net-data-access-sf-10 18/38
Generated Code
Private Sub CUSTOMERBindingNavigatorSaveItem_Click(ByVal sender
As System.Object, ByVal e As System.EventArgs) Handles
CUSTOMERBindingNavigatorSaveItem.Click
Me.Validate()
Me.CUSTOMERBindingSource.EndEdit()
Me.CUSTOMERTableAdapter.Update(Me.SalesDBDataSet.CUSTOMER)
End Sub
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the'SalesDBDataSet.CUSTOMER' table. You can move, or remove it, as
needed.
Me.CUSTOMERTableAdapter.Fill(Me.SalesDBDataSet.CUSTOMER)
End Sub
8/10/2019 Vb Net Data Access Sf 10
http://slidepdf.com/reader/full/vb-net-data-access-sf-10 19/38
Other Data Form Demos
• DataGrid View
• Add /Modify/Delete records.
• Read only form:
– Delete AddNew, Delete, Save buttons fromnavigator bar.
8/10/2019 Vb Net Data Access Sf 10
http://slidepdf.com/reader/full/vb-net-data-access-sf-10 20/38
Hierarchical Forms
• Parent table/Child table
– Add parent table and child table to Data Source
– Drag the parent table and the child table to the form.Parent table uses detail view and child table usesdataGrid view
– Click Dataset object’s smart tag to choose Edit inDataset Designer
– With the designer, right click the parent table andchoose Add/Relation
– Change dataGrid’s DataSource property to the relation.
8/10/2019 Vb Net Data Access Sf 10
http://slidepdf.com/reader/full/vb-net-data-access-sf-10 21/38
Detail Form with Bound ListBox
• Example: Customer table form with CID listboxand displays selected customer information intextboxes.
– Choose detail view for the customer table. – Click the dropdown list next to the CID field and click
ListBox
– Drag the Customer table to the form.
– Bind the CID field to the BindingSource:• Activate the Property window and click the listbox
• Set the DataSOurce property to BindingSource
• Set the Display Member property to CID
8/10/2019 Vb Net Data Access Sf 10
http://slidepdf.com/reader/full/vb-net-data-access-sf-10 22/38
Creating A Database Application
Without Programming• Creating a database application to display
information and update database.
• A main form with buttons to open dataforms:
– DisplayInfo
– Enter New
– Modify
– Exit
8/10/2019 Vb Net Data Access Sf 10
http://slidepdf.com/reader/full/vb-net-data-access-sf-10 23/38
Data Adapter Wizard – 2nd Level
of Using ADO.Net• Configure Data Adapter and generating a dataset:
– From the Data tab of the ToolBox, DragOledbDataAdapter to the form.
– Use the Data Adapter Wizard to configure the Adapter. – Right Click the Adapter to preview data and create
dataset.
• Note: If OledbDataAdapter is not listed inToolbox’s Data tab , right click ToolBox DataTab and select Choose Item; then selectOleDbDataAdapter from .Net Framework components.
8/10/2019 Vb Net Data Access Sf 10
http://slidepdf.com/reader/full/vb-net-data-access-sf-10 24/38
Creating a Form with Bound DataGridView
• Configure Adapter and generate dataset.
• Bind DataGridView control to the table:
– Data Source property:• DataSet
– Data Member property
• A table in the dataset
• In the Form Load event, use Adapter’s Fillmethod to load the dataset:
• OleDbDataAdapter1.Fill(DataSet11)
• Note 1: No navigation capability
• Note 2: View adapter’s properties and seethe SQL commands.
8/10/2019 Vb Net Data Access Sf 10
http://slidepdf.com/reader/full/vb-net-data-access-sf-10 25/38
8/10/2019 Vb Net Data Access Sf 10
http://slidepdf.com/reader/full/vb-net-data-access-sf-10 26/38
Using BindingContext to Navigate
Records
• BindingContext: It is an object that manages a
collection of data sources used for binding.
• CurrencyManager: It is an object that keeps track
of position (the current row) of a data source. Twouseful properties:
– Position property: is the index of the current row. The
index is a 0-based index, the first record has a position
of 0. – Count property: The number of rows in the data source.
8/10/2019 Vb Net Data Access Sf 10
http://slidepdf.com/reader/full/vb-net-data-access-sf-10 27/38
MoveNext and MoveLast Example
• MoveNext: – Me.BindingContext(DataSet21, "customer").Position += 1
• MoveLast: – Me.BindingContext(DataSet21, "customer").Position =
Me.BindingContext(DataSet21, "customer").Count -1
• How to MovePrevious and MoveFirst?
• Note: The Position property takes care of the end of fileautomatically.
• Note: Me.BindingContext(DataSet21, "customer") returnsa CurrencyManager object.
8/10/2019 Vb Net Data Access Sf 10
http://slidepdf.com/reader/full/vb-net-data-access-sf-10 28/38
Using BindingSource Object to Do
the Binding and Navigation
• It is an object that keeps track of position (the current row)of a data source.
• Useful properties: – DataSource
– DataMember – Position property: is the index of the current row. The index is a 0-
based index, the first record has a position of 0.
• Methods: – MoveFirst, MoveLast, MoveNext, MovePrevious
– AddNew – AllowEdit
– EndEdit
– Current
– RemoveCurrent
8/10/2019 Vb Net Data Access Sf 10
http://slidepdf.com/reader/full/vb-net-data-access-sf-10 29/38
Bind Controls to BindingSource
Object• Configure Adapter and generate dataset.
• Add BindingSource object to the form
• Use BindingSource object’s property window toset its DataSource and Data Member properties tothe dataset object and table object.
• Set control’s DataBinding property to theBindingSource object.
8/10/2019 Vb Net Data Access Sf 10
http://slidepdf.com/reader/full/vb-net-data-access-sf-10 30/38
Use BindingSource Object’s
MoveNext, MovePrevious Methods• Add a MoveNext button
– BindingSource1.MoveNext()
• Add a MovePrevious button:
– BindingSource1.MovePrevious()
8/10/2019 Vb Net Data Access Sf 10
http://slidepdf.com/reader/full/vb-net-data-access-sf-10 31/38
Adding AddNew and Save Button
AddNew button: Use BindingSource AddNew Method:
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
BindingSource1.AddNew()
End Sub
Save button: Use BindingSource EndEdit method and
Adapter’s Update method:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
BindingSource1.EndEdit()
OleDbDataAdapter1.Update(DataSet21.CUSTOMER)
End Sub
8/10/2019 Vb Net Data Access Sf 10
http://slidepdf.com/reader/full/vb-net-data-access-sf-10 32/38
Binding ListBox
• Example: Bind Customer Table’s CID field
to a listbox.
– Create a Adapter for Customer table , andgenerate the dataset.
– Add ListBox and set binding properties:
• Data Source: Customer table• Display Member: Field to display in the listbox.
• Value Member: the actual values for items in the list
box.
8/10/2019 Vb Net Data Access Sf 10
http://slidepdf.com/reader/full/vb-net-data-access-sf-10 33/38
Display Selected Record
• Bound textbox (same data source as the listbox): – If the Listbox and the textbox are bound to the same
BindingSource object, the textbox will automatically
displays the record of the selected listbox item.
• Unbound textbox – To display the ValueMember
• Textbox1.text = ListBox1.SelectedValue
– To display other fields:
• Textbox1.text = ListBox1.SelectedItem(“Cname”) • Can we use TextBox1.text=ListBox1.SelectedItem?
No!
8/10/2019 Vb Net Data Access Sf 10
http://slidepdf.com/reader/full/vb-net-data-access-sf-10 34/38
ListBox SelectedItem Property
• How to display the selected record in unboundtextbox?
• After binding to a data source, this property return aDataRowView object.
• What is DataRowView? – Object Browser:
• System.Data
– DataRowView: Item property is the default property
• To retrieve a column from a DataRowView object(use 0-based index to identity a column):• ListBox1.SelectedItem.Item(1)
• Or: ListBox1.SelectedItem(1)
• Or: ListBox1.SelectedItem(“Cname”)
8/10/2019 Vb Net Data Access Sf 10
http://slidepdf.com/reader/full/vb-net-data-access-sf-10 35/38
Using Object Browser
• View/Object Browser
• DataSet object model:
• System.Data
– DataSet
• Relations
• Tables
– Rows
– Columns
• Use Object Browser to study object’s properties,methods. Ex. System.Data.OleDbDataAdapter
8/10/2019 Vb Net Data Access Sf 10
http://slidepdf.com/reader/full/vb-net-data-access-sf-10 36/38
8/10/2019 Vb Net Data Access Sf 10
http://slidepdf.com/reader/full/vb-net-data-access-sf-10 37/38
DataSet and Data Adapter
• DataSet Object: A DataSet object can hold severaltables and relationships between tables.
• DataAdapter: This the object used to pass data between the database and the dataset.
– It is an object with SQL Select, Insert, Update andDelete commands.
i / hild i
8/10/2019 Vb Net Data Access Sf 10
http://slidepdf.com/reader/full/vb-net-data-access-sf-10 38/38
Creating Parent/Child Form Using
DataAdapter Wizard
• Add two DataAdapters to the form: one for the parenttable and one for the child table.
• Use the parent table’s DataAdapter to generate a
dataset; then use the child table’s DataAdapter to
generate the child table into the same dataset.
• Open dataset’s designer and right-click the parent table
and choose Add/Relation to add a relation.
• Add and bind textboxes to the parent table; adddataGridView and bind its datasource property to the
relation.
• Add navigation button’s using BindingSource object.
Recommended