0% found this document useful (0 votes)
69 views

Bca Chapter 10 - Ado - Net With Database

The document discusses: 1) Data access in ADO.Net including accessing data from SQL, XML, and text files using classes like Connection, Command, DataReader, and DataAdapter. 2) SQL commands for creating tables, inserting, selecting, deleting, and updating data. 3) Key differences between ADO and ADO.Net including working in disconnected environments and storing data in XML format. 4) Namespaces and classes used in ADO.Net including System.Data and classes like Connection, Command, DataReader, and DataAdapter. 5) Steps to bind data to a DataGrid control and code samples for performing CRUD operations on a database using ADO.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

Bca Chapter 10 - Ado - Net With Database

The document discusses: 1) Data access in ADO.Net including accessing data from SQL, XML, and text files using classes like Connection, Command, DataReader, and DataAdapter. 2) SQL commands for creating tables, inserting, selecting, deleting, and updating data. 3) Key differences between ADO and ADO.Net including working in disconnected environments and storing data in XML format. 4) Namespaces and classes used in ADO.Net including System.Data and classes like Connection, Command, DataReader, and DataAdapter. 5) Steps to bind data to a DataGrid control and code samples for performing CRUD operations on a database using ADO.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Chapter - 10

Data Access with ADO.Net


Introduction
• ADO – Active Data Objects
• ADO.Net can access data from SQL, OLEDB,
XML, Text files and Hierarchical Files
• Database : collection of related information
that is stored with determination
• Relational Database : It is a DB with relations,
relations can be viewed as set of records or
tuples
Data Types in SQL – Refer Table in pg 10.3
• Numeric
• Integer
• Decimal
• Smallint
• Char
• Varchar
• Date
• Time
• Timestamp
SQL Commands
• Table Creation :
– CREATE TABLE table_name(Col_name datatype,….);
– Eg : CREATE TABLE stud(sno INTEGER, sname CHAR(10));

• Insertion:
– INSERT INTO table_name VALUES (value1,value2…);
– Eg : INSERT INTO stud VALUES(101,’rani’);
INSERT INTO stud VALUES(102,’mala’);
• Displaying :
– SELECT * FROM table_name;
– Eg : SELECT * FROM stud;

• Deleting :
– DELETE FROM table_name WHERE conditions;
– Eg : DELETE FROM stud WHERE sno=101;

• Modifying:
– UPDATE table_name SET col_names=new_values WHERE
conditions;
– Eg : UPDATE stud SET sno=103, sname=‘priya’ WHERE sno=102
Special Features of ADO.Net
• Usage of XML to transmit data
• Improved performance and maintenance
S.No ADO ADO.Net

1. Works in connected Environment Works in Disconnected Environment

2. Record Set – has only single table Record Set – retrieves data from multiple tables

3. Sequential Access is only available Any kind of Access – can move to any record
dynamically
4. Client Side cursor only - Both Client and Server Side cursors –
MoveNext() method MoveNext(), MovePrev(), MoveFirst(),
MoveLast() methods
5. Based on COM – Component Obj Based on CLR – Common Lang Runtime
Modeling
6. Stores data in Binary Format Stores data in XML format (parsing of data)

7. Can’t have multiple transactions Can have multiple transactions


