2

I used a dataset to store 15 tables that I need at the time of loading. When i filled all the tables using stored procedure it returns me all the table but name of the table doesn't comes as that of actual table name in a database.

It takes all the table with table name as Table1, Table2, Table3...

I want them to be with the name as they actually are in table.

SELECT PK_GUEST_TYPE, [DESCRIPTION] FROM L_GUEST_TYPE
SELECT PK_AGE_GROUP_ID, AGE_GROUP FROM L_AGE_GROUP
SELECT PK_COMPANY_ID, COMPANY_NAME FROM M_COMPANY
SELECT PK_COUNTRY_ID, COUNTRY FROM L_COUNTRY
SELECT PK_EYE_COLOR_ID, [DESCRIPTION] FROM L_EYE_COLOR
SELECT PK_GENDER_ID, [DESCRIPTION] FROM L_GENDER
SELECT PK_HAIR_COLOR_ID, [DESCRIPTION] FROM L_HAIR_COLOR
SELECT PK_STATE_PROVONCE_ID, [DESCRIPTION] FROM L_STATE_PROVINCE
SELECT PK_STATUS_ID, [DESCRIPTION] FROM L_STATUS

SELECT PK_TITLE_ID, [DESCRIPTION] FROM L_TITLE
SELECT PK_TOWER_ID, [DESCRIPTION] FROM M_TOWER
SELECT PK_CITY_ID, [DESCRIPTION] FROM L_CITY
SELECT PK_REGISTER_TYPE_ID, [DESCRIPTION] FROM L_REGISTER_TYPE

Here is my frontend coding to fill dataset.

             OpenConnection();
             adp.Fill(ds);
             CloseConnection(true);
Shantanu Gupta
  • 20,688
  • 54
  • 182
  • 286
  • 3
    You cannot do that - the SqlDataAdapter will *not* look at the physical table names in your database to determine the table names in the ADO.NET DataSet. Sorry, there's really no way to do this automagically. – marc_s May 10 '10 at 11:37

5 Answers5

3

May be this could be the work around by adding an extra column in returning table

Create procedure psfoo ()
AS
select * ,'tbA' as TableName from tbA
select * ,'tbB' as TableName from tbB

Then in C# code

 foreach (DataTable dt in ds.Tables)
    {
        if (dt.Rows[0]["TableName"].ToString().Contains("tbA"))
        {

        }
        else if (dt.Rows[0]["TableName"].ToString().Contains("tbB"))
        {

        }
    }
vinayak hegde
  • 2,117
  • 26
  • 26
2

Probably this would help Mapping Data Source Tables to Dataset Tables

KMån
  • 9,896
  • 2
  • 31
  • 41
  • Page moved, archive at: https://web.archive.org/web/20141005072324/http://msdn.microsoft.com/en-us/library/aa728893(VS.71).aspx – admalledd Feb 23 '18 at 22:52
1

I would have invested time to use typed dataset, makes a lot of things much easier. Remember you probarly will come back to this code in a month or three. :)

Slampen
  • 790
  • 9
  • 21
0

Have the first (or last) table a meta table of table names in the same order as the following (or preceding) tables.

Lloyd
  • 29,197
  • 4
  • 84
  • 98
  • thx, That could be a solution to the problem that I want right now. But this is not a actual answer. I can see in adp.fill function's overload that it has one of the overload that has datatable array which can map table names but it is looking for other two parameters as well which i dont want to give. i.e. startRow, max records to retrieve – Shantanu Gupta May 10 '10 at 10:16
  • 1
    @Shantanu: Lloyd meant, return meta from your SP(eg: like `SELECT 'L_GUEST_TYPE', 'L_AGE_GROUP'...`) and fill in the `.TableName` property as `ds.Table[0].TableName = ds.Table[nLastTable].Rows[0][0].ToString()` – KMån May 10 '10 at 10:41
0

MS table mapping is a total joke. What is the difference between

ds.Table(0)
ds.Table("Table")
ds.Table("Customer")

when we have no guarantee of the order of tables returned within our application. The need is a STRONG-NAME match.... See my solution

Community
  • 1
  • 1
davidWazy
  • 61
  • 6