Title: Introduction%20to%20ADO.Net,%20VB.Net%20Database%20Tools%20and%20Data%20Binding
1Introduction to ADO.Net, VB.Net Database Tools
and Data Binding
2Client/Server Structure
- a. Presentation user interface
- Menus, forms, reports, etc
- b. Processing logic
- Business rules
- c. Database
SQL queries
Client
Database Server
Results
3Database Processing
- Querying database
- Updating database
- Insertion, deletion, modification
4Steps to Retrieve Data
- Establishes a connection to the database.
- Executes commands against the database.
- Store data results.
5A Simplified View of ADO.Net Objects
Ado.Net
Data Provider
Data Consumer
Adapter
Dataset
WinForm
Connection
Command
WebForm
Reader
6ADO.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.
7Data Providers
- ODBC Provider
- Open Database Connectivity
- A driver manager
- Used for relational databases
- OLE DB Provider
- OLE DB interfaces provide applications with
uniform access to data stored in diverse
information sources, or data stores. - Access
- SQL Server Provider
- Oracle Provider
- Microsofts .NET data provider for Oracle
- Connecting to Oracle, the Server name is XE
8Using ODBC
- Windows XP
- Control Panel /Administrative Tools/DataSource(ODB
C) - Three types of data source names
- User DSN usable only by you and only on the
machine currently using. - System DSN Any one using the machine can use.
- File DSN Can be copied and used by other
computers with the same driver installed. - Demo
- Excel Data/Import
- Access File/Get External Data/Import, then
select ODBC data source
9Connecting to Database
- Adding connection
- Tools/Connect to database
- Data Source
- ODBC
- OLD DB (Access)
- SQL Server
- Oracle, etc.
- Server Explorer
- Data connections Right click data connection
- Add Connection
- Tables, Views
- Create new SQL Server Database
10Adding Data Source
- Data/Add New Data Source
- Database
- Using the connections
- Service
- Object
- LINQ To SQL Server
11Working with SQL Server
- Starting SQL Server
- Control Panel/Administrative Tools/Services/SQLSer
ver - SQL Server database file extension mdf
- Default database folder testSQL08.mdf
- C\Program Files\Microsoft SQL Server\MSSQL.1\MSSQ
L\Data\testSQL08.mdf
12Creating SQL Server Database
- From Server Explorer, right click data connection
and choose - Create new SQL Server Database
- Server name
- LocalServerName\SQLExpress
- Add new table Right click Tables and choose Add
New Table - Add rows Right click the table name and choose
Show table data. - Note After creating the database, you may create
an ODBC DSN to connect to it.
13How to create ADO.Net objects?
- Automatically generated when creating data bound
form. - Form wizard
- Using Data Adapter Wizard
- Using code
- Example
- dim strConn as string "ProviderMicrosoft.Jet.OLE
DB.4.0Data Source c\sales2k.mdb" - dim objConn as new OledbConnection(strConn)
- objConn.open()
14Data 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 controls Data Source and Data
Member to bind the data.
15Creating 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 tables name
- Datagrid view
- Details
- Drag the table to form.
16Items 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
17Generated Code
Private Sub CUSTOMERBindingNavigatorSaveItem_Click
(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles CUSTOMERBindingNavigator
SaveItem.Click Me.Validate()
Me.CUSTOMERBindingSource.EndEdit()
Me.CUSTOMERTableAdapter.Update(Me.SalesDBDataSet.C
USTOMER) 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.CUSTOMERTableAdap
ter.Fill(Me.SalesDBDataSet.CUSTOMER) End Sub
18Other Data Form Demos
- DataGrid View
- Add /Modify/Delete records.
- Read only form
- Delete AddNew, Delete, Save buttons from
navigator bar.
19Hierarchical 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 uses dataGrid view - RightClick Dataset object to choose Edit in
Dataset Designer - With the designer, right click the parent table
and choose Add/Relation - Change dataGrids DataSource property to the
relation.
20Detail Form with Bound ListBox
- Example Customer table form with CID listbox and
displays selected customer information in
textboxes. - 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
21Creating A Database Application Without
Programming
- Creating a database application to display
information and update database. - A main form with buttons to open data forms
- DisplayInfo
- Enter New
- Modify
- Exit