0% found this document useful (0 votes)
47 views69 pages

Unit 5

The document provides an overview of ADO.NET, including its architecture and core components. ADO.NET is a set of classes that provides data access functionality for client-server and distributed applications. It allows applications to connect to data sources, execute commands, retrieve data using a DataReader or populate a DataSet using a DataAdapter, and then disconnect from the data source. The core components of ADO.NET are connections, commands, data readers, data adapters, and data sets.

Uploaded by

dawngliani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views69 pages

Unit 5

The document provides an overview of ADO.NET, including its architecture and core components. ADO.NET is a set of classes that provides data access functionality for client-server and distributed applications. It allows applications to connect to data sources, execute commands, retrieve data using a DataReader or populate a DataSet using a DataAdapter, and then disconnect from the data source. The core components of ADO.NET are connections, commands, data readers, data adapters, and data sets.

Uploaded by

dawngliani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 69

UNIT 5

INTRODUCTION TO DATA ACCESS


OVERVIEW OF ADO.NET
 ADO.NET stands for ActiveX Data Object is a database access
technology created by Microsoft as part of its .NET framework that
can access any kind of data source.
 It’s a set of object-oriented classes that provides a rich set of data
components to create high-performance, reliable and scalable
database applications for client- server applications as well as
distributed environments over the Internet and intranets.
 In the ADO.NET model, unlike ADO (in connected state) and
previous data access technologies applications connect to the data
sources when they are reading or updating the data. After that the
connection closes.
 This is important because in client- server or distributed
applications, having connection resources open all the time is one of
the most resource- consuming parts.
Advantages of ADO .NET

Single Object- Oriented API


The ADO.NET classes are easy to use
Managed Code
Deployment
XML Support
Visual Data Components
Performance and Scalability
Connections and Disconnected data
ADO.NET ARCHITECTURE
The ADO.NET is designed to work with
multiple kinds of data sources in same
fashion.
You can categorize ADO.NET components
in three categories: disconnected, common or
shared and the .NET data providers.
The disconnected components build the basic
ADO.NET architecture. You can use these
components (or classes) with or without data
providers
COMPONENT OF ADO.NET
Data provider
A data provider is a set of components, such as
Connection, Command, DataAdapter and DataReader.
The Connection is the first component that talks to a
data source.
A Command object executes a SQL query and stored
procedures to read, add, update, and delete data of a
data source via a DataAdapter.
A DataAdapter is a bridge between a dataset and the
connection. It uses Command Object to execute SQL
queries and stored procedures.
Connection Object
A connection sets a link between a data
source and ADO.NET.
A Connection object sits between a data
source and a DataAdapter (via Command).
Connection can also be connected to a
Command object to execute SQL queries,
which can be used to retrieve, add, update and
delete data to a data source.
 The Connection object also plays a useful
role in creating a transaction.
Command object
The Command object can execute SQL
queries and stored procedures.
You can execute SQL queries to return data
in a DataSet or a DataReader object.
To retrieve add, update and delete data you
use SELECT, INSERT, UPDATE, and
DELETE SQL queries.
A DataAdapter generated using the VS .NET
Integrated development Environment (IDE)
has these queries.
DataAdapter
 The DataAdapter object serves as a conduit between the
data source and the Dataset.
 The DataAdapter knows about the DataSet and it knows
how to populate it.
 The Adapter also knows about the connection to the
data source.
DataReader
 Data reader is used to retrieve data from a data source
in a read-only and forward-only mode.
 The DataReader are created only by calling the
ExecuteReader method of a Command Object.
DataSet
The DataSet consists of a collection of tables, rows,
columns and relationships.
The DataSet contains a collection of DataTables and
the DataTable contains a collection of DataRows,
DataRelations, and DataColumns.
A DataTable maps to a table in the database. The
previous DataSet contains a DataTable that maps to
the Orders table because you filled it with a SELECT
query executed on the Order table.
 A DataTable in the DataSet consists of a collection of
