Visual Basic Database Access
Download
Report
Transcript Visual Basic Database Access
Introduction to ADO.Net and Visual
Studio Database Tools
ISYS 350
Database Processing
• Querying database
• Updating database:
– Insertion, deletion, modification
Steps to Retrieve Data
• Establishes a connection to the database.
• Executes query commands against the
database.
– SQL Select commands
• Process the returned data.
Steps to Update Data
• Establishes a connection to the database.
• Executes update commands against the
database.
– SQL Insert, Delete, Update commands
• Receive confirmation of completion.
ADO.Net Classes and Data Consumer
Ado.Net
Adapter
Connection
Dataset
Data Consumer
Command
WinForm
DataReader
WebForm
ADO.NET Objects
• Connection Object: Represent a connection to the
database.
• Command Object: The command object allows us
to 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.
SQL Server Express LocalDB
• SQL Server Express LocalDB is a lightweight version of SQL Server Express that is
already installed with VS 2012, and it does not
bring heavy workload on development
machines
Lab: Creating a LocalDB Database
• 1. Start SQL Server Express LocalDB:
– View/SQL Server Object Explorer
– Click and open: SQL Server
Lab: Creating a LocalDB Database
• Database Name: MyHR
• Table:
– Employee
•
•
•
•
•
EID: NCHAR(5)
Ename: NCHAR(20)
Sex: NCHAR(1)
Salary: Numeric(9,2)
HireDate: Datetime
To Add a New Database
• Open the LocalDB node
• Right-click Databases
– Add new database
• Enter database name
– Example: MyHR
To Add a New Table: Employee
• Open the Database node: Open the MyHR node
• Right-click Tables and select: Add New Table
• Change the table name to Employee
– CREATE TABLE [dbo].[Employee]
• Define fields
• Click Update:
– click Update Database
To Add Records
• Open the Tables node
• Right-click the table name (Employee) and
select View Data
To Modify Table Design
• Point and right-click the table name:
– Select: View/Designer
• Change the CREATE TABLE command
• Or Change the design with the designer
Data Binding
• Connect a control or property to one or more data
elements.
• Simple binding: Use simple binding to display a
field 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.
Generating Data Bound Form with
Form Wizard
• Creating a form with ADO.Net objects and databound controls to display and update information in
a dataset.
• Step 1: Add a connection to the LocalDB database.
• Step 2: Add a new data source:
• Step 3: Select controls for table fields:
– Click the dropdown list next to the table’s name:
• Datagrid view
• Details
• Step 4: Drag the table to form.
Add a Connection to LocalDB Database
• Tools/Connect to database
– Click Change
– Click Microsoft SQL Server:
• Enter Server Name:
– (localdb)\Projects
– Or, (localdb)\v11.0
• Log on to server:
– Using Windows authentication
• Select or enter a database
name
– Test Connection
Add a New Data Source
• Data Source window:
– View/Other windows/Data Source
• Add New Data Source
–
–
–
–
From Database
From Dataset
Select connection
Select tables
Data Bound Form Examples
• Using the LocalDB database MyHR, show
records of the Employee table:
– with dataGridView
– with detail view
Items Added to the Form
• Table Adapter: click smart tag
– Add query
– Preview data
• Dataset:
• Binding Source: It is an object that keeps track of
position (the current row) of a data source.
– Preview data
Other Data Form Demos
• DataGrid View
• Add /Modify/Delete records.
• Read only form:
– Delete AddNew, Delete, Save buttons from
navigator bar.
Detail Form with Bound ListBox
• Example: Employee table form with EID listbox
and displays selected employee information in
textboxes.
– Choose detail view for the Employee table.
– Click the dropdown list next to the EID field and click
ListBox
– Drag the Employee table to the form.
– Bind the EID field to the BindingSource:
• Activate the Property window and click the listbox
• Set the DataSOurce property to BindingSource
• Set the Display Member property to EID
Add an Dependents Table to the MyHR
Database
• Dependents table:
–
–
–
–
•
•
•
•
•
•
DependentID: NCHAR(5)
DepName: NCHAR(20)
DepRelationship: NCHAR(15) ----- Spouse/Son/Daughter
EID: NCHAR(5)
Open the Database node: Open the MyHR node
Right-click Tables and select: Add New Table
Change the table name to Dependents
CREATE TABLE [dbo].[Dependents]
Define fields
Click Update; then click Update Database
Hierarchical Forms:
Employee/Dependents
• 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 uses
dataGrid view
– Click Dataset object’s smart tag to choose Edit in
Dataset Designer
– With the designer, right click the parent table and
choose Add/Relation
– Change dataGrid’s DataSource property to the relation.