Table name's in Dataset

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello Guys,

I have a proc which returns variable number of tables. I want to do some
validations on these tables and dont want to use the index (e.g
Dataset.Tables(0)) to refer to the table, I want to use the tablenames
instead. I cant do a tablemapping either caus I dont know which table will be
returned at which position.

Is there a way to get the table names returned by the proc in the dataset
instead of the default names like Table1, Table2 ...

Thanks in advance...
 
There wasn't a reliable way to always determine what the table name is, so
the first table name is provided by the caller and the following tables are
generated off of that.

For what you want to do, here is a C# code sample that may do what you want.

internal sealed class DynamicallyNameTableAdapter : DataAdapter {
internal int FillFromReader(DataSet set, DbCommand command)
{
int count = 0;
using(DbDataReader reader =
command.ExecuteReader(CommandBehavior.SequentialAccess|CommandBehavior.KeyInfo)) {
do {
DataTable table = null;
string tableName =
(reader.GetSchemaTable().Rows[0][SchemaTableColumn.BaseTableName] as string);
if (!String.IsNullOrEmpty(tableName)) {
table = set.Tables[tableName];
}
if (null == table) {
table = new DataTable(tableName);
set.Tables.Add(table);
}

count += Fill(new DataTable[] { table }, reader, 0, 0);
} while(reader.NextResult());
}
return count;
}
}

private static void MultiTableName() {
SqlDataAdapter adapter = ...;
DataSet set = ...;

int count = (new DynamicallyNameTableAdapter()).FillFromReader(set,
adapter.SelectCommand);
foreach(DataTable table in set.Tables) {
Console.WriteLine("{0}: {1}", table.TableName, table.Rows.Count);
}
}
 
Hi Mark,

Thanks for the realy.

It seems that the "SchemaTableColumn.BaseTableName" is newly introduced in
framework 2.0 and I forgot to mention that I am using framework 1.1. I'll see
if I cant find way in 1.1.

Any ways thanks again for the reply.

Mark Ashton said:
There wasn't a reliable way to always determine what the table name is, so
the first table name is provided by the caller and the following tables are
generated off of that.

For what you want to do, here is a C# code sample that may do what you want.

internal sealed class DynamicallyNameTableAdapter : DataAdapter {
internal int FillFromReader(DataSet set, DbCommand command)
{
int count = 0;
using(DbDataReader reader =
command.ExecuteReader(CommandBehavior.SequentialAccess|CommandBehavior.KeyInfo)) {
do {
DataTable table = null;
string tableName =
(reader.GetSchemaTable().Rows[0][SchemaTableColumn.BaseTableName] as string);
if (!String.IsNullOrEmpty(tableName)) {
table = set.Tables[tableName];
}
if (null == table) {
table = new DataTable(tableName);
set.Tables.Add(table);
}

count += Fill(new DataTable[] { table }, reader, 0, 0);
} while(reader.NextResult());
}
return count;
}
}

private static void MultiTableName() {
SqlDataAdapter adapter = ...;
DataSet set = ...;

int count = (new DynamicallyNameTableAdapter()).FillFromReader(set,
adapter.SelectCommand);
foreach(DataTable table in set.Tables) {
Console.WriteLine("{0}: {1}", table.TableName, table.Rows.Count);
}
}

Navnit said:
Hello Guys,

I have a proc which returns variable number of tables. I want to do some
validations on these tables and dont want to use the index (e.g
Dataset.Tables(0)) to refer to the table, I want to use the tablenames
instead. I cant do a tablemapping either caus I dont know which table will be
returned at which position.

Is there a way to get the table names returned by the proc in the dataset
instead of the default names like Table1, Table2 ...

Thanks in advance...
 
SchemaTableColumn.BaseTableName is string field for ""BaseTableName"

Navnit said:
Hi Mark,

Thanks for the realy.

It seems that the "SchemaTableColumn.BaseTableName" is newly introduced in
framework 2.0 and I forgot to mention that I am using framework 1.1. I'll see
if I cant find way in 1.1.

Any ways thanks again for the reply.

Mark Ashton said:
There wasn't a reliable way to always determine what the table name is, so
the first table name is provided by the caller and the following tables are
generated off of that.

For what you want to do, here is a C# code sample that may do what you want.

internal sealed class DynamicallyNameTableAdapter : DataAdapter {
internal int FillFromReader(DataSet set, DbCommand command)
{
int count = 0;
using(DbDataReader reader =
command.ExecuteReader(CommandBehavior.SequentialAccess|CommandBehavior.KeyInfo)) {
do {
DataTable table = null;
string tableName =
(reader.GetSchemaTable().Rows[0][SchemaTableColumn.BaseTableName] as string);
if (!String.IsNullOrEmpty(tableName)) {
table = set.Tables[tableName];
}
if (null == table) {
table = new DataTable(tableName);
set.Tables.Add(table);
}

count += Fill(new DataTable[] { table }, reader, 0, 0);
} while(reader.NextResult());
}
return count;
}
}

private static void MultiTableName() {
SqlDataAdapter adapter = ...;
DataSet set = ...;

int count = (new DynamicallyNameTableAdapter()).FillFromReader(set,
adapter.SelectCommand);
foreach(DataTable table in set.Tables) {
Console.WriteLine("{0}: {1}", table.TableName, table.Rows.Count);
}
}

Navnit said:
Hello Guys,

I have a proc which returns variable number of tables. I want to do some
validations on these tables and dont want to use the index (e.g
Dataset.Tables(0)) to refer to the table, I want to use the tablenames
instead. I cant do a tablemapping either caus I dont know which table will be
returned at which position.

Is there a way to get the table names returned by the proc in the dataset
instead of the default names like Table1, Table2 ...

Thanks in advance...
 
Back
Top