data access
DESCRIPTION
Data Access. Tonga Institute of Higher Education. Introduction. Many programs work with large amounts of data Databases are an good place to store data Programs need a way to connect to, get data from and save data to databases - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Data Access](https://reader036.vdocuments.net/reader036/viewer/2022070503/56815a64550346895dc7a985/html5/thumbnails/1.jpg)
Data Access
Tonga Institute of Higher Education
![Page 2: Data Access](https://reader036.vdocuments.net/reader036/viewer/2022070503/56815a64550346895dc7a985/html5/thumbnails/2.jpg)
Introduction Many programs work with large amounts
of data Databases are an good place to store data Programs need a way to connect to, get
data from and save data to databases ActiveX Data Objects .Net (ADO .Net)
provides way to access data from Object Linking and Embedding Database (OLE DB)-compliant data sources such as SQL Server and Oracle.
![Page 3: Data Access](https://reader036.vdocuments.net/reader036/viewer/2022070503/56815a64550346895dc7a985/html5/thumbnails/3.jpg)
ADO .Net Components
.Net Data Providers – Objects designed for connecting to a database, executing commands and retrieving resultsConnection Command DataReader DataAdapter
![Page 4: Data Access](https://reader036.vdocuments.net/reader036/viewer/2022070503/56815a64550346895dc7a985/html5/thumbnails/4.jpg)
Connection Object
Connection Object – Provides connectivity to a data source
1. SQLConnection object – Allows connection to a Microsoft SQL Server 7.0 or later database Located in System.Data.SQLClient namespace
2. OLEDbConnection object – Allows connection to an OLE DB data source Located in System.Data.OleDb namespace Microsoft Access is an OLE DB compliant database
![Page 5: Data Access](https://reader036.vdocuments.net/reader036/viewer/2022070503/56815a64550346895dc7a985/html5/thumbnails/5.jpg)
Connection Strings The SQLConnection and OLEDbConnection objects both
use connection strings to connect to a data source Connection String – Provides information needed to
connect to a database You do not need to include everything in a connection
string. Only include what is needed. SQLConnection object
SQL Server Initial Catalog=pubs;Data Source=(local);User ID=sa;password=;
OLEDbConnection object Oracle
Provider=msdaora;Data Source=MyOracleDB;User ID=myUsername;Password=myPassword;
Microsoft Access Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\northwind.mdb
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\tihe1\student.mdb
You don’t alwaysneed a User IDand Password
Local DB
Network DB
![Page 6: Data Access](https://reader036.vdocuments.net/reader036/viewer/2022070503/56815a64550346895dc7a985/html5/thumbnails/6.jpg)
Opening and Closing Connections
Before a Connection object can work with a database, the connection must be openedUse the Open() method
After the Connection object is used, the connection must be closedUse the Close() method
![Page 7: Data Access](https://reader036.vdocuments.net/reader036/viewer/2022070503/56815a64550346895dc7a985/html5/thumbnails/7.jpg)
Command Object
Command Object – Enables the execution of queries and return of data from a data source A Connection object must be specified to connect to a
data source A SQL statement must be specified for execution at
the data source There are different ways to execute SQL statements
ExecuteNonQuery() – Use this when data will not be returned
ExecuteReader() – Use this when data will be returned
![Page 8: Data Access](https://reader036.vdocuments.net/reader036/viewer/2022070503/56815a64550346895dc7a985/html5/thumbnails/8.jpg)
ExecuteNonQuery Code
ExecuteNonQuery doesnot return anything
![Page 9: Data Access](https://reader036.vdocuments.net/reader036/viewer/2022070503/56815a64550346895dc7a985/html5/thumbnails/9.jpg)
Demonstration
Connections, Commands and ExecuteNonQuery
![Page 10: Data Access](https://reader036.vdocuments.net/reader036/viewer/2022070503/56815a64550346895dc7a985/html5/thumbnails/10.jpg)
DataReader Object DataReader Object – Provides a stream of data
from the data source Useful members
FieldCount Get<DataType>
GetByte GetString And more
HasItems Item Read
![Page 11: Data Access](https://reader036.vdocuments.net/reader036/viewer/2022070503/56815a64550346895dc7a985/html5/thumbnails/11.jpg)
DataReader and ExecuteReader Code
Using a DataReaderto display data fromdatabase
Keeps loopinguntil all rowsare read
Gets string from first index
ExecuteReader returnsA DataReader
![Page 12: Data Access](https://reader036.vdocuments.net/reader036/viewer/2022070503/56815a64550346895dc7a985/html5/thumbnails/12.jpg)
Demonstration
Connections, Commands and DataReader.ExecuteReader Code
![Page 13: Data Access](https://reader036.vdocuments.net/reader036/viewer/2022070503/56815a64550346895dc7a985/html5/thumbnails/13.jpg)
DataSet Object DataSet –An in-memory database Useful members (* covered later)
Clear Tables*
DataSet
![Page 14: Data Access](https://reader036.vdocuments.net/reader036/viewer/2022070503/56815a64550346895dc7a985/html5/thumbnails/14.jpg)
DataTable Object A DataTable object is an in-memory database table. It contains:
Rows and columns of data Primary key, foreign key, constraint and relation information about the
data Useful members (* covered later)
ChildRelations* Clear Columns* Constraints* DataSet NewRow ParentRelations* PrimaryKey TableName
DataTables
![Page 15: Data Access](https://reader036.vdocuments.net/reader036/viewer/2022070503/56815a64550346895dc7a985/html5/thumbnails/15.jpg)
DataColumn Object A DataColumn object represents a table column Useful members
AllowDBNull AutoIncrement AutoIncrementSeed AutoIncrementStep Caption ColumnName DataType DefaultValue MaxLength Ordinal Table Unique
DataColumn
![Page 16: Data Access](https://reader036.vdocuments.net/reader036/viewer/2022070503/56815a64550346895dc7a985/html5/thumbnails/16.jpg)
DataRow Object A DataRow Object represents a row of data in a table Useful members
Item Table
DataRow
![Page 17: Data Access](https://reader036.vdocuments.net/reader036/viewer/2022070503/56815a64550346895dc7a985/html5/thumbnails/17.jpg)
Demonstration
DataSets, DataTables, DataColumns and DataRows
![Page 18: Data Access](https://reader036.vdocuments.net/reader036/viewer/2022070503/56815a64550346895dc7a985/html5/thumbnails/18.jpg)
DataTableCollection
A DataTableCollection contains DataTables Access the DataTableCollection of a DataSet by
using the Tables property Useful members
Add Count Clear Item Remove
![Page 19: Data Access](https://reader036.vdocuments.net/reader036/viewer/2022070503/56815a64550346895dc7a985/html5/thumbnails/19.jpg)
Demonstration
DataTableCollection
![Page 20: Data Access](https://reader036.vdocuments.net/reader036/viewer/2022070503/56815a64550346895dc7a985/html5/thumbnails/20.jpg)
DataColumnCollection A DataColumnCollection contains DataColumns Access the DataColumnCollection of a
DataTable by using the Columns property Useful members
Add Count Clear Item Remove
![Page 21: Data Access](https://reader036.vdocuments.net/reader036/viewer/2022070503/56815a64550346895dc7a985/html5/thumbnails/21.jpg)
Demonstration
DataColumnCollection
![Page 22: Data Access](https://reader036.vdocuments.net/reader036/viewer/2022070503/56815a64550346895dc7a985/html5/thumbnails/22.jpg)
DataRowCollection A DataRowCollection contains DataRows Access the DataRowCollection of a DataTable
by using the Rows property Useful members
Add Count Clear Item Remove
![Page 23: Data Access](https://reader036.vdocuments.net/reader036/viewer/2022070503/56815a64550346895dc7a985/html5/thumbnails/23.jpg)
Demonstration
DataRowCollection
![Page 24: Data Access](https://reader036.vdocuments.net/reader036/viewer/2022070503/56815a64550346895dc7a985/html5/thumbnails/24.jpg)
DataAdapter Object
DataAdapter – Used to retrieve data from a database and put it in a DataSet. Can also send changes from the DataSet to the database
Useful membersFillUpdate
![Page 25: Data Access](https://reader036.vdocuments.net/reader036/viewer/2022070503/56815a64550346895dc7a985/html5/thumbnails/25.jpg)
DataAdapter Code
The Fill method opens the Connection if the connection is not already open. If Fill opened the connection, it will also close the connection.
![Page 26: Data Access](https://reader036.vdocuments.net/reader036/viewer/2022070503/56815a64550346895dc7a985/html5/thumbnails/26.jpg)
Demonstration
DataAdapter Object
![Page 27: Data Access](https://reader036.vdocuments.net/reader036/viewer/2022070503/56815a64550346895dc7a985/html5/thumbnails/27.jpg)
Programmatically Creating DataSet Objects DataSets, DataTables, DataRows and
DataColumns are all objects We can create and customize all of these
objects
![Page 28: Data Access](https://reader036.vdocuments.net/reader036/viewer/2022070503/56815a64550346895dc7a985/html5/thumbnails/28.jpg)
Programmatically Creating DataSet Objects Code
![Page 29: Data Access](https://reader036.vdocuments.net/reader036/viewer/2022070503/56815a64550346895dc7a985/html5/thumbnails/29.jpg)
Demonstration
Programmatically Creating DataSet Objects
![Page 30: Data Access](https://reader036.vdocuments.net/reader036/viewer/2022070503/56815a64550346895dc7a985/html5/thumbnails/30.jpg)
DBNull, Nothing and “” There is a difference between having
No DataAnd “”
In a database, use null when you have no data In VB.Net
Use “” when you have an empty string Use nothing when there is no data in a variable Use dbnull when there is no data in a database field
![Page 31: Data Access](https://reader036.vdocuments.net/reader036/viewer/2022070503/56815a64550346895dc7a985/html5/thumbnails/31.jpg)
Demonstration
DBNull, Nothing and “”
![Page 32: Data Access](https://reader036.vdocuments.net/reader036/viewer/2022070503/56815a64550346895dc7a985/html5/thumbnails/32.jpg)
Storing Single Quotation Marks in a Database
We often store SQL queries in a stringDim SQL as String = “Insert into Students
(FirstName) values (‘Tevita’)” SQL queries use single quotation marks To store a single quote in a database, use
the single quote twiceDim SQL as String = “Insert into Students
(FirstName) values (‘Ma’’afu’)”
![Page 33: Data Access](https://reader036.vdocuments.net/reader036/viewer/2022070503/56815a64550346895dc7a985/html5/thumbnails/33.jpg)
Demonstration
Storing Single Quotation Marks in a Database
![Page 34: Data Access](https://reader036.vdocuments.net/reader036/viewer/2022070503/56815a64550346895dc7a985/html5/thumbnails/34.jpg)
Data Access Exceptions Often, errors occur when databases are used.
The name of the file is changed The location of a file is incorrect The program does not have the proper username and password
to access the database Common Data Access Exceptions
DataException - Exception that is thrown when errors are generated using ADO.NET components.
ConstraintException - Exception that is thrown when attempting an action that violates a constraint.
NoNullAllowedException - Exception that is thrown when attempting to insert a null value into a column where AllowDBNull is set to false.
ReadOnlyException - Exception that is thrown when attempting to change the value of a read-only column.
![Page 35: Data Access](https://reader036.vdocuments.net/reader036/viewer/2022070503/56815a64550346895dc7a985/html5/thumbnails/35.jpg)
Demonstration
Data Access Exceptions