2

I am using ADO.NET to execute store procedure. The store procedure is having multiple select statement. When I access DataSet, the tables are named as Table1, Table2 and so on. I want to give user friend name to each of these table. I do not want to use Table variable or temp tables in my SQL query. DO I have any other alternatives?

I am using following code to get the dataset

SqlCommand cmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();

DataSet ds = new DataSet();
try
{
    con.ConnectionString = ConfigurationManager.ConnectionStrings["connString"].ConnectionString;
    con.Open();
    cmd = new SqlCommand("sp_GetData", con);
    cmd.Parameters.Add(new SqlParameter("@ParamOne", param));
    cmd.CommandType = CommandType.StoredProcedure;
    da.SelectCommand = cmd;
    da.Fill(ds);

}

I dont want to do this either

da.TableMappings.Add("Table", "MyTable1");
da.TableMappings.Add("Table1", "MyTable2");
da.TableMappings.Add("Table2", "MyTable3");

or this

ds.Tables[0].TableName = "NametbA";
ds.Tables[1].TableName = "NametbB";

Preferably I want to specify the name in the SQL query. The reason I want to use this approach is because, I will pass this dataset as it to a function which will write the table name into a file.

Please provide your suggestions.

Thank you

iabbott
  • 873
  • 1
  • 8
  • 23
SharpCoder
  • 18,279
  • 43
  • 153
  • 249

2 Answers2

1

It is unfortunately not possible to set it automatically. You will have to provide it to the code somehow.

One option would be to change the structure of your results to have twice as many result sets where the odd one is the name and the even is the data:

-- Table name
SELECT 'nameoftable' AS TableName

-- Data
SELECT * FROM ...

c# code (consider it to be psudo code):

myDataSet.Tables[1].TableName = myDataSet.Tables[0]["TableName"].ToString();
Asken
  • 7,679
  • 10
  • 45
  • 77
-1

Table names in the ADO.Net dataset object are entirely .Net, C# (or vb.net) specific. They have nothing to do with the table names in the SQL query or in the database. Change them in your C# code, by simply writing

 myDataSet.Tables[0].TableName 'WhateverYouWant";
Charles Bretana
  • 143,358
  • 22
  • 150
  • 216