introduction to ado.net and visual studio database tools

23
Introduction to ADO.Net and Visual Studio Database Tools ISYS 350

Upload: calais

Post on 15-Jan-2016

99 views

Category:

Documents


1 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Introduction to ADO.Net and Visual Studio Database Tools

Introduction to ADO.Net and Visual Studio Database Tools

ISYS 350

Page 2: Introduction to ADO.Net and Visual Studio Database Tools

Database Processing

• Querying database

• Updating database:– Insertion, deletion, modification

Page 3: Introduction to ADO.Net and Visual Studio Database Tools

Steps to Retrieve Data

• Establishes a connection to the database.

• Executes query commands against the database.– SQL Select commands

• Process the returned data.

Page 4: Introduction to ADO.Net and Visual Studio Database Tools

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.

Page 5: Introduction to ADO.Net and Visual Studio Database Tools

ADO.Net Classes and Data Consumer

Ado.Net

Connection

Adapter

Command

DataReader

Dataset Data Consumer

WinForm

WebForm

Page 6: Introduction to ADO.Net and Visual Studio Database Tools

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.

Page 7: Introduction to ADO.Net and Visual Studio Database Tools

SQL Server Express LocalDB

• SQL Server Express LocalDB is a light-weight version of SQL Server Express that is already installed with VS 2012, and it does not bring heavy workload on development machines

Page 8: Introduction to ADO.Net and Visual Studio Database Tools

Lab: Creating a LocalDB Database

• 1. Start SQL Server Express LocalDB:– View/SQL Server Object Explorer– Click and open: SQL Server

Page 9: Introduction to ADO.Net and Visual Studio Database Tools

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

Page 10: Introduction to ADO.Net and Visual Studio Database Tools

To Add a New Database

• Open the LocalDB node

• Right-click Databases– Add new database

• Enter database name– Example: MyHR

Page 11: Introduction to ADO.Net and Visual Studio Database Tools

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

Page 12: Introduction to ADO.Net and Visual Studio Database Tools

To Add Records

• Open the Tables node

• Right-click the table name (Employee) and select View Data

Page 13: Introduction to ADO.Net and Visual Studio Database Tools

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

Page 14: Introduction to ADO.Net and Visual Studio Database Tools

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.

Page 15: Introduction to ADO.Net and Visual Studio Database Tools

Generating Data Bound Form with Form Wizard

• Creating a form with ADO.Net objects and data-bound 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.

Page 16: Introduction to ADO.Net and Visual Studio Database Tools

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

Page 17: Introduction to ADO.Net and Visual Studio Database Tools

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

Page 18: Introduction to ADO.Net and Visual Studio Database Tools

Data Bound Form Examples

• Using the LocalDB database MyHR, show records of the Employee table:– with dataGridView– with detail view

Page 19: Introduction to ADO.Net and Visual Studio Database Tools

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

Page 20: Introduction to ADO.Net and Visual Studio Database Tools

Other Data Form Demos

• DataGrid View

• Add /Modify/Delete records.

• Read only form: – Delete AddNew, Delete, Save buttons from

navigator bar.

Page 21: Introduction to ADO.Net and Visual Studio Database Tools

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

Page 22: Introduction to ADO.Net and Visual Studio Database Tools

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

Page 23: Introduction to ADO.Net and Visual Studio Database Tools

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.