accessing the dataset tables, by name and not by index

  • Thread starter Thread starter roni
  • Start date Start date
R

roni

hi.

i created a command that get 2 tables from db.

when try to access the tables in the dataset , i can't access the tables by
their names(by their original name from db ),only by index.

is there a way to access them by name ? (meaning the dataset will automatic
will set the real name of the db table ).
 
Hi there,

When you use DataAdapter to fill a DataSet, unless you give specified table
name, it assigns default table name, ‘Table’. For example, in following code

SqlDataAdapter dap = new SqlDataAdapter(“Select * From table_one; Select *
From table_twoâ€, CONNECTION_STRING);
DataSet ds = new DataSet();
Dap.Fill(ds);

Two tables are in ds, their table names are ‘Table’ and ‘Table1’ respectively.

Or

dap.Fill(ds, “Table_Oneâ€);

You have table name ‘Table_One’ for the first table and ‘Table_One1’ for
second table.

HTH

Elton Wang
(e-mail address removed)
 
Am Wed, 13 Jul 2005 13:03:58 +0200 schrieb roni:
i created a command that get 2 tables from db.

when try to access the tables in the dataset , i can't access the tables by
their names(by their original name from db ),only by index.

is there a way to access them by name ? (meaning the dataset will automatic
will set the real name of the db table ).

There are few ways to access the tables of your database. Depending what
kind of dataset you use.

myDataset.Tables["myTable"]
myDataset.Tables[0]

myDataset.myTable //typed dataset
myDataset.myTable[0][1] //typed dataset (row and column access)

You must add a table mapping when you fill the data adapter.

This is C# syntax.

Mfg

Frank Loizzi
Germany
 
Hi Roni,

You can refer to a table within a dataset through its pseudonym, which may
be established when you build your dataset. Here's an example (oconn is the
connection object):
Dim ocmd As New SqlCommand("select * from evcodes", oconn)

Dim oda As New SqlDataAdapter(ocmd)

Dim ods As New DataSet("Event Codes")

Try

oconn.Open()

Catch ex As Exception

MessageBox.Show(ex.Message)

End Try

oda.Fill(ods, "Event Codes")

Dim irow As DataRow

Dim colpk(0) As DataColumn

colpk(0) = ods.Tables(0).Columns("eventcd")

colpk(0) = ods.Tables("Event Codes").Columns("eventcd")

' either of the two lines above will work

HTH,

Bernie Yaeger
 
Back
Top