ASP.NET & Databases Part 2
page 3 of 5
by . .
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 24772/ 65

DataTables

DataTables

A DataTable is simply one table of data (much like the recordset) and you can do things like get data, modify data and all that kind of stuff. You can either create a DataTable like that or we can create a DataTable from a dataset.

Dim dtable As DataTable = ds1.Tables("people")

This fills dtable with the table at ds1.Tables("people"). The table structure and data is copied into a DataTable object. However it's disconnected but it still remains a link to the dataset, so if we add a row and then apply the changes we can update the dataset.

Dim row as DataRow = dtable.NewRow()
row(0) = "Philip"
row(1) = "Quinn"
dtable.Rows.Add(row)

dtable.AcceptChanges

This creates a DataRow. The dtable.NewRow() returns a datarow with the same syntax as the columns, so row(0) is FName. Then we add the row to the DataTable.
When we use dtable.AcceptChanges, because we got the table from the dataset, it then updates the dataset to include those changes.

<%@ Import Namespace="System.Data" %>
<script language="VB" runat="server">
Sub Page_Load(sender as object, e as eventargs)
Dim ds1 As New DataSet()
Dim dtable As new DataTable("people")
With dtable.Columns
.Add("FName", System.Type.GetType("System.String"))
.Add("LName", System.Type.GetType("System.String"))
.Add("UID", System.Type.GetType("System.Int32"))
End With

dtable.Columns("UID").AutoIncrement = True
ds1.Tables.Add(dtable)

Dim dtable2 As DataTable = ds1.Tables("people")

Dim row as DataRow = dtable2.NewRow()
row(0) = "Philip"
row(1) = "Quinn"
dtable2.Rows.Add(row)
dtable2.AcceptChanges

Response.Write(ds1.Tables("people").Rows(0)("FName").ToString)
End Sub
</script>

This should print "Philip" to the screen. A quick breakdown of this is (incase you haven't been reading):

1.      We create a new DataSet and a DataTable called "people"

2.      Added three columns and set their properties

3.      Added them to the DataSet

4.      Created another DataTable. This table was a copy of the one we had just put into the dataset.

5.      We created a DataRow using the syntax of the DataTable and put some data into it.

6.      We then accepted the changes which then updated the dataset.

7.      And printed the First column (which happens to be "FName") of the first row to the screen

There are also other methods that you may want to use with a DataTable.
We'll be covering these as we use them. So far we have seen what a DataTable is and how you can create them and then use them to edit a dataset (with the help of a DataRow). Next we'll take a look at the DataRow


View Entire Article

User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2021 ASPAlliance.com  |  Page Processed at 2021-12-07 8:11:37 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search