You are on page 1of 37

DataSet, DataTable

Visual Basic.net 2010

2013


..

DataSet

DataSet

DataSet

DataSet

DataTable

11

11

DataTable DataSet

11

DataTable

12

DataTable

13

15

DataColumn

16

19

DataRow

19

DataRow

19

DataRow

21

23

24

25

26

27

28

29

30

31

31

32


DataSet

DataSet
2
DataSet
. DataSet

DataTable DataRelation .DataView

DataTable DataRows DataRow

.DataColumns .System.Data
:DataSet

DataSethttp://msdn.microsoft.com/en-us/library/zb0sdh0b%28v=vs.100%29.aspx :

.................................................................................................................................................................................................................

.................................................................................................................................................................................................................
.................................................................................................................................................................................................................

DataSet
3
DataSet
DataSet Constructor "DataSets " DataSet

."NewDataSet" :

Dim customerOrders As DataSet = New DataSet("CustomerOrders")

:
Dim customerOrders As New DataSet("CustomerOrders")
MsgBox(customerOrders.DataSetName, MsgBoxStyle.Information, "DataSet Name is:")

: DataSet CustomerOrders DataSet


Dim customerOrders As New DataSet

:
Dim customerOrders As New DataSet
MsgBox(customerOrders.DataSetName, MsgBoxStyle.Information, "DataSet Name is:")

:DataSet
:DataSetName
. DataSet /
Dim dataSet As DataSet
dataSet = New DataSet("SuppliersProducts")
MsgBox(dataSet.DataSetName, MsgBoxStyle.Information, "DataSet Name is:")

:IsInitialized
. DataSet
Dim dataSet As DataSet
dataSet = New DataSet("SuppliersProducts")
MsgBox(dataSet.IsInitialized, MsgBoxStyle.Information, "IsInitialized")

.................................................................................................................................................................................................................

.................................................................................................................................................................................................................
.................................................................................................................................................................................................................

DataSet
4
:Tables
. . DataTables

. DataTable

:DataSet
:AcceptChanges
. DataSet
Private Sub AcceptChanges()
Dim myDataSet As DataSet
myDataSet = new DataSet()
' Not shown: methods to fill the DataSet with data.
Dim t As DataTable
t = myDataSet.Tables("Suppliers")
' Add a DataRow to a table.
Dim myRow As DataRow
myRow = t.NewRow()
myRow("CompanyID") = "NWTRADECO"
myRow("CompanyName") = "NortWest Trade Company"
' Add the row.
t.Rows.Add( myRow )
' Calling AcceptChanges on the DataSet causes AcceptChanges to be
' called on all subordinate objects.
myDataSet.AcceptChanges()
End Sub

:Clear
.DataSet DataSet
dataSet.Clear()

:Clone
. DataSet

.................................................................................................................................................................................................................

.................................................................................................................................................................................................................
.................................................................................................................................................................................................................

DataSet
5
Dim dataSet As DataSet
dataSet = New DataSet("SuppliersProducts")
MsgBox(dataSet.DataSetName, MsgBoxStyle.Information, "DataSet Name is:")
Dim ClonedDataSet As DataSet
ClonedDataSet = dataSet.Clone()
MsgBox(ClonedDataSet.DataSetName, MsgBoxStyle.Information, "ClonedDataSet Name is:")

:DataSet
Dim dataSet As DataSet
dataSet = New DataSet
MsgBox(dataSet.DataSetName, MsgBoxStyle.Information, "DataSet Name is:")
Dim ClonedDataSet As DataSet
ClonedDataSet = dataSet.Clone()
MsgBox(ClonedDataSet.DataSetName, MsgBoxStyle.Information, "ClonedDataSet Name is:")

:Copy
. DataSet
Dim dataSet As DataSet
dataSet = New DataSet("SuppliersProducts")
MsgBox(dataSet.DataSetName, MsgBoxStyle.Information, "DataSet Name is:")
Dim CopiedDataSet As DataSet
ClonedDataSet = dataSet.Copy()
MsgBox(CopiedDataSet.DataSetName, MsgBoxStyle.Information, "CopiedDataSet Name is:")