DataRow.
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.
The data provider in ADO.Net consists of
the following four objects:
 CONNECTION
 COMMAND
 DATA ADAPTER
 DATA READER
CONNECTION
The Connection Object connects to the
specified database and open a connection
between the application and the Database.
The following are the commonly use
connections in the ADO.NET
(1) SqlConnection
(2) OleDbConnection
(3) OdbcConnection
(1) SqlConnection
You can connect your VB.Net application to data in a SQL Server database
using the Microsoft .NET Framework Data Provider for SQL Server. You must
import System.Data.SqlClient before making SQL connection.

Import system.data.sqlclient
Public sub main()
Dim connectionString As String
Dim cnn As SqlConnection
connectionString = "Data Source=ServerName;Initial
Catalog=DatabaseName; User ID=UserName;Password=Password"
cnn = New SqlConnection(connectionString)
Try
cnn.Open()
MsgBox.show("Connection Open!")
cnn.Close()
Catch ex As Exception
MsgBox.show("Cannot open connection!")
End Try
End Sub
(2) OleDbConnection
An instance of the OleDbConnection class in .NET Framework is
supported the OLEDB Data Provider.
Imports System.Data.OleDb
Public sub main()
Dim connectionString As String
Dim cnn As OleDbConnection
connetionString="Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=sample.accdb;"
cnn = New OleDbConnection(connectionString)
Try
cnn.Open()
MsgBox.show("Connection Open ! ")
cnn.Close()
Catch ex As Exception
MsgBox.show("Cannot open connection ! ")
End Try
End Sub
(3) OdbcConnection
An instance of the OdbcConnection class in .NET
Framework is supported the ODBC Data Provider.
Imports System.Data.Odbc
Public sub main()
Dim connectionString As String
Dim cnn As OdbcConnection
connectionString="Driver={Microsoft Access
Driver(*.mdb)};DBQ=sample.mdb;"
cnn = New OdbcConnection(connectionString)
Try
cnn.Open()
MsgBox.show("Connection Open ! ")
cnn.Close()
Catch ex As Exception
MsgBox.show("Cannot open connection ! ")
End Try
End Sub
COMMAND
A command is a SQL statement or a stored procedure used to retrieve,
insert, delete or modify data in a data source.
Dim sqlConnection1 As New SqlConnection("Your Connection
String")
Dim cmd As New SqlCommand
Dim reader As SqlDataReader
cmd.CommandText = "SELECT * FROM Customers"
cmd.CommandType = CommandType.Text
cmd.Connection = sqlConnection1
sqlConnection1.Open()
reader = cmd.ExecuteReader()
sqlConnection1.Close()
The different execute commands are describe below:
(1) ExecuteNonQuery
ExecuteNonQuery() is used for executing statements that do not return
result set. ExecuteNonQuery() performs Data Definition tasks as well
as Data Manipulation tasks.
(2) ExecuteReader
ExecuteReader() in SqlCommand Object send the SQL statements to
Connection Object and populate a SqlDataReader Object based on the
SQL statement. It is mainly use to get number of rows from the
database table.
(3) ExecuteScalar
ExecuteScalar() in SqlCommand Object is used for get a single value
from Database after its execution. If the Result Set contains more than
one columns or rows , it takes only the first column of first row, all
other values will ignore.
DATAREADER
Data reader is used to retrieve data from a data source in a read-only and forward-
only mode.
The two types of DataReaders are:
(1) SqlDataReader
SqlDataReader Object provides a connection oriented data access to the SQL
Server data Sources. ExecuteReader() in the SqlCommand Object send the SQL
statements to SqlConnection Object and populate a SqlDataReader Object based
on the SQL statement.
Dim sqlReader As SqlDataReader = sqlCmd.ExecuteReader()
SqlDataReader.Read()

(2) OleDbDataReader
OleDbDataReader Object provides a connection oriented data access to the
OLEDB Data Sources. ExecuteReader() in the OleDbCommand Object send the
SQL statements to OleDbConnection Object and populate an OleDbDataReader
Object based on the SQL statement.
Dim oledbReader As OleDbDataReader = oledbCmd.ExecuteReader()
OleDbDataReader.Read()
Sample Code for SQL Data Reader
Imports System.Data.SqlClient
Module Module1
Sub Main()
Dim str As String = "Data Source=.;uid=sa; pwd=123;database=master"
Dim con As New SqlConnection(str)
Try
con.Open()
Dim sql As String = "SELECT * FROM emp;"
Dim cmd As New SqlCommand(sql, con)
Dim myreader As SqlDataReader = cmd.ExecuteReader()
Console.WriteLine("Firstname & lastname ")
Console.WriteLine("=============================")
While myreader.Read()
Console.Write(myreader("Firstname").ToString() & ", ")
Console.Write(myreader("Lastname").ToString() & ", ")
Console.WriteLine("")
End While
Catch ex As SqlException
Console.WriteLine("Error: " & ex.ToString())
End Try
End Sub
End Module
Sample Code for OLEDB Data Reader
Dim myConnection As OleDbConnection = New
OleDbConnection(connString)
' Open connection
myConnection.Open()
Dim cmd As OleDbCommand = New OleDbCommand(cmdString,
myConnection)
cmd.CommandType = CommandType.Text
Dim TheDataReader As OleDbDataReader = cmd.ExecuteReader()
While TheDataReader.Read()
Console.Write(TheDataReader("ContactID").ToString())
Console.Write(" ")
Console.Write(TheDataReader("FirstName").ToString())
Console.Write(" ")
Console.Write(TheDataReader("LastName").ToString())
Console.WriteLine()
End While
Catch ae As OleDbException
MsgBox.show(ae.Message())
End Try
DATAADAPTER
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. The different DataAdapters are:
(1) SqlDataAdapter
SqlDataAdapter provides the communication between the Dataset and
the SQL database. We can use SqlDataAdapter Object in combination
with Dataset Object. It resides in the System.Data.SqlClient namespace.
Dim adapter As New SqlDataAdapter
(2) OleDbDataAdapter
OleDbDataAdapter provides the communication between the Dataset and
the OleDb Data Sources. We can use OleDbDataAdapter Object in
combination with Dataset Object. It resides in the System.Data.OleDb
namespace.
Dim oledbAdapter As OleDbDataAdapter
Sample Code for SQL Data Adapter
Private Sub SqlDataAdapter_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
String ConnectionString = "Integrated Security = SSPI;" +
"Initial catalog = Northwind;" + " Data Source =MAIN-SERVER; "
Dim SQL As String = "SELECT CustomerID, CompanyName FROM
Customers"
Dim conn As SqlConnection = New SqlConnection(ConnectionString)
' open the connection
conn.Open()
'Create a SqlDataAdapter object
Dim adapter As SqlDataAdapter = New SqlDataAdapter(SQL, conn)
' Call DataAdapter's Fill method to fill data from the
' Data Adapter to the DataSet
Dim ds As DataSet = New DataSet("Customers")
adapter.Fill(ds)
' Bind data set to a DataGrid control
dataGrid1.DataSource = ds.DefaultViewManager
End Sub
Sample Code for OLEDB Data Adapter
Private Sub OleDbDataAdapter_Click(ByVal sender As Object, ByVal Args As
System.Event)
'Create a connection object
Dim ConnectionString As String = "provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source= C:/northwind.mdb"
Dim SQL As String = "SELECT * FROM Orders"
Dim conn As OleDbConnection = New OleDbConnection(ConnectionString)
' open the connection
conn.Open()
' Create an OleDbDataAdapter object
Dim adapter As OleDbDataAdapter = New OleDbDataAdapter()
adapter.SelectCommand = New OleDbCommand(SQL, conn)
' Create Data Set object
Dim ds As DataSet = New DataSet("orders")
' Call DataAdapter's Fill method to fill data from the DataAdapter to the
DataSet
adapter.Fill(ds)
dataGrid1.DataSource = ds.DefaultViewManager
End Sub
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.
Components of DataSet
1. DataTable
2. DataTableCollection
3. DataRelation
4. DataRelationCollection
5. ExtendedProperties
6. DataRow
7. DataRowCollection
8. DataView
9. Primary Key
10. Data Column
11. DataColumnCollection
1. DataTable
The DataTable type stores data in memory. It is often used alongside
SQL databases. DataTable has columns and rows properties. DataTable
is an in-memory representation of structured data.

Imports System.Data Function GetTable() As DataTable


Module Module1 ' Create new DataTable instance.
Sub Main() Dim table As New DataTable
' Declare data table variable. ' Create 3 typed columns in the DataTable.
Dim table As DataTable = GetTable() table.Columns.Add("Sl. No", GetType(Integer))
Console.WriteLine("COLUMNS: {0}", table.Columns.Add("SName", GetType(String))
table.Columns.Count) table.Columns.Add("Address",
Console.WriteLine("ROWS: {0}", GetType(String))
table.Rows.Count) ' Add five rows in the DataTable.
Console.ReadLine() table.Rows.Add(1, "Sanga", "Ramthar")
End Sub table.Rows.Add(2, "Mawia", "Venglai")
table.Rows.Add(3, "Remi", "Bawngkawn")
table.Rows.Add(4, "Liani", "Leitan")
table.Rows.Add(5, "Dinga", "Ramhlun")
Return table
End Function
End Module
The output is shown as:
COLUMNS: 3
ROWS: 5
2. DataTableCollection
The DataTableCollection contains all the DataTable objects for a particular DataSet. The
DataTableCollection uses methods such as Add, Clear, and Remove
Class Example
Private Sub GetTables(ByVal dataSet As DataSet)
For Each table As DataTable In dataSet.Tables
For Each row As DataRow In table.Rows
For Each column As DataColumn In table.Columns
If row(column) IsNot Nothing Then Console.WriteLine(row(column))
Next
Next
Next
End Sub
Private Sub CreateTable(ByVal dataSet As DataSet)
Dim newTable As DataTable = New DataTable("table")
newTable.Columns.Add("ID", GetType(Integer))
newTable.Columns.Add("Name", GetType(String))
dataSet.Tables.Add(newTable)
End Sub
End Class
3. DataRelation
A DataRelation is used to relate two DataTable objects to each other
through DataColumn objects. Relationships are created between
matching columns in the parent and child tables. That is, the DataType
value for both columns must be identical.

Private Sub CreateRelation()


Dim parentColumn As DataColumn =
DataSet1.Tables("Customers").Columns("CustID")
Dim childColumn As DataColumn =
DataSet1.Tables("Orders").Columns("CustID")
Dim relCustOrder As DataRelation
relCustOrder = New DataRelation("CustomersOrders",
parentColumn, childColumn)
DataSet1.Relations.Add(relCustOrder)
End Sub
4. DataRelationCollection
It contains relationships and the links between tables in a data set.
DataRelationCollection object containing null or multiple DataRelation
objects which establish a parent/child relation between two DataTable
objects.
Sl No Name Description
1 Add(Data Column, Creates a DataRelation with a specified parent
DataColumn) and child column, and adds it to the collection.
2 Add(DataRelation) Adds a DataRelation to the
DataRelationCollection.
3 Clear() Clears the collection of any relations.
4 Contains(String) Verifies whether a DataRelation with the
specific name (case insensitive) exists in the
collection.
5 Equals(Object) Determines whether the specified object is
equal to the current object.
(Inherited from Object)
6 IndexOf(DataRelation) Gets the index of the specified DataRelation
object.
6 Remove(DataRelation) Removes the specified relation from the
collection.
5. ExtendedProperties
The ExtendedProperties property enables you to store
custom information with the DataSet. For example, you
might store a time when the data should be refreshed.

Private Sub SetProperty(ByVal column As DataColumn)


column.ExtendedProperties.Add("TimeStamp", DateTime.Now)
End Sub
Private Sub GetProperty(ByVal column As DataColumn)
Console.WriteLine(column.ExtendedProperties("TimeStamp").To
String())
End Sub
End Class
6. DataRow
It represents a row in the DataTable. The DataRow object
and its properties and methods are used to retrieve,
evaluate, insert, delete, and update values in the DataTable.
Module Module1 Function GetTable() As DataTable
Sub Main() ' Create new DataTable instance.
Dim table As DataTable = GetTable() Dim table As New DataTable
' First row. ' Create 3 typed columns in the DataTable.
Dim row1 As DataRow = table.Columns.Add("Sl. No",
table.Rows(0) GetType(Integer))
Console.WriteLine(row1("SName")) table.Columns.Add("SName",
' Last row. GetType(String))
Dim row2 As DataRow = table.Columns.Add("Address",
table.Rows(table.Rows.Count - 1) GetType(String))
Console.WriteLine(row2("SName")) ' Add five rows in the DataTable.
End Sub table.Rows.Add(1, "Sanga", "Ramthar")
table.Rows.Add(2, "Mawia", "Venglai")
table.Rows.Add(3, "Remi", "Bawngkawn")
table.Rows.Add(4, "Liani", "Leitan")
table.Rows.Add(5, "Dinga", "Ramhlun")
7. DataRowCollection
It contains all the rows in a DataTable. While the DataColumnCollection
defines the schema of the table, the DataRowCollection contains the actual data
for the table, where each DataRow in the DataRowCollection represents a
single row.

Class Example
Private Sub ShowRows(ByVal table As DataTable)
Console.WriteLine(table.Rows.Count)
For Each row As DataRow In table.Rows
Console.WriteLine(row(1))
Next
End Sub
Private Sub AddRow(ByVal table As DataTable)
Dim rowCollection As DataRowCollection = table.Rows
Dim newRow As DataRow = table.NewRow()
table.Rows.Add(newRow)
End Sub
End Class
8. DataView
It represents a fixed customized view of a DataTable for sorting, filtering, searching,
editing and navigation. The DataView provides different views of the data stored in a
DataTable. The DataView class is typically used for sorting, filtering, searching, editing,
and navigating the data from a DataSet. A DataView is bindable, meaning it can be bound
to controls in the same way that the DataSet can be bound to controls.

Imports System.Data.SqlClient SqlConnection(connetionString)


Public Class Form1Private Sub Try
Button1_Click(ByVal sender As connection.Open()
System.Object, ByVal e As command = New SqlCommand(sql,
System.EventArgs) Handles Button1.Click connection)
Dim connetionString As String adapter.SelectCommand = command
Dim connection As SqlConnection adapter.Fill(ds, "Create DataView")
Dim command As SqlCommand adapter.Dispose()
Dim adapter As New SqlDataAdapter command.Dispose()
Dim ds As New DataSet connection.Close()
Dim dv As DataView dv = ds.Tables(0).DefaultView
Dim sql As String DataGridView1.DataSource = dv
connetionString = "Data Catch ex As Exception
Source=ServerName;Initial Catalog=
MsgBox(ex.ToString)
DatabaseName;User
ID=UserName;Password=Password" End Try
sql = "Select * from product" End Sub
connection = New End Class
9. PrimaryKey
It represents the column that uniquely identifies a row in a DataTable.
The PRIMARY KEY constraint uniquely identifies each record in a
table. Primary keys must contain UNIQUE values, and cannot contain
NULL values.
The following SQL creates a PRIMARY KEY on the "ID" column
when the "Persons" table is created:

CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (ID)
);
10. DataColumn
DataColumn represents a column in the DataTable. It consists of the
number of columns that comprise a DataTable. The DataColumn is the
fundamental building block for creating the schema of a DataTable.

Private Sub MakeTable()


Dim table As DataTable = New DataTable("Product")
Dim column As DataColumn = New DataColumn()
column.DataType = System.Type.[GetType]("System.Decimal")
column.AllowDBNull = False
column.Caption = "Price"
column.ColumnName = "Price"
column.DefaultValue = 25
table.Columns.Add(column)
Dim row As DataRow
For i As Integer = 0 To 10 - 1
row = table.NewRow()
row("Price") = i + 1
table.Rows.Add(row)
Next
End Sub
11. DataColumnCollection
It represents all the columns in a DataTable. The
DataColumnCollection defines the schema of a DataTable, and
determines what kind of data each DataColumn can contain. You can
access the DataColumnCollection through the Columns property of the
DataTable object.
Private Sub PrintDataTableColumnInfo(ByVal table As
DataTable)
Dim columns As DataColumnCollection = table.Columns

For Each column As DataColumn In columns


Console.WriteLine(column.ColumnName)
Console.WriteLine(column.DataType)
Next
End Sub
CONNECTING TO A DATABASE
The step by step method for using visual tools
for data access or data form wizard is as follows.
Select TOOLS -> Connect to Database. This
will open database connection wizard.

You can also directly link from the control


Add a DataGridView on the form.
Click on the Choose Data Source combo box and
Click on the Add Project Data Source link.
select Database as the data source type.
Choose DataSet as the database model.
Choose the connection already set up
Select a server name and the database name
in the Add Connection dialog box.
Click on the Test Connection button to
check if the connection succeeded.
Save the connection string.
Choose the database object, Customers table in our
example, and click the Finish button.
CREATE REPORT USING REPORT
WIZARD.
Reports are summary of table from
database.
We can Create Reports, Print and Print
Preview of reports from Reports Wizard.
It is a step by step method that
automatically generates and adds report to
your project.
 To add a new report definition file using the
Report Wizard, from the Project menu, select
Add New Item and then choose Report Wizard.
 In Name, select the name of the database and
then click Add. A graphical design surface opens
behind the dialog box.
 In the Dataset Properties page, in the Data
source drop-down list, select the DataSet you
created.
 The Available datasets box is automatically
updated with the DataTable you created.
 Click Next.In the Arrange Fields page and you
like and then click Next twice, then click Finish.
Add the ReportViewer control to
your form.
 From the View menu, choose Designer.
 From the Reporting section of the Toolbox, drag the
ReportViewer control to the form.
 Open the smart tags panel of the ReportViewer1
control by clicking the smart-tag glyph on the top right
corner. Click the Choose Report drop-down list and
select your report file.rdlc.
 From the smart tags panel, click Dock in parent
container.
After you run the source code you will
get the report like this.
Data Binding with Windows Forms
and ADO.NET
Windows Forms allow you to bind easily to
almost any structure that contains data.
The user can bind values to the respective
controls in ADO.NET. Depending on the type of
binding offered, they are distinguished as
follows:
1. Simple data binding allows you to bind a control to a single
data element. You use this type of data binding for controls that
show only one value. Uses of simple data binding include
binding data to text boxes and labels.
2. Complex data binding allows you to bind more than one
data element to a control. Controls that support complex data
binding include data grid controls, combo boxes, and list boxes.
DATA BINDING WITH TEXTBOX
 Textbox control can be used to display data that is already stored in
the databases. Consider an example to display the result of the
students in an examination. The details are added in the following
format.
 Create a Windows Form Application in Visual Studio .NET. The
following customized format is created for user.

 Once the design of the form is created, select the View option from
the menu bar. Click on the Properties window.
 Select the first text box and the properties for it appear in the
window.
 Expand the DataBindings property
 Select the Text property for enabling the drop down list.
 Click the Add Project Data Source from the drop
down list
 Make a connection with the CurrentInfo database and
select the Student table
 Select the Other Data Sources, Project Data Sources,
CurrentInfoDataSet, Student table.
 Select the Name column and bind it with the textbox.
 Bind all the other text boxes with the database values.
 Press F5 and execute the Windows Form.
 The following output is displayed to the user.
DATA BINDING WITH DATAGRID
CONTROL
Design Time:
 Create a Windows Form Application in Visual
Studio .NET. Drag the Datagridview control to the form.
The following customized format is created for user.

 Once the design of the form is created. Click on the data


grid view control. Click the Properties window.
 Click datasource property and select the binding source.
 Click data member from the property window and select
the table.
 Press F5 and execute the Windows Form.
 Table from the selected database will be displayed.
Imports System.Data.OleDb
Public Class Form1
Public da As OleDbDataAdapter = New
OleDbDataAdapter("select * from mytable",
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\
HP\OneDrive\Documents\mydatabase.accdb")
Public ds As New DataSet
Private Sub Form1_Load(sender As System.Object, e As
System.EventArgs) Handles MyBase.Load
da.Fill(ds)
DataGridView1.DataSource = ds.Tables(0)
End Sub
End Class
When the above code is executed and run
using Start button available at the Microsoft Visual
Studio tool bar, it will show the following window:
Clicking the Fill button displays the table on
the data grid view control:
BINDING SOURCE NAVIGATOR

A BindingNavigator control is used for handling


the binding to the data source through the
pointer to the current item in the list of records.
The navigator control is used with the
BindingSource control for enabling the users to
navigate the data records on a form.
It provides a layer between the controls and
windows form of the data source.
Users can navigate and modify the records in
the Windows form.
The following figure displays the
BindingNavigator control and the BindingSource
control in the Windows Form.
The Binding Navigator control has many controls for
modifying the data source. The list of controls and their
functions are mentioned below:
 bindingNavigatorAddNewItem Button: The + sign
indicates that the new row can be added to the data
source.
 bindingNavigatorDeleteItem Button: The X sign
indicates that the current row can be deleted from the
data source.
 bindingNavigatorMoveFirstItem Button: The button
indicates that the user can move to the first item in the
data source.
 bindingNavigatorMoveLastItem Button: The button
indicates that the user can move to the last item in the
data source
 bindingNavigatorMoveNextItem Button: The button
indicates that the user can move to the next item in the
data source
 bindingNavigatorMovePreviousItem Button: The button
indicates that the user can move to the previous item in
the data source
 bindingNavigatorPositionItem textbox: The returns
current position in the data source
 bindingNavigatorCountItemText box: The is used to
return the total number of items in the data source.
Consider the Order details table containing the data about
the orders to be added. The data is organized into a format
as shown below:
 Open Visual studio application and add Windows Forms
Application from the template pane.
 Add the labels and a binding Navigator control, and
textbox controls to the form
 Click OK button
 Click View, Properties Window, and open the Properties
Window.
 Add the appropriate names to the controls present in the
web form.
 Open the Data Source Configuration Wizard. The
Database icon must be selected. Click Next Button
 Click New Connection Button. Add Connection dialog
box is shown.
 Add the Server Name, select Use SQL
Server Authentication option from the Log on the server
section
 Add the User name as sa and password as abcd1234
 Select the Order Details database and click Test
Connection button
 Click OK and close the Add Connection dialog box
 Click Next button. In the Choose Your Database Objects
dialog box, expand Tables node.
 Select the Orderdata table and click Finish button.
For binding the data to the control in the Windows
Form, the following steps are executed.
 Select the textbox1, and expand the DataBindings
property.
 Select the Text property and click on the drop down
list.
 Expand the Other Data Sources, Project Data,
Sources, Orderdataset, Orderdata nodes.
 Select the OrderID column and bind it with
textbox1.
 Perform the similar operations for all the textboxes.
 Select binding source property from bindingsource
navigator, select the binding source in the list
Press F5 or click Debug -> Start Debugging option
from the menu. The Order Details form is
displayed as shown below:
END OF SLIDE

You might also like