26
Visual Basic .NET Programming DataSet DataSet DataSet DataSet Object Object Object Object a disconnected, memory resident cache of data reads the database and creates an in- memory copy of that part of that database that the program needs ADO.NET * Property of STI Page 1 of 26 contains DataTables, DataRelation and Constraints objects

MELJUN CORTES Vb.net ado.net part_ii

Embed Size (px)

Citation preview

Page 1: MELJUN CORTES Vb.net ado.net part_ii

Visual Basic .NET Programming

DataSet DataSet DataSet DataSet ObjectObjectObjectObject

� a disconnected, memory resident cache of

data

� reads the database and creates an in-

memory copy of that part of that database

that the program needs

ADO.NET * Property of STIPage 1 of 26

� contains DataTables, DataRelation and

Constraints objects

Page 2: MELJUN CORTES Vb.net ado.net part_ii

Visual Basic .NET Programming

PopulatingPopulatingPopulatingPopulatingDataSetsDataSetsDataSetsDataSets

� done at runtime

� use DataAdapter to access data

� Example:

Private conServer As

SqlClient.SqlConnection

Public Sub Form1_Load(ByVal

sender As System.Object,

ADO.NET * Property of STIPage 2 of 26

sender As System.Object,

ByVal e As System.EventArgs)

Handles MyBase.Load

conserver = New

SqlClient.SqlConnection()

Dim strConnection As String =

“Integrated Security=True;Data

Source=Localhost;Initial

Catalog=Books;”

conServer.ConnectionString =

strConnection

conserver.Open()

End Sub

Page 3: MELJUN CORTES Vb.net ado.net part_ii

Visual Basic .NET Programming

PopulatingPopulatingPopulatingPopulatingDataSetsDataSetsDataSetsDataSets

� Example: (con’t)

Private Sub Button1_Click ByVal sender

As System.Object, ByVal e As

System.EventArgs) Handles

Button1.Click

Dim adaptSQL As

SqlClient.SqlDataAdapter(“Select

ADO.NET * Property of STIPage 3 of 26

SqlClient.SqlDataAdapter(“Select

count(*) from Catalogue”,

conserver)

‘the code that will fill the DataSet

Dim datBooks As New DataSet()

adaptSQL.Fill(datBooks, “MyTable”)

‘manipulate data locally using

DataSet

adaptSQL.Update(datBooks, “MyTable”)

End Sub

Page 4: MELJUN CORTES Vb.net ado.net part_ii

Visual Basic .NET Programming

Relationships Relationships Relationships Relationships in DataSetin DataSetin DataSetin DataSet

� DataRelation object

� allows tables to provide relationship with each

other

� contains an array of DataColumn objects that

define the parent column, primary key and child

column or foreign key in the relationship

� Example:

Private Sub Button1_Click ByVal sender As

System.Object, ByVal e As

ADO.NET * Property of STIPage 4 of 26

System.Object, ByVal e As

System.EventArgs) Handles Button1.Click

Dim adaptSQL As SqlClient.SqlDataAdapter

Dim datBooks As New DataSet()

adaptSql = New

SqlClient.SqlDataAdapter(“Select ISDN,

title, author, publisher from

Catalogue”, conServer)

adaptSQL.Fill(datBooks, “MyTable1”)

adaptSql = New

SqlClient.SqlDataAdapter(“Select ISDN,

name, address from Publisher”,

conServer)

adaptSQL.Fill(datBooks, “MyTable2”)

Page 5: MELJUN CORTES Vb.net ado.net part_ii

Visual Basic .NET Programming

Relationships in Relationships in Relationships in Relationships in DataSetDataSetDataSetDataSet

� Example: (con’t)

‘the code that provides the

relationship

Dim relBooksTitile As New

DataRelation(“BooksTitles”,

datBooks.Tables(“MyTable1”).Columns(

“ISDN”),

ADO.NET * Property of STIPage 5 of 26

“ISDN”),

datBooks.Tables(“MyTable2”).Columns(

“ISDN”))

datBooks.Relations.Add(relBooksTitle)

End Sub

Page 6: MELJUN CORTES Vb.net ado.net part_ii

Visual Basic .NET Programming

Accessing Accessing Accessing Accessing Related DataRelated DataRelated DataRelated Data

� GetChildRows method

� used to access related records in a

different table

� Example

Dim BookRow, PubRow As DataRow

Dim TitleRows() As DataRow ‘ Array

