Beruflich Dokumente
Kultur Dokumente
ADO.NET OVERVIEW
TAKEN BY,
M.STEPHEN,MCA.
Introduction to
Microsoft ADO.NET
Part1: Overview
Overview of ADO.NET
What is ADO.NET?
Using Namespaces
What is a DataSet?
What is ADO.NET?
ADO.NET provides a set of classes for working with
data. ADO.NET provides:
Using Namespaces
System.Data
System.Data
System.Data.SqlClient
System.Data.SqlClient
System.Data;
System.Data;
System.Data.SqlClient;
System.Data.SqlClient;
System.Data
System.Data.SqlClient
System.Data.OleDb
DataSet
DataTable
SqlDataAdapter
OleDbDataAdapter
OLE DB .NET
Data Provider
OleDbConnection
SqlConnection
OLEDB sources
(SQL Server 6.5)
What is a Dataset?
DataSet
DataTable
DataTable
DataTable
SqlDataAdapter
SqlConnection
OleDbConnectio
n
OleDb Database
3.
33
4.
44
5.
55
6.
66
7.
77
Database
SqlConnection
SqlDataAdapter
Client
Client
DataSet
UpdateCommand
InsertCommand
DeleteCommand
Command
Command
Command
Command
Command
Command
DataReader
DataReader
Command
Command
Connection
Connection
sp_SELECT
sp_UPDATE
sp_INSERT
Database
sp_DELETE
Generating a DataSet
through the UI
Creates a DataSet that allows you to access
data as an object
or through code
Dim
Dim ds
ds As
As New
New DataSet()
DataSet()
DataSet
DataSet ds
ds == new
new DataSet();
DataSet();
DropDownList
DataGrid
ListBox
DataList
CheckBoxList
Repeater
RadioButtonList
DataSource
DataSource
DataMember
DataMember
DataTextField
DataTextField
DataValueField
DataValueField
Description
Description
The
TheDataSet
DataSet containing
containingthe
thedata
data
The
TheDataTable
DataTableininthe
theDataSet
DataSet
The
Thefield
fieldininthe
the DataTable
DataTable that
thatisisdisplayed
displayed
The
Thefield
fieldininthe
the DataTable
DataTable that
thatbecomes
becomesthe
the
value
valueofofthe
theselected
selecteditem
itemininthe
thelist
list
Overview
Connecting to a Database
DataSet
DataReader
Read-only
Disconnected
Connected
Forward-only
Slower access
Faster access
Mixed
Mixedmode
mode
authentication
authentication
or
or
Client
Windows
Windowsonly
only
authentication
authentication
Web
WebServer
Server
Windows
Windowsauthentication
authentication
Using SqlConnection
Dim
Dim con
con as
as new
new connection()
connection()
Con.connectionstring=database=master;server=ORION;
Con.connectionstring=database=master;server=ORION;
Uid=sample;pwd=sample;
Uid=sample;pwd=sample;
Con.open()
Con.open()
Data Base
Server
User ID
Password
Provider
Creating a DataAdapter
Creating a DataSet
Using a DataView
Handling Errors
Creating a DataAdapter
da.SelectCommand.CommandText;
da.SelectCommand.CommandText;
Set the InsertCommand,
UpdateCommand, and DeleteCommand
da.SelectCommand.Connection;
da.SelectCommand.Connection;
properties if needed
Creating a DataSet
DataSet
DataSet ds
ds == new
new DataSet();
DataSet();
da.Fill(ds,
da.Fill(ds, "Authors");
"Authors");
Dim
Dim ds
ds As
As New
New DataSet()
DataSet()
da.Fill(ds,
da.Fill(ds, "Authors")
"Authors")
Access a DataTable
ds.Tables["Authors"].Rows.Count;
ds.Tables["Authors"].Rows.Count;
string
string str="";
str="";
foreach(DataRow
foreach(DataRow rr in
in
ds.Tables["Authors"].Rows)
ds.Tables["Authors"].Rows)
{{
str
str +=
+= r[2];
r[2];
str
+=
r["au_lname"];
str += r["au_lname"];
}}
ds.Tables("Authors").Rows.Count
ds.Tables("Authors").Rows.Count
Dim
Dim rr As
As DataRow
DataRow
Dim
str
As
Dim str As String
String
For
Each
r
in
For Each r in __
ds.Tables("Authors").Rows
ds.Tables("Authors").Rows
str
str &=
&= r(2)
r(2)
str
str &=
&= r("au_lname")
r("au_lname")
Next
Next
Using a DataView
dg.DataSource
dg.DataSource == ds;
ds;
dg.DataMember
=
"Authors";
dg.DataMember = "Authors";
dg.DataBind()
dg.DataBind();;
Handling Errors
Login failed
Code Example
Creating Relationships
conn1
conn2
DataSet
Orders
Creating Relationships
Dim
Dim parentCol
parentCol As
As DataColumn
DataColumn == __
ds.Tables("Customers").Columns("CustomerID")
ds.Tables("Customers").Columns("CustomerID")
Dim
Dim childCol
childCol As
As DataColumn
DataColumn == __
ds.Tables("Orders").Columns("CustomerID")
ds.Tables("Orders").Columns("CustomerID")
Create DataRelation
parentCol
Dim
Dim dr
dr As
As New
New DataRelation
DataRelation __
("name",
("name", parentCol,
parentCol, __
childCol)
childCol)
ds.DataRelations.Add(dr)
ds.DataRelations.Add(dr)
Customers table
DataSet
DataRelation
childCol
Orders table
Orders
Customers
GetChildRows
DataSet
GetParentRow
DataView
DataRowView
CreateChildView
DataSet
Orders
What is a DataReader?
Creating a DataReader
What is a DataReader?
Forward-only, read-only
Creating a DataReader
To use a DataReader:
1.1
2.2
3.3
4.4
44
55
5.5
66
6.
77
7.
Code Example
Access fields
Parameter is the ordinal position or name of the field
Get functions give best performance
Do
Do While
While myReader.Read()
myReader.Read()
str
&=
str &= myReader(1)
myReader(1)
str
str &=
&= myReader("field")
myReader("field")
str
&=
myReader.GetDateTime(2)
str &= myReader.GetDateTime(2)
Loop
Loop
while
while (myReader.Read())
(myReader.Read())
{{
str
str +=
+= myReader[1];
myReader[1];
str
+=
myReader["field"];
str += myReader["field"];
str
str +=
+= myReader.GetDateTime(2);
myReader.GetDateTime(2);
}}
<asp:DataGrid
<asp:DataGrid id="dgAuthors"
id="dgAuthors" runat="server"
runat="server" />
/>
Bind to a DataReader
dgAuthors.DataSource
dgAuthors.DataSource == dr
dr
dgAuthors.DataBind()
dgAuthors.DataBind()
dgAuthors.DataSource
dgAuthors.DataSource == dr;
dr;
dgAuthors.DataBind();
dgAuthors.DataBind();
Using Parameters
Input
Output
InputOutput
ReturnValue
Code Examples
ds
ds == new
new DataSet();
DataSet();
da.Fill(ds);
da.Fill(ds);
iTotal
iTotal == da.Parameters("@ItemCount").Value
da.Parameters("@ItemCount").Value
iTotal
iTotal == da.Parameters("@ItemCount").Value;
da.Parameters("@ItemCount").Value;
THANK YOU