Creating A DataSet Programmatically

A

Arpan

The following ASPX code snippet creates a DataSet programmatically right from the scratch:

'create an empty DataSet
Dim objDS As New DataSet("MyDataSet")

'create a new table & add columns
Dim dTable As New DataTable("Users")
dTable.Columns.Add("ID",System.Type.GetType("System.Int32"))
dTable.Columns.Add("FirstName",System.Type.GetType("System.String"))
dTable.Columns.Add("LastName",System.Type.GetType("System.String"))
dTable.Columns("ID").AutoIncrement=True

'add the new table
objDS.Tables.Add(dTable)

'define the Primary Key
Dim keys As DataColumn={objDS.Tables("Users").Columns("ID")}
objDS.Tables("Users").PrimaryKey=keys

'add a row to this table
Dim dRow As DataRow=dTable.NewRow()
dRow(1)="Michael"
dRow(2)="Johnson"
dTable.Rows.Add(dRow)

As such, the above code doesn't generate any errors but how do I access this DataSet i.e. how do I convert it into an actual file or insert it in a database so that I can access the data that it stores (presently it stores only one row - Michael as the FirstName & Johnson as the LastName)?

Thanks,

Arpan
 
V

V. Jenks

You can access this in a number of ways or even write it to
disk, that's actually quite a loaded question. If you had
done a little research you'd have quickly found answers to
this.

You can write a DataSet to disk and read it back into a
DataSet in memory whenever you like, you may also generate
schemas for the XML you generate, see here:

http://samples.gotdotnet.com/quickstart/howto/doc/Xml/SaveDataSetXML.aspx

As for reading into a database, there are a number of ways
to do this. A DataSet is a database represented in memory
and is composed of XML (to make a long story short).

It is comprised of DataTables, DataViews, DataRelations,
etc. You can use DataReaders to read data out of the
DataSet or you can use Commands to write data into your
database. You can even create relationships between
DataTables in memory and enforce integrity.

There's obviously much more, try these links for starters:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconxmldataset.asp

http://www.c-sharpcorner.com/database/ado.net.tut.sh.asp

http://www.411asp.net/home/tutorial/specific/database/classes/dataset

-----Original Message-----
The following ASPX code snippet creates a DataSet
programmatically right from the scratch:
'create an empty DataSet
Dim objDS As New DataSet("MyDataSet")

'create a new table & add columns
Dim dTable As New DataTable("Users")
dTable.Columns.Add("ID",System.Type.GetType("System.Int32"))
dTable.Columns.Add("FirstName",System.Type.GetType("System.String"))
dTable.Columns.Add("LastName",System.Type.GetType("System.String"))
dTable.Columns("ID").AutoIncrement=True

'add the new table
objDS.Tables.Add(dTable)

'define the Primary Key
Dim keys As DataColumn={objDS.Tables("Users").Columns("ID")}
objDS.Tables("Users").PrimaryKey=keys

'add a row to this table
Dim dRow As DataRow=dTable.NewRow()
dRow(1)="Michael"
dRow(2)="Johnson"
dTable.Rows.Add(dRow)

As such, the above code doesn't generate any errors but
how do I access this DataSet i.e. how do I convert it into
an actual file or insert it in a database so that I can
access the data that it stores (presently it stores only
one row - Michael as the FirstName & Johnson as the LastName)?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top