Name Spaces for ADO.Net
(Ref table in pg 10.14)
• System.Data
• System.Data.Common
• System.Data.OleDb
• System.Data.SqlClient
• System.Data.SqlTypes
Four Classes of ADO.Net
1. Connection
– OleDbConnection, SqlConnection
- Eg:
Dim strconn As String =
"Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=C:\bca\bcadb.accdb"
Dim myconn As OleDbConnection = New
OleDbConnection(strconn)
Four Classes of ADO.Net …..
2. Command - OleDbCommand, SqlCommand
– 4 types
ExecuteReader
ExecuteNonQuery
ExceuteScalar
ExecuteXmlReader
-Eg : Dim mycmd As OleDbCommand = New
OleDbCommand("Select * from student", myconn)
• Commands Types
• 1. ExecuteReader – to select records from table
Eg: Dim myr as New OleDbReader
myr = mycmd.ExecuteReader()
Textbox1.Text = myr(“sname”)
• 2. ExecuteScalar – returns data as scalar values
Eg: Object o = mycmd.ExecuteScalar()
Textbox1.Text = myr(“sname”)
• 3. ExecuteNonQuery – affects the state of the table
Eg: Dim qry, myqry as String
qry=“Insert into stud values(101,’kala’)”
myqry = New OleDbCommand(qry,myconn)
myqry.ExecuteNonQuery()
• 4. ExecuteXmlReader – returns data as xml text data
Eg: Imports System.Xml
mydaset.WriteXml(“D:\sample.xml”)
mydaset.ReadXml(“D:\sample.xml”)
Four Classes of ADO.Net …..
3. DataReader – OleDbDataReader, SqlDataReader
-2 options to access records - DataReader, DataSet
Eg: Dim myr as New OleDbReader
myr = mycmd.ExecuteReader()
Textbox1.Text = myr(“sname”)
4. DataAdapter – OleDbDataAdapter, SqlDataAdapter
- 2 options to access records - DataReader, DataSet
- Eg : Dim myadap As OleDbDataAdapter = New
OleDbDataAdapter(mycmd)
Dim mydaset as New DataSet()
myadap.Fill(mydaset, "student")
DataSet, DataTable, DataRow, DataColumn, DataRelation can also
be used
DataGrid…..
Steps :
• Place DataGridView from Data tab of Toolbox
• In the Top right Corner of the Control – select the small arrow…
• Follow the steps of the wizard (write all the steps u follow)
Coding…..
• Imports System.Data
• Dim strconn As String
• Dim myconn As OleDbConnection
• Dim mycmd As OleDbCommand
• Dim myadap As New OleDbDataAdapter()
• Dim mydset As New DataSet()
• Dim maxrows, sel, temp As Integer
Form_Load()
strconn = "Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=C:\bca\dbpgm\dbpgm\bcadb.accdb"
myconn = New OleDbConnection(strconn)
mycmd = New OleDbCommand("select * from student order by
studno", myconn)
myadap = New OleDbDataAdapter(mycmd)
myadap.Fill(mydset, "student")
maxrows = mydset.Tables("student").Rows.Count
combodata()  coding in next slide
temp = 0
mycmd.Dispose()
myconn.Close()
Public Function combodata() As Boolean
If temp = 1 Then
maxrows = maxrows - 1
End If
MsgBox("combodata fn" & maxrows)
For i = 0 To maxrows Step 1 '' to add items to the
combobox from database
comboBox1.Items.Add(mydset.Tables(0).Rows(i).Item(0).ToString())
Next i
temp = 0
Return 1
End Function
ComboBox1_SelectedIndexChanged

sel = ComboBox1.SelectedIndex

TextBox1.Text = mydset.Tables(0).Rows(sel).Item(0).ToString()

TextBox2.Text = mydset.Tables(0).Rows(sel).Item(1).ToString()

TextBox3.Text = mydset.Tables(0).Rows(sel).Item(2).ToString()

TextBox4.Text = mydset.Tables(0).Rows(sel).Item(3).ToString()

TextBox5.Text = mydset.Tables(0).Rows(sel).Item(4).ToString()

TextBox6.Text = Integer.Parse(TextBox3.Text) +

Integer.Parse(TextBox4.Text) + Integer.Parse(TextBox5.Text)
XML & ADO.Net
• Imports System.Data
• Imports System.Xml
• Imports System.Io

• Dim strconn As String


• Dim myconn As OleDbConnection
• Dim mycmd As OleDbCommand
• Dim myadap As New OleDbDataAdapter()
• Dim mydset As New DataSet()
Button1_Click (to Write in Xml File)
strconn = "Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=C:\bca\dbpgm\dbpgm\bcadb.accdb"
myconn = New OleDbConnection(strconn)
mycmd = New OleDbCommand("select * from student ", myconn)
myadap = New OleDbDataAdapter(mycmd)
myadap.Fill(mydset, "student")
Mydaset.WriteXml(“d:\bca\studxml.xml”)
Button2_Click (to read from Xml File)
mydaset.ReadXml(“d:\bca\studxml.xml”)
DataGridView1.DataSource = mydaset
After t2

You might also like