Introduction To
Introduction To
Introduction To
NET
2.0
Agenda
Plain Talk
.NET Framework Data Provider
Operation without Connection to a Database
Applications
Plain Talk
Categories of Objects
Connected Objects:
Connecting a database
Read-only record set
Forward-only stream
Disconnected Objects:
No connection to a database
Off-line operation
Sorting, Searching, Filtering, Modifying
ADO.NET Architecture
Parameters UpdateCommand
ConstraintCollection
DeleteCommand
DataReader
DataRelationCollection
DB XML
Connected Objects –
Connection Object
A Connection for communicating with a
Database
As an ADO Connection Object
Connected Objects –
Command Object
Query
Execute a Stored Procedure
Any SQL Command
As an ADO Command Object
Connected Objects –
DataReader Object
Data Row is Read-Only
Keep one row only
Forward-only stream
Connected Objects –
DataAdapter Object
A Bridge for Disconnected Objects
One DataAdapter mapping to one DataTable
Disconnected Objects –
DataTable Object
Retrieving data from the Rows property
As an ADO Recordset Object
Disconnected Objects –
DataSet Object
A container for DataTable objects
Exchange easily with XML Docs.
As an ADO Recordset Object
Disconnected Objects –
DataRelation Object
Define a relation between DataTable objects
i.e. Matster-to-Detail
Define a rule. i.e. Delete the details when to
delete the master"s record.
Disconnected Objects –
DataView Object
Define a rule. i.e. Sorting or Filtering a
DataTable
.NET Framework Data Provider
Using the Connection Object (on e)
Other usages:
Property: State
Methods: Open, Close
Event: StateChange
Using the Command Object (on e)
B) myCommand = SqlCommand.Create ()
myCommand.Connection = myConnection
myCommand.CommandText = “…”
C) myCommand = SqlCommand.Create(mySqlStr,
myConnection)
Using the Command Object (thre e)
How to do?
myCommand.CommandText = “Update permis set…”
myCommand.ExecuteNonQuery ()
Using the Command Object (fo ur )
How to do?
Dim myDataReader as SqlDataReader
myCommand.CommandText = “select * from PermisUser"”
myDataReader = myCommand.ExecuteReader
While myDataReader.Read()
Response.Write (myDataReader(0), myDataReader(1))
End While
reader.Close()
Using the Command Object (fi ve)
Special operation:
Dim sCount as String
myCommand.CommandText = "select count(*) from
PermisUser”
sCount = myCommand.ExecuteScalar.ToString()
Using the Command Object (si x)
DataAdapter
DataSet
Database DataTable
Using the DataAdapter Object (two)
B) myDataAdapter = SqlDataAdapter.Create(strSQLSelect,
myConnection)
D) myDataAdapter = SqlDataAdapter.Create(myCommand)
Using the DataAdapter Object
(three)
B) myDataAdapter.TableMappings.Add("Table", "Customers")
myDataAdapter.Fill(myDataSet)
C) myDataAdapter.Fill(myDataSet, "myTableName")
"myTableName")
nStartRow is zero-base.
E) myDataAdapter.Fill(myDataTable)
If you have a DataTable object
Using the DataAdapter Object (f ive)
Applications:
You will fill the same DataTable twice:
If your DataTable has no primary key:
The result set from the DataAdapter is appended.
If your DataTabel has a primary key:
DataTable is updated by the primary key.
Use the FillSchema method to let your DataTable get a primary
key as its mapping table in our database.
myDataAdapter.FillSchema(myDataSet, SchemaType.Source)
Other usages:
Methods:
Fill
FillSchema
Update
Events:
FillError
RowUpdating
RowUpdated
Operation without Connection to a
Database
Using the DataSet Object (on e)
You can:
scroll, search, sort, and filter a DataSet object.
define a master-detail relation.
modify the DataSet of content.
integrated with XML Doc.
Using the DataSet Object (two)
B) myDataSet = DataSet.Create("myDataSetName")
Using the DataSet Object (three)
myDataAdapter.Fill(myDataSet, "myTableName")
A) myDataSet.Tables(0).Rows(0)(1).ToString
B) myDataSet.Tables("myTableName").Rows(0)("Emp_Na").ToString
C) myDataSet.Tables.Item(0).Rows.Item(0).Item(1).ToString
D)
myDataSet.Tables.Item("myTableName").Rows.Item(0).Item("Emp_Na").To
String
Using the DataSet Object (fou r)
myDataAdapter.Fill(myDataSet, "myTableName")
A)
myDataSet.Tables("myTableName").Columns(1).ColumnName.ToString
B)
myDataSet.Tables("myTableName").Columns("Emp_Na").Ordinal.ToStri
ng
C)
myDataSet.Tables("myTableName").Columns("Emp_Na").DataType.ToS
tring
Using the DataSet Object (five)
Other usages
Methods:
AcceptChanges GetXml
RejectChanges GetXmlSchema
GetChanges ReadXml
Clear ReadXmlSchema
Clone WriteXml
Copy WriteXmlSchema
Merge
Reset
Using the DataTable Object (on e)
ReadOnly
myColumn.ReadOnly = True
Unique
myColumn.Unique = True
Column of MaxLength
myColumn.MaxLength = 5
Using the DataTable Object (three)
aryColumn(0) = myDataTable.Columns.Add("Emp_No",
TypeOf(Integer))
aryColumn(1) =
myDataTable.Columns.Add("Emp_Name")
myDataTable.PrimaryKey = aryColumn
Using the DataTable Object (fou r)
myForeignKey =
ForeignKeyConstraint.Create(myMasterCol, myDetailCol)
myDataTable.Constraints.Add(myForeignKey)
Using the DataTable Object (fi ve)
Insert:
If your DataTable is… ( VB + SQL Server )
Edit:
Dim theRow as DataRow
theRow = myDataTable.Rows.Find("92010002")
If theRow Is Nothing then
ShowMessage("Primary-Key Value not found.")
Else
theRow("Emp_Salary") = 33000
or
…
Else begin
Reference
theRow.BeginEdit
Method:
theRow("Emp_Salary") = 33000 CancelEdit()
theRow.EndEdit Events:
RowChanging
End RowChanged
ColumnChanging
ColumnChanged
Using the DataTable Object (ei gh t)
Delete:
Use the Delete() to mark.
Use the Remove() / RemoveAt() to remove the row
truly.
Use the Clear() to remove all rows.
theRow.Delete()
or
myDataTable.Remove(theRow)
or
myDataTable.Clear()
Using the DataTable Object (nine)
sResult =
myDataTable.Rows("Emp_Name",
DataRowVersion.Current).ToString
Using the DataTable Object (el ev en)
Other usages
Methods:
AcceptChanges BeginLoadData
RejectChanges EndLoadData
GetChanges ImportRow
Clear
Clone
Copy
Compute
Reset
DataColumn Objects
Other usages
Properties:
Caption
DefaultValue
Table
DataRow Objects
Other usages
Properties: Methods:
RowError AcceptChanges
Table RejectChanges
CancelEdit
GetChildRows
GetParentRow
GetParentRows
SetParentRow
IsNull
Use the DataRelation Object (on e)
myRelation = DataRelation.Create("RelationName",
myCustTable.Columns("CustID"),
myOrderTable.Columns("CustID"))
myDataSet.Relations.Add(myRelation)
For theOrderRow in
theCustRow.GetChildRows("RelationName") do
ShowMessage(theOrderRow("ProdName").ToString)
Other usages
Properties:
ChildTable
ParentTable
Nested
Applications
Searching, Sorting, Filtering (on e)
Searching: Find()
Find a row by the Primary Key.
aryKey(0) = nSerialNo
aryKey(1) = "myProdId"
theRow = myDataTable.Rows.Find(aryKey)
Searching, Sorting, Filtering (two )
Searching: Select()
Find rows with conditions.
Dim theRows as DataRow()
Dim strCondition as String
strCondition = "ProdType = ""PO"" and Cost > 10000 and
ID like ""A%"""
theRows = myDataTable.Select(strCondition)
Sorting: Select()
Var strSort : String
strSort = "Cost Desc"
theRows = myDataTable.Select(strCondition, strSort)
Searching, Sorting, Filtering (three)
DataView objects?
Not a SQL command.
One DataView is mapping to one DataTable.
A DataTable must have a TableName.
Including all columns from one DataTable.
Searching, sorting, filtering, editing, and
navigation.
Searching, Sorting, Filtering (fi ve)
myDataTable = myDataAdapter.Fill(myDataSet,
"myTableName")
myDataView = DataView.Create(myDataTable)
Searching, Sorting, Filtering (si x)
theRowView = myDataView(nRowIndex)
ShowMessage(theRowView("theFieldName").ToString)
Next
or
For nRowIndex = 0 to myDataView.Count - 1
sResult =
myDataView(nRowIndex)("theFieldName").ToString
ShowMessage(sResult)
Next
FindRows() ?
Return an array of DataRowView objects.
Dim aryRows as Object()
aryRows = myDataView.FindRows(mySearchingValue)
If ( Length(aryRows) > 0 ) then …
Editing with DataRowView
Modify a row.
theRowView.BeginEdit()
theRowView("ProdName") = "yyy"
theRowView.EndEdit()
Delete a row.
theRowView.Delete()
DataView & DataRowView Objeccts
Other usages:
DataView objects
Methods: CopyTo, Delete
DataRowView objects
Methods: CancelEdit, CreateChildView
Requirement:
SqlDataAdapter.InsertCommand
SqlDataAdapter.DeleteCommand
SqlDataAdapter.UpdateCommand
SqlCommand.Connection
SqlCommand.Parameters
From DataSet to Database (two )
InsertCommand:
Initial
strSQL = "insert into CustTable(CustNo) values (@CustNo)"
myDataAdapter.InsertCommand.Connection =
mySqlConnection
myDataAdapter.InsertCommand.CommandText = strSQL
myDataAdapter.InsertCommand.Parameters.Add("@CustNo",
SqlDbType.VarChar)
Execute
Dim theRow as DataRow
myDataAdapter.InsertCommand.Parameters("@CustNo").Value
=
theRow("CustNo").ToString()
myDataAdapter.InsertCommand.ExecuteNonQuery()
From DataSet to Database (thr ee)
DeleteCommand:
Initial
strSQL = "delete from CustTable where CustNo = @CustNo"
myDataAdapter.DeleteCommand.Connection = mySqlConnection
myDataAdapter.DeleteCommand.CommandText = strSQL
myDataAdapter.DeleteCommand.Parameters.Add("@CustNo",
SqlDbType.VarChar)
Execute
Dim theRow as DataRow
myDataAdapter.DeleteCommand.Parameters("@CustNo").Value =
theRow("CustNo",
DataRowVersion.Original).ToString()
myDataAdapter.DeleteCommand.ExecuteNonQuery()
From DataSet to Database (four )
UpdateCommand:
strSQL = "update CustTable set CustType = @CustType_New where
CustType =
@CustType"
myDataAdapter.UpdateCommand.Connection = mySqlConnection
myDataAdapter.UpdateCommand.CommandText = strSQL
myDataAdapter.UpdateCommand.Parameters.Add("@CustType_New",
SqlDbType.VarChar)
myDataAdapter.UpdateCommand.Parameters.Add("@CustType",
SqlDbType.VarChar)
Execute
Dim theRow as DataRow
myDataAdapter.UpdateCommand.Parameters("@CustType_New").Valu
e= theRow("CustType",
DataRowVersion.Current).ToString()
myDataAdapter.UpdateCommand.Parameters("@CustType").Value =
theRow("CustType",
DataRowVersion.Original).ToString()
myDataAdapter.UpdateCommand.ExecuteNonQuery()
From DataSet to Database (fi ve)
strXML = myDataSet.GetXml()
The strXML is a string list table by table.
myDataSet.WriteXML(“C:\myXMLFile.xml”, XMLWriteMode.
WriteSchema)
End & Thank you.
Author : Benjamin
Editor : Albert