Вы находитесь на странице: 1из 22

COLLEGE OF COMPUTER STUDIES

PREPARED BY: P. FERNANDEZ, A.K. RACELIS


Computer Programming
2
VISUAL BASIC - DATABASE
Database
Applications communicate with a database,
firstly, to retrieve the data stored there and present it in a user-friendly way,
secondly, to update the database by inserting, modifying and deleting data.
Microsoft ActiveX Data Objects.Net (ADO.Net) is a model, a part of the .Net
framework that is used by the .Net applications for retrieving, accessing and
updating data.
ADO.Net Object Model
ADO.Net object model is
nothing but the structured
process flow through various
components.
ADO.Net Object Model
The data residing in a data store or database is retrieved through the data
provider. Various components of the data provider retrieve data for the
application and update data.
An application accesses data either through a dataset or a data reader.
Datasets store data in a disconnected cache and the application retrieves data
from it.
Data readers provide data to the application in a read-only and forward-only
mode.
Data Provider
A data provider is used for connecting to a database, executing commands and
retrieving data, storing it in a dataset, reading the retrieved data and updating the
database.
S.N Objects & Description
1 Connection
This component is used to set up a connection with a data source.
2 Command
A command is a SQL statement or a stored procedure used to retrieve,
insert, delete or modify data in a data source.
Data Provider
S.N Objects & Description
3 DataReader
Data reader is used to retrieve data from a data source in a read-only and
forward-only mode.
4 DataAdapter
This is integral to the working of ADO.Net since data is transferred to and
from a database through a data adapter. It retrieves data from a database
into a dataset and updates the database. When changes are made to the
dataset, the changes in the database are actually done by the data adapter.
Data Provider
There are following different types of data providers included in ADO.Net
The .Net Framework data provider for SQL Server - provides access to Microsoft SQL
Server.
The .Net Framework data provider for OLE DB - provides access to data sources
exposed by using OLE DB.
The .Net Framework data provider for ODBC - provides access to data sources exposed
by ODBC.
The .Net Framework data provider for Oracle - provides access to Oracle data source.
The EntityClient provider - enables accessing data through Entity Data Model (EDM)
applications.
DataSet
DataSet is an in-memory representation of data. It is a disconnected, cached set
of records that are retrieved from a database.
When a connection is established with the database, the data adapter creates a
dataset and stores data in it.
After the data is retrieved and stored in a dataset, the connection with the
database is closed.
This is called the 'disconnected architecture'. The dataset works as a virtual
database containing tables, rows, and columns.
DataSet
The following diagram
shows the dataset
object model:
Connecting to a Database
The .Net Framework provides two types of Connection classes:
SqlConnection - designed for connecting to Microsoft SQL Server.
OleDbConnection - designed for connecting to a wide range of databases,
like Microsoft Access and Oracle.
MySQL database
MySQL is a leading open source database management system.
It is a multi user, multithreaded database management system.
MySQL is especially popular on the web. It is one of the parts of the very
popular LAMP platform consisting of Linux, Apache, MySQL, and PHP.
Currently MySQL is owned by Oracle. MySQL database is available on most
important OS platforms. It runs on BSD Unix, Linux, Windows, or Mac OS.
Wikipedia and YouTube use MySQL.
These sites manage millions of queries each day.
MySQL comes in two versions: MySQL server system and MySQL embedded
system.
MySQL database
The Connection, Command, DataReader, DataSet, and DataProvider are the core
elements of the .NET data provider model.
The Connection creates a connection to a specific data source.
The Command object executes an SQL statement against a data source.
The DataReader reads streams of data from a data source.
The DataSet object is used for offline work with a mass of data. It is a
disconnected data representation that can hold data from a variety of
different sources.
MySQL database
Both DataReader and DataSetare used to work with data; they are used under
different circumstances.
If you only need to read the results of a query, the DataReader is the better
choice.
If you need more extensive processing of data, or we want to bind
a Winforms control to a database table, the DataSet is preferred.
MySQL Connector/NET
Before we begin, to use MySQL with VB.NET you will need to download the
MySQL Connector/NET which is available from
http://dev.mysql.com.../connector/net/.
The installer will install the connector on your system ready for use.
Once the connector is installed, start a new project in VB.NET and you will
then need to add a reference to the connector.
MySQL Connector/NET
Choose "Add
Reference" from the
Project menu, then
select "Browse" and
browse to the
installation folder
where the connector
was installed,
choose
"MySQL.Data.dll" .
MySQL Connector/NET
Choose "Add
Reference" from the
Project menu, then
select "Browse" and
browse to the
installation folder
where the connector
was installed,
choose
"MySQL.Data.dll" .
Connecting
We connect to the database and get some info about the MySQL server.
Imports MySql.Data.MySqlClient

We import the elements of the MySQL data provider.


Dim cs As String = "Database=testdb;Data Source=localhost;" _ & "User
Id=testuser;Password=test623"
Connecting
This is the connection string. It is used by the data provider to establish a
connection to the database. We specify the database name, host, user name and
password.
Dim conn As New MySqlConnection(cs)
A MySQLConnection object is created.
This object is used to open a connection to a database.
conn.Open()
Example
Imports MySql.Data.MySqlClient

Module Example

Sub Main()
Dim cs As String = "Database=testdb;Data Source=localhost;" _
& "User Id=testuser;Password=test623"

Dim stm As String = "SELECT VERSION()"


Dim version As String
Dim conn As MySqlConnection
Try
Example conn = New MySqlConnection(cs)
conn.Open()

Dim cmd As MySqlCommand = New MySqlCommand(stm, conn)

version = Convert.ToString(cmd.ExecuteScalar())
Console.WriteLine("MySQL version: {0}", version)
Catch ex As MySqlException
Console.WriteLine("Error: " & ex.ToString())
Finally
conn.Close()
End Try

End Sub
End Module
References:
1. https://www.tutorialspoint.com/vb.net/vb.net_database_access.h
tm
2. http://zetcode.com/db/mysqlvb/
3. http://www.dreamincode.net/forums/topic/115753-use-vbnet-to-
connect-to-mysql/

Вам также может понравиться