Vous êtes sur la page 1sur 3

Connection Object

*****************
The ADO Connection Object is used to create an open connection to a data source.
Through this connection, you can access and manipulate a database.

If you want to access a database multiple times, you should establish a connection
using the Connection object. You can also make a connection to a database by
passing a connection string via a Command or Recordset object. However, this type
of connection is only good for one specific, single query.

Command Object
**************
The ADO Command object is used to execute a single query against a database. The
query can perform actions like creating, adding, retrieving, deleting or updating
records.

If the query is used to retrieve data, the data will be returned as a RecordSet
object. This means that the retrieved data can be manipulated by properties,
collections, methods, and events of the Recordset object.

The major feature of the Command object is the ability to use stored queries and
procedures with parameters.

Data Adapter
************
In ADO.NET, a DataAdapter functions as a bridge between a data source, and a
disconnected data class, such as a DataSet. At the simplest level it will specify
SQL commands that provide elementary CRUD functionality. At a more advanced level
it offers all the functions required in order to create Strongly Typed DataSets,
including DataRelations. Data adapters are an integral part of ADO.NET managed
providers, which are the set of objects used to communicate between a data source
and a dataset. (In addition to adapters, managed providers include connection
objects, data reader objects, and command objects.) Adapters are used to exchange
data between a data source and a dataset. In many applications, this means reading
data from a database into a dataset, and then writing changed data from the dataset
back to the database. However, a data adapter can move data between any source and
a dataset. For example, there could be an adapter that moves data between a
Microsoft Exchange server and a dataset.

Datasets
********
The DataSet is a complex and very powerful object that is actually an equivalent to
"in-memory representation" of a database.
It can contain DataTables that correlate to database tables while DataTables can
contain a series of DataColumns that define the composition of each DataRow.
As you guess, the DataRow correlates to a row in a database table.
In addition you can establish relationships between DataTables within the DataSet
in the same way that a database has relationships between tables.

Data Reader
***********
When you have connected to a database and queried it, you need to access the result
set and for that purpose you use data reader. Data readers are fast, unbuffered,
forward-only, read-only connected stream that retrieve data on a per-row basis and
implement the System.Data.IDataReader interface.
Using the DataReader object, you are getting as close to the raw data as possible
in ADO.NET, because you do not have to populate a DataSet object, which sometimes
may be expensive if the DataSet contains a lot of data.

******************************

Private Sub bAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles bAdd.Click

Try
Dim sqlconn As New OleDb.OleDbConnection
Dim sqlquery As New OleDb.OleDbCommand
Dim connString As String
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Visual
Studio 2010\Projects\Invent\Invent\mdbInvent.accdb"
sqlconn.ConnectionString = connString
sqlquery.Connection = sqlconn
sqlconn.Open()
sqlquery.CommandText = "INSERT INTO tblUsers(Username,
Password)VALUES(@Username, @Password)"
sqlquery.Parameters.AddWithValue("@Username", txtUname.Text)
sqlquery.Parameters.AddWithValue("@Password", txtPass.Text)
sqlquery.ExecuteNonQuery()
sqlconn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

End Sub

**********************************************
Imports System.Data.OleDb

Public Class LoginForm1


Dim con As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
Dim cmd As New OleDbCommand
Dim adapter As New OleDbDataAdapter
Dim data As OleDbDataReader

Private Sub OK_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles OK.Click
dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
dbSource = "Data Source = C:\projects\TUITION\DATABASE\TUITION.mdb"
con.ConnectionString = dbProvider & dbSource
con.Open()
cmd.Connection = con
cmd.CommandText = "select * from login where username ='" &
UsernameTextBox.Text & "'"
adapter.SelectCommand = cmd
data = cmd.ExecuteReader
'MsgBox(data.Read)
If data.Read = True Then
If data(1).ToString = PasswordTextBox.Text Then
MsgBox("Successfully logged in")
MDIParent1.Show()
con.Close()
Me.Hide()
Else
MsgBox("Password incorrect")
End If
Else
MsgBox("User name incorrect")
End If
con.Close()
End Sub

*************************
Filling datagrid view with datasets
Imports System.Data.OleDb

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click

Dim connString As String = "PROVIDER=Microsoft.Jet.OLEDB.4.0; Data


Source=C:\projects\TUITION\DATABASE\TUITION.mdb"
Dim MyConn As OleDbConnection
Dim da As OleDbDataAdapter
Dim ds As DataSet
Dim tables As DataTableCollection
Dim source1 As New BindingSource

MyConn = New OleDbConnection


MyConn.ConnectionString = connString
ds = New DataSet
tables = ds.Tables
da = New OleDbDataAdapter("Select * from STUDENT", MyConn)
da.Fill(ds, "TUITION")
Dim view As New DataView(tables(0))
source1.DataSource = view
DataGridView1.DataSource = view
End Sub

Vous aimerez peut-être aussi