:DataSet
Dim dataSet As DataSet
dataSet = New DataSet
MsgBox(dataSet.DataSetName, MsgBoxStyle.Information, "DataSet Name is:")
Dim CopiedDataSet As DataSet
ClonedDataSet = dataSet.Copy()
MsgBox(CopiedDataSet.DataSetName, MsgBoxStyle.Information, "CopiedDataSet Name is:")

.................................................................................................................................................................................................................

.................................................................................................................................................................................................................
.................................................................................................................................................................................................................

DataSet
6
:GetChanges
DataSet

AcceptChanges DataSet.

: .

:HasChanges
) (True/False DataSet

If dataSet.HasChanges = True Then


)"MsgBox("There are some changes
Else
)"MsgBox("The DataSet was not changed
End If

):Merge(DataSet
DataSet DataSet.
:
)"Dim FirstDataSet As New DataSet("FirstDataSet
)"Dim FirstTable As New DataTable("FirstTable

DataSet DataTable :FirstDataSet


)FirstDataSet.Tables.Add(FirstTable

FirstDataSet : FirstTable :
)MsgBox(FirstDataSet.Tables(0).TableName

: DataSet SecondDataSet :
)"Dim SecondDataSet As New DataSet("SecondDataSet

.................................................................................................................................................................................................................

.................................................................................................................................................................................................................
.................................................................................................................................................................................................................

DataSet
7
FirstDataSet :SecondDataSet
)SecondDataSet.Merge(FirstDataSet

:
)MsgBox(SecondDataSet.Tables(0).TableName

FirstDataSet .
):Merge(DataTable
DataTable .

):Merge(DataSet, Boolean
DataSet DataSet
.

)SecondDataSet.Merge(FirstDataSet, False

.
)SecondDataSet.Merge(FirstDataSet, True

:RejectChanges
DataSet

AcceptChanges.

) (AcceptChanges, RejectChanges :
.

.................................................................................................................................................................................................................

.................................................................................................................................................................................................................
.................................................................................................................................................................................................................

DataSet
8
:Reset
. DataSet
Dim FirstDataSet As New DataSet("FirstDataSet")
Dim FirstTable As New DataTable("FirstTable")
FirstDataSet.Tables.Add(FirstTable)
MsgBox(FirstDataSet.Tables(0).TableName)
Dim SecondDataSet As New DataSet("SecondDataSet")
SecondDataSet.Merge(FirstDataSet)
MsgBox(SecondDataSet.Tables(0).TableName)
SecondDataSet.Reset()
MsgBox(SecondDataSet.Tables(0).TableName, , "after reset is called")

Cannot find table 0 : Reset

. SecondDataset

.................................................................................................................................................................................................................

.................................................................................................................................................................................................................
.................................................................................................................................................................................................................

DataSet
9

DataSet .
DataSet

2010
.


:
.
abubakerswedan@yahoo.com
.

DataTable
..

.................................................................................................................................................................................................................

.................................................................................................................................................................................................................
.................................................................................................................................................................................................................


DataTable

DataTable
11
DataTable
.DataSet DataColumn : DataRow
) (Columns ) .(Rows .System.Data

Creating a DataTable
System.Data DataTable

Dim UnNamedTable As New DataTable

:Customers

)"Dim Customers As New DataTable("Customers

ADO.NET Table1 Table2.

TableName

. DataTableCollection
.System.Data

DataTable DatatSet
DataTable DataSet :
)"Dim EmployeesDataSet As New DataSet("Employees DataSet
Dim PersonalInfoTable As New DataTable
)EmployeesDataSet.Tables.Add(PersonalInfoTable

Add Tables .DataSet

.................................................................................................................................................................................................................

.................................................................................................................................................................................................................
.................................................................................................................................................................................................................

DataTable
12
:
:TableName
.DataTable /
Dim FirstTable As New DataTable
MsgBox(FirstTable.TableName)
FirstTable.TableName = "Nationalities"
MsgBox(FirstTable.TableName)

:Columns

.DataTable
Private
Dim
Dim
For

Sub PrintValues(ByVal table As DataTable)


row As DataRow
column As DataColumn
Each row in table.Rows
For Each column In table.Columns
Msgbox(row(column))
Next
Next
End Sub

.DataRow DataColumn :

:DataSet
.DataTable DataSet
Dim EmployeesDataSet As New DataSet("Employees DataSet")
Dim PersonalInfoTable As New DataTable("Personal Info")
EmployeesDataSet.Tables.Add(PersonalInfoTable)
MsgBox(PersonalInfoTable.DataSet.DataSetName)

:PrimaryKey
.DataTable /
.DataColumn

:Rows
.DataRow .DataTable

.................................................................................................................................................................................................................

.................................................................................................................................................................................................................
.................................................................................................................................................................................................................

DataTable
13
Methodes :DataTable

) ( .
:Clear
.

)(PersonalInfoTable.Clear

:Clone
.
Dim PersonalInfoTable As New DataTable
"PersonalInfoTable.TableName = "Original Table
Dim ClonedTable As New DataTable
)(ClonedTable = PersonalInfoTable.Clone
)MsgBox(ClonedTable.TableName

:Copy
.
Dim PersonalInfoTable As New DataTable
"PersonalInfoTable.TableName = "Original Table
Dim CopiedTable As New DataTable
)(CopiedTable = PersonalInfoTable.Copy
)MsgBox(CopiedTable.TableName

:ImportRow
DataRow DataTable .
.DataRow

):Merge(DataTable
. .

.................................................................................................................................................................................................................

.................................................................................................................................................................................................................
.................................................................................................................................................................................................................

DataTable
14
):Merge(DataTable, Boolean
. .
.

:NewRow
DataRow . .

:Reset
.

:Select
DataRows.

):Select(String
DataRows .

):Select(String, String
DataRows .

):Select(String, String, DataViewRowState


DataRows .

.................................................................................................................................................................................................................

.................................................................................................................................................................................................................
.................................................................................................................................................................................................................

DataTable
15
)( Adding Columns to DataTable
! DataTable

! .Data Columns

System.Data DataColumn
.Table Schema

:
)"Dim Customers As New DataTable("Customers

:
))Dim CustomerID As New DataColumn("ID", GetType(Long

ID .Long
CustomerID
:
)Customers.Columns.Add(CustomerID

Add Columns .

Add Columns

)"Dim Customers As New DataTable("Customers


))Customers.Columns.Add("ID", GetType(Long
))Customers.Columns.Add("CustomerName", GetType(String
))Customers.Columns.Add("BirthDay", GetType(Date

.................................................................................................................................................................................................................

.................................................................................................................................................................................................................
.................................................................................................................................................................................................................

DataTable
16
DataColumn
.

:
:AllowDBNull
Boolean = .Null

Dim column As DataColumn


))"column = New DataColumn("ID", System.Type.GetType("System.Int32
column.AllowDBNull = True

Dim column As DataColumn


))"column = New DataColumn("ID", System.Type.GetType("System.Int32
column.AllowDBNull = False

System.DBNull ) :

:(Null

If (DBNull.Value.Equals(FieldValue)) Then
Do something
End If

AutoIncrement AutoIncrementSeed:AutoIncrementStep

If (IsDBNull(FieldValue)) Then
Do something
End If

AutoIncrementSeed .AutoIncrementStep

AutoIncrement False .

Dim column As DataColumn = New DataColumn


)"column.DataType = System.Type.GetType("System.Int32
With column
.AutoIncrement = True
.AutoIncrementSeed = 1000
.AutoIncrementStep = 10
End With

.................................................................................................................................................................................................................

.................................................................................................................................................................................................................
.................................................................................................................................................................................................................

DataTable
17
:ColumnName
. .
Dim column As New DataColumn
With column
.ColumnName = "CustomerName"
.AllowDBNull = True
End With
MsgBox(column.ColumnName)

:DataType

.
.

Dim column As New DataColumn


With column
.ColumnName = "FirstName"
.DataType = System.Type.GetType("System.String")
End With
MsgBox(column.DataType.ToString)

:DefaultValue

Dim column As New DataColumn


With column
.ColumnName = "FirstName"
.DataType = System.Type.GetType("System.String")
.DefaultValue = "None"
End With
MsgBox(column.DefaultValue)

:MaxLength
-1 :
.

Dim column As New DataColumn


With column
.ColumnName = "FirstName"
.DataType = System.Type.GetType("System.String")
.MaxLength = 20
.DefaultValue = "None"
End With

.................................................................................................................................................................................................................

.................................................................................................................................................................................................................
.................................................................................................................................................................................................................

DataTable
18
:ReadOnly
:

Dim column As New DataColumn


With column
.ColumnName = "FirstName"
.DataType = System.Type.GetType("System.String")
.ReadOnly = True
.DefaultValue = "None"
End With

:Unique
True

.Null

Dim column As New DataColumn


With column
.ColumnName = "ID"
.DataType = System.Type.GetType("System.Int32")
.Unique = True
End With

/ - PrimaryKey DataTable
:DataTable

Dim Customers As New DataTable("Customers")


Customers.Columns.Add("ID", GetType(Long))
Customers.Columns.Add("CustomerName", GetType(String))
Customers.Columns.Add("BirthDay", GetType(Date))
Customers.PrimaryKey = {Customer.Columns("ID")}

.( )Columns Collection DataTable DataColumn

Rows Collection
. DataTable

.................................................................................................................................................................................................................

.................................................................................................................................................................................................................
.................................................................................................................................................................................................................

DataTable
19
Creating New Rows
DataColumns Creating DataTables

."( Rows " ):

( DataColumns )

..DataRow

DataRow
.DataTables DataColumns

. DataColumn

DataRow
:Item(DataColumn)
. /
Dim Customers As New DataTable("Customers")
Customers.Columns.Add("ID", GetType(Long))
Customers.Columns.Add("CustomerName", GetType(String))
Customers.Columns.Add("RegistrationDate", GetType(Date))
Customers.PrimaryKey = {Customers.Columns("ID")}
Dim NewRow As DataRow
NewRow = Customers.NewRow
With NewRow
.Item("ID") = 1
.Item("CustomerName") = "Abubaker Swedan"
.Item("RegistrationDate") = Now.Date
End With
Customers.Rows.Add(NewRow)
MsgBox(Customers.Rows(0).Item("CustomerName"))
MsgBox(Customers.Rows(0).Item("RegistrationDate"))

.................................................................................................................................................................................................................

.................................................................................................................................................................................................................
.................................................................................................................................................................................................................

DataTable
20
:Item(Int32)
. /
Dim Customers As New DataTable("Customers")
Customers.Columns.Add("ID", GetType(Long))
Customers.Columns.Add("CustomerName", GetType(String))
Customers.Columns.Add("RegistrationDate", GetType(Date))
Customers.PrimaryKey = {Customers.Columns("ID")}
Dim NewRow As DataRow
NewRow = Customers.NewRow
With NewRow
.Item(0) = 1
.Item(1) = "Abubaker Swedan"
.Item(2) = Now.Date
End With
Customers.Rows.Add(NewRow)
MsgBox(Customers.Rows(0).Item(1))
MsgBox(Customers.Rows(0).Item(2))

:Item(String)
. /
Dim Customers As New DataTable("Customers")
Customers.Columns.Add("ID", GetType(Long))
Customers.Columns.Add("CustomerName", GetType(String))
Customers.Columns.Add("RegistrationDate", GetType(Date))
Customers.PrimaryKey = {Customers.Columns("ID")}
Dim NewRow As DataRow
NewRow = Customers.NewRow
With NewRow
.Item("ID") = 1
.Item("CustomerName") = "Abubaker Swedan"
.Item("RegistrationDate") = Now.Date
End With
Customers.Rows.Add(NewRow)
MsgBox(Customers.Rows(0).Item("CustomerName"))
MsgBox(Customers.Rows(0).Item("RegistrationDate"))

.................................................................................................................................................................................................................

.................................................................................................................................................................................................................
.................................................................................................................................................................................................................

DataTable
21
:Table
. DataTable
Dim Customers As New DataTable("Customers")
Customers.Columns.Add("ID", GetType(Long))
Customers.Columns.Add("CustomerName", GetType(String))
Customers.Columns.Add("RegistrationDate", GetType(Date))
Customers.PrimaryKey = {Customers.Columns("ID")}
Dim NewRow As DataRow
NewRow = Customers.NewRow
With NewRow
.Item("ID") = 1
.Item("CustomerName") = "Abubaker Swedan"
.Item("RegistrationDate") = Now.Date
End With
Customers.Rows.Add(NewRow)
MsgBox(NewRow.Table.TableName)

: DataRow Methodes
:Delete
.DataRow

: DataRelation
:GetChildRows(DataRelation)
.DataRelation DataRow )(

:GetChildRows(DataRelation)
.DataRelation RelationName DataRow )( Rows
:GetParentRow(DataRelation)

.................................................................................................................................................................................................................

.................................................................................................................................................................................................................
.................................................................................................................................................................................................................

DataTable
22
. DataRelation DataRow )(

:GetParentRow(String)
.DataRelation RelationName DataRow )(

:IsNull(DataColumn)
.Null DataColumn
If NewRow.IsNull(ColumnObject) Then
MsgBox("Empty Column")
Else
MsgBox("it is not null value")
End If

:IsNull(Int32)

.Null ) ( DataColumn
If NewRow.IsNull(1) Then
MsgBox("Empty Column")
Else
MsgBox("it is not null value")
End If

:IsNull(String)

.Null ) ( DataColumn
If NewRow.IsNull("CustomerName") Then
MsgBox("Empty Column")
Else
MsgBox("it is not null value")
End If

DataColumns NewRow DataTable

.DataTable
:( DataRow )

Dim TheNewRow As DataRow = TableName.NewRow()

.................................................................................................................................................................................................................

.................................................................................................................................................................................................................
.................................................................................................................................................................................................................

DataTable
23

= AutoIncrement Default Values Null

.True

DataRow
DataColumn Item DataRow

DataColumn ) ( DataTable

Dim TheNewRow As DataRow = TableName.NewRow()


TheNewRow.Item("ID")= 100
TheNewRow.Item(0)= 100
Dim SpecificRow As DataColumn = TableName.Columns(0)
TheNewRow.Item(SpecificRow)= 100

( ID)

: DataRow Item
Dim TheNewRow As DataRow = TableName.NewRow()
TheNewRow("ID")= 100

: )!( ) (
Dim Customers As New DataTable("Customers")
Customers.Columns.Add("ID", GetType(Long))
Customers.Columns.Add("CustomerName", GetType(String))
Customers.Columns.Add("RegistrationDate", GetType(Date))
Customers.PrimaryKey = {Customers.Columns("ID")}
Dim NewRow As DataRow
NewRow = Customers.NewRow
NewRow!ID = 1
NewRow!CustomerName = "Abubaker Swedan"
NewRow!RegistrationDate = Now.Date
Customers.Rows.Add(NewRow)

.................................................................................................................................................................................................................

.................................................................................................................................................................................................................
.................................................................................................................................................................................................................

DataTable
24
DataTable
DataTable

.Rows.Add :DataTable

)(Dim TheNewRow As DataRow = TableName.NewRow


TheNewRow.Item("ID")= 100
)TableName.Rows.Add(TheNewRow

Rows.Add :
)(Dim TheNewRow As DataRow = TableName.NewRow
) TableName.Rows.Add(100, Abubaker, Tripoli,

Add
.

.................................................................................................................................................................................................................

.................................................................................................................................................................................................................
.................................................................................................................................................................................................................

DataTable
25
DataTable
.

(
Select Method DataTable
Dim foundRows() As Data.DataRow
)"'foundRows = DataSet1.Tables("Customers").Select("LastName = 'Swedan

Select

.LastName .
:

Dim RowsCount As Integer


RowsCount = foundRows.Count

:
Dim FirstColValue As Integer
)"FirstColValue = foundRows(0).Item("OrderID

( Primary Key Value


Find DataRowCollection :
"Dim s As String = "primaryKeyValue
)Dim foundRow As DataRow = dataset1.Tables("Customers").Rows.Find(s
If foundRow IsNot Nothing Then
))(MsgBox(foundRow(1).ToString
Else
)"MsgBox("A row with the primary key of " & s & " could not be found
End If

.................................................................................................................................................................................................................

.................................................................................................................................................................................................................
.................................................................................................................................................................................................................

DataTable
26
DataTable
DataTable
.

Select Method DataTable :


Dim customerRow() As Data.DataRow
)"customerRow = DataSet1.Tables("Customers").Select("CustomerID = 107
"customerRow(0)("CompanyName") = "New Company Name
"customerRow(0)("City") = "Tripoli

CustomerID Primary Key Select

)( customerRow
. ) (0 .

:
"customerRow(0).Item("CompanyName") = "New Company Name
"customerRow(0).Item("City") = "Tripoli

Item DataRow .
DataSet:
"DataSet1.Tables(0).Rows(4).Item(1) = "New Company Name
"DataSet1.Tables(0).Rows(4).Item(2) = "Tripoli

:
"DataSet1.Tables("Customers").Rows(4).Item(1) = "New Company Name
"DataSet1.Tables("Customers").Rows(4).Item(2) = "Tripoli

.................................................................................................................................................................................................................

.................................................................................................................................................................................................................
.................................................................................................................................................................................................................

DataTable
27
Viewing Data in a DataTable
Rows Columns .DataTable
Select Method .

Find Method DataRowCollection

Select Method DataRow :

)( .Filter expression
.Sort expression

.DataViewRowState

.LastName = Swedan
DataColumn
.ORDER BY LastName DESC
Sql
: .Row State

.................................................................................................................................................................................................................

.................................................................................................................................................................................................................
.................................................................................................................................................................................................................

DataTable
28
Removing Data from DataTable
: DataTable
.DataTable.Rows Remove

.DataTable.Rows RemoveAt

: Remove Method
Dim SelectedRow As DataRow = SelectedTable.Rows(0)
SelectedTable.Rows.Remove(SelectedRow)

: RemoveAt Method
SelectedTable.Rows.RemoveAt(0)

.
:DataTable.Rows Clear Method
SelectedTable.Rows.Clear()

.................................................................................................................................................................................................................

.................................................................................................................................................................................................................
.................................................................................................................................................................................................................

DataTable
29
/ Batch Processing

.
.

ADO.NET
! : .

AcceptChanges Method DataTable .

RejectChanges Method

.DataRows DataRow

AcceptChanges RejectChanges
) (AcceptChanges )

.(RejectChanges

)(SelectedTable.AcceptChanges
)(SelectedTable.RejectChanges

.................................................................................................................................................................................................................

.................................................................................................................................................................................................................
.................................................................................................................................................................................................................

DataTable
30
Row State
ADO.NET

. ADO.NET

DataRow.RowState :

:DataRowState.Detached .DataTable

:DataRowState.Added DataTable .

:DataRowState.UnChanged

RejectChanges .

.AcceptChanges Method

.NewRow

:DataRowState.Deleted

:DataRowState.Modified .Modified

AcceptChanges .

Rows.Remove

Rows.RemoveAt
.

Delete Method DataRow Rows.Remove/

Rows.RemoveAt Deleted
AcceptChanges .RejectChanges

)(TheRow.Delete
AcceptChanges DataSet DataTable DataRow

= Deleted Current Values


.Original Values

RejectChanges = Added

Unchanged Original Values

.Current Values

.................................................................................................................................................................................................................

.................................................................................................................................................................................................................
.................................................................................................................................................................................................................

DataTable
31
Row Versions
ADO.NET

DataRowVersion
:

:DataRowVersion.Original AcceptChanges

:DataRowVersion.Porposed .

:DataRowVersion.Current .Original

. .Original

:DataRowVersion.Default . Added ModifiedUnchanged


Current Deleted Original Detached .Proposed

Added Modified Unchanged Deleted Detached .

Validating Changes
Add

Add

: !.

5,261 System.Int32 .

.................................................................................................................................................................................................................

.................................................................................................................................................................................................................
.................................................................................................................................................................................................................

DataTable
32

.AcceptChanges Method pending


:
) AcceptChanges ( .

" "RowState Detached : Added Modified Deleted.Unchanged


Original : Proposed Current.Default


Method DataSet HasChanges : True

. GetChanges DataSet/ DataTable

DataSet DataTable

. GetChanges
AcceptChanges !.

( DataSet
DataSet :
)(Dim changedRecords As DataSet = dataset1.GetChanges

( DataTable
DataTable :
)(Dim changedRecordsTable As DataTable = Customers.GetChanges

( ) (
GetChanges:
)Dim addedRecords As DataSet = dataset1.GetChanges(DataRowState.Added

.................................................................................................................................................................................................................

.................................................................................................................................................................................................................
.................................................................................................................................................................................................................

DataTable
33

DataTable
.
.

abubakerswedan@yahoo.com .

.

.................................................................................................................................................................................................................

.................................................................................................................................................................................................................
.................................................................................................................................................................................................................