ADO.NET * Property of STIPage 6 of 26

Dim TitleRows() As DataRow ‘ Array

of DataRow objects

BookRow =

datBooks.Tables(“MyTable1”).Rows

(0)

TitleRows =

BookRow.GetChildRows(“BooksTitle

s”)

For Each PubRow In TitleRows

ListBox1.Items.Add(PubRow(“title

”).ToString)

Next

Page 7: MELJUN CORTES Vb.net ado.net part_ii

Visual Basic .NET Programming

Creating Creating Creating Creating ConstraintsConstraintsConstraintsConstraints

� Constraint

� a rule used to maintain the integrity of the

data in the DataTable

� Types of constraint classes to DataColumns:

� ForeignKeyConstraint

� UniqueConstraint

ADO.NET * Property of STIPage 7 of 26

� ForeignKeyConstraint

� controls what happens to a child row when a

parent row is updated or deleted

Cascade Deletes or updates any child

records based on the parent

record.

SetNull Sets related values to DB Null.

SetDefault Sets related values to their

defaults.

None Does not affect related rows.

Page 8: MELJUN CORTES Vb.net ado.net part_ii

Visual Basic .NET Programming

Creating Creating Creating Creating ConstraintsConstraintsConstraintsConstraints

� ForeignKeyConstraint Example:

Dim colParent As DataColumn

Dim colChild As DataColumn

Dim fkcBooksTitles As

ForeignKeyConstraint

colParent =

datBooks.Tables(“MyTable1”).Columns(

“ISDN”)

ADO.NET * Property of STIPage 8 of 26

colChild =

datBooks.Tables(“MyTable2”).Columns(

“ISDN”)

fkcBooksTitles = New

ForeignKeyConstraint(“BooksTitlesFKC

onstraint”, colParent, colChild)

fkcBooksTitles.DeleteRule =

Rule.SetNull

fkcBooksTitles.UpdateRule =

Rule.Cascade

datBooks.Tables(“titles”).Constraints.A

dd(fkcBooksTtiles)

datBooks.EnforceConstraints = True

Page 9: MELJUN CORTES Vb.net ado.net part_ii

Visual Basic .NET Programming

Creating Creating Creating Creating ConstraintsConstraintsConstraintsConstraints

� UniqueConstraint

� can be added to one column or to an array

of columns

� checks if the values in the columns are

unique

� UniqueConstraint Example

ADO.NET * Property of STIPage 9 of 26

Dim ucTitles As New

UniqueConstraint(“UniqueTitles”,

datBooks.Tables(“titles”).Column

s(“title”))

datBooks.EnforceConstraints = True

Page 10: MELJUN CORTES Vb.net ado.net part_ii

Visual Basic .NET Programming

Using Existing Using Existing Using Existing Using Existing ConstraintsConstraintsConstraintsConstraints

� FillSchema method

� used to copy constraint information into a

DataSet

� Example:

adaptSQL = New

SqlClient.SqlDataAdapter(“Select

ADO.NET * Property of STIPage 10 of 26

ISDN, title, author, publisher

from Catalogue”, conServer)

adaptSQL.FillSchema(datBooks,

schematype.Source, “Titles”)

adaptSQL.Fill(datBooks, “Titles”)

‘edit some data

adaptSQL.Fill(datBooks, “Titles”)

Page 11: MELJUN CORTES Vb.net ado.net part_ii

Visual Basic .NET Programming

Updating Data Updating Data Updating Data Updating Data in the DataSetin the DataSetin the DataSetin the DataSet

� Adding Rows

� instantiate DataRow object by issuing the

NewRow method of the DataTable

� populate the columns with data

� call the Add method of the DataRows

collection passing the DataRow object

� Example:

ADO.NET * Property of STIPage 11 of 26

� Example:

Dim newDataRow As DataRow =

datBooks.Tables(“Titles”).NewRow

newDataRow(“title”) = “New York”

newDataRow(“type”) = “business”

datBooks.Tables(“Titles”.Rows.Add(n

ewDataRow)

Page 12: MELJUN CORTES Vb.net ado.net part_ii

Visual Basic .NET Programming

Updating Data Updating Data Updating Data Updating Data in the DataSetin the DataSetin the DataSetin the DataSet

� Editing existing rows

� Call the BeginEdit method of the row.

� Change the data in the columns.

� Call the EndEdit or CancelEdit to

accept or reject the changes.

� Example:

ADO.NET * Property of STIPage 12 of 26

� Example:

Dim editDataRow As DataRow =

datBooks.Tables(“Titles”).Rows(0

)

editDataRow.BeginEdit()

editdataRow(“Title”) =

editDataRow(“Title”).ToString &

“ 1”

editDataRow.EndEdit()

Page 13: MELJUN CORTES Vb.net ado.net part_ii

Visual Basic .NET Programming

Updating Data Updating Data Updating Data Updating Data in the DataSetin the DataSetin the DataSetin the DataSet

� Deleting Data

� Remove method

� Delete method

� Remove method Example

Dim delRow As DataRow =

datBooks.Tables(“Titles”).Rows(0

ADO.NET * Property of STIPage 13 of 26

)

datBooks.Tables(“Titles”).Rows.Rem

ove(delRow)

� Delete method Example

Dim delRow As DataRow =

datBooks.Tables(“Titles”).Rows(0)

datBooks.Tables(“Titles”).Rows.Delete(delRo

w)

Page 14: MELJUN CORTES Vb.net ado.net part_ii

Visual Basic .NET Programming

Updating Data Updating Data Updating Data Updating Data in the DataSetin the DataSetin the DataSetin the DataSet

� To commit deletion invoke AcceptChanges

of the DataSet:

� datBooks.AcceptChanges()

� To undo deletion invoke RejectChanges of

the DataSet:

ADO.NET * Property of STIPage 14 of 26

the DataSet:

� datBooks.RejectChanges()

Page 15: MELJUN CORTES Vb.net ado.net part_ii

Visual Basic .NET Programming

Designing Designing Designing Designing DataSetsDataSetsDataSetsDataSets

� Connection Wizard

� Tools > Connect to Database

ADO.NET * Property of STIPage 15 of 26

� click the Change button to select the data

source

Page 16: MELJUN CORTES Vb.net ado.net part_ii

Visual Basic .NET Programming

Designing Designing Designing Designing DataSetsDataSetsDataSetsDataSets

� select a data source then click OK

ADO.NET * Property of STIPage 16 of 26

� on the Data source specification section, click

the Use user or system data source namethen select the MS Access Database

� click OK. Select the Access database file and

then click OK

� open the Server Explorer panel to see the

connection that appears under the Data

Connections

Page 17: MELJUN CORTES Vb.net ado.net part_ii

Visual Basic .NET Programming

Designing Designing Designing Designing DataSetsDataSetsDataSetsDataSets

� DataAdapter Configuration Wizard

� can be used with an existing database

connection

� Adding DataAdapter to Toolbox

� right-click Data from the Toolbox then

click Choose Items

ADO.NET * Property of STIPage 17 of 26

� tick on a data adapter

• (i.e. OleDbDataAdapter)

� click OK

Page 18: MELJUN CORTES Vb.net ado.net part_ii

Visual Basic .NET Programming

Data Binding in Data Binding in Data Binding in Data Binding in Windows FormsWindows FormsWindows FormsWindows Forms

� Data binding

� used to customize the appearance of the

forms

� Types of binding

� Simple Binding

• used to link a control to a single field in a

ADO.NET * Property of STIPage 18 of 26

• used to link a control to a single field in a

DataSet

� Complex Binding

• used to link multiple fields in a DataSet

Page 19: MELJUN CORTES Vb.net ado.net part_ii

Visual Basic .NET Programming

Data Binding in Data Binding in Data Binding in Data Binding in Windows FormsWindows FormsWindows FormsWindows Forms

� Simple Binding Example:Private conServer As SqlClient.SqlConnection

Public Sub Form1_Load(ByVal sender As

System.Object, ByVal e As

System.EventArgs) Handles MyBase.Load

conserver = New SqlClient.SqlConnection()

Dim strConnection As String = “Integrated

Security=True;Data

Source=Localhost;Initial Catalog=Books;”

conServer.ConnectionString = strConnection

ADO.NET * Property of STIPage 19 of 26

conServer.ConnectionString = strConnection

conserver.Open()

End Sub

Private Sub Button1_Click ByVal sender As

System.Object, ByVal e As

System.EventArgs) Handles Button1.Click

Dim adaptSQL As

SqlClient.SqlDataAdapter(“Select title,

author from Catalogue”, conserver)

‘the code that will fill the DataSet

Dim datBooks As New DataSet()

adaptSQL.Fill(datBooks, “Catalogue”)

TextBox1.DataBindings.Add(“Text”,

datBooks.Tables(“Catalogue”), “title”)

TextBox2.DataBindings.Add(“Text”,

datBooks.Tables(“Catalogue”), “author”)

End Sub

Page 20: MELJUN CORTES Vb.net ado.net part_ii

Visual Basic .NET Programming

Data Binding in Data Binding in Data Binding in Data Binding in Windows FormsWindows FormsWindows FormsWindows Forms

� Complex Binding Example:

Private conServer As SqlClient.SqlConnection

Public Sub Form1_Load(ByVal sender As

System.Object, ByVal e As

System.EventArgs) Handles MyBase.Load

conserver = New SqlClient.SqlConnection()

Dim strConnection As String = “Integrated

Security=True;Data

Source=Localhost;Initial Catalog=Books;”

ADO.NET * Property of STIPage 20 of 26

Source=Localhost;Initial Catalog=Books;”

conServer.ConnectionString = strConnection

conserver.Open()

End Sub

Private Sub Button1_Click ByVal sender As

System.Object, ByVal e As

System.EventArgs) Handles Button1.Click

Dim adaptSQL As

SqlClient.SqlDataAdapter(“Select title,

author from Catalogue”, conserver)

‘the code that will fill the DataSet

Dim datBooks As New DataSet()

adaptSQL.Fill(datBooks, “Catalogue”)

DataGrid1.DataSource =

datBooks.Tables(“Catalogue”)

End Sub

Page 21: MELJUN CORTES Vb.net ado.net part_ii

Visual Basic .NET Programming

Demonstration: How to Demonstration: How to Demonstration: How to Demonstration: How to Create a Data SourceCreate a Data SourceCreate a Data SourceCreate a Data Source

� File > New Project and name it as

MyDataSource then click OK

� select the ToolBox tab

� select the OleDbDataAdapter from the

ToolBox and drop it onto the form

ADO.NET * Property of STIPage 21 of 26

� click New Connection

Page 22: MELJUN CORTES Vb.net ado.net part_ii

Visual Basic .NET Programming

Demonstration: How to Demonstration: How to Demonstration: How to Demonstration: How to Create a Data SourceCreate a Data SourceCreate a Data SourceCreate a Data Source

� click the Change button

� select Microsoft Access Database File

ADO.NET * Property of STIPage 22 of 26

� select Microsoft Access Database File then click OK

Page 23: MELJUN CORTES Vb.net ado.net part_ii

Visual Basic .NET Programming

Demonstration: How to Demonstration: How to Demonstration: How to Demonstration: How to Create a Data SourceCreate a Data SourceCreate a Data SourceCreate a Data Source

� click the Browse button to locate

StudentDB access database then click OK

� click Next

ADO.NET * Property of STIPage 23 of 26

� select Use SQL Statements then click Next

Page 24: MELJUN CORTES Vb.net ado.net part_ii

Visual Basic .NET Programming

Demonstration: How to Demonstration: How to Demonstration: How to Demonstration: How to Create a Data SourceCreate a Data SourceCreate a Data SourceCreate a Data Source

� click the Query Builder button

ADO.NET * Property of STIPage 24 of 26

� select the Student table then click Addand Close button

Page 25: MELJUN CORTES Vb.net ado.net part_ii

Visual Basic .NET Programming

Demonstration: How to Demonstration: How to Demonstration: How to Demonstration: How to Create a Data SourceCreate a Data SourceCreate a Data SourceCreate a Data Source

� tick the (All Columns) checkbox

� click the OK button to save and close the

window

SELECT student.*

FROM student

� click Next then the Finish button

ADO.NET * Property of STIPage 25 of 26

� click Next then the Finish button

� right-click the OleDbDataAdapter1 and

select Generate DataSet then click the

OK button

� right-click on the OleDbDataAdapter1again then choose Preview Data

Page 26: MELJUN CORTES Vb.net ado.net part_ii

Visual Basic .NET Programming

Demonstration: How to Demonstration: How to Demonstration: How to Demonstration: How to Create a Data SourceCreate a Data SourceCreate a Data SourceCreate a Data Source

� click the Preview button to view data in

then click the Close button

� OleDbDataAdapter, OleDbConnection, and

dataset are now created. This new

dataset can be used in any .Net control of

your choice

ADO.NET * Property of STIPage 26 of 26

your choice