You can use TableMappings
for your DataAdapter
to get the correct table names:
using (var con = new SqlConnection(connectionString))
{
string sql = @"SELECT * FROM locGSP;
SELECT * FROM locCountry;
SELECT * FROM locMarketUnit";
using(var da = new SqlDataAdapter(sql, con))
{
// demonstrate the issue here:
DataSet dsWrong = new DataSet();
da.Fill(dsWrong); // now all tables are in this format: Table,Table1,Table2
// following will map the correct names to the tables
DataSet dsCorrect = new DataSet();
da.TableMappings.Add("Table", "locGSP");
da.TableMappings.Add("Table1", "locCountry");
da.TableMappings.Add("Table2", "locMarketUnit");
da.Fill(dsCorrect); // now we have the correct table-names: locGSP,locCountry,locMarketUnit
}
}
Here's a different way using a DataReader
and DataSet.Load
to fill the DataSet
:
using (var con = new SqlConnection(connectionString))
{
string sql = @"SELECT * FROM locGSP;
SELECT * FROM locCountry;
SELECT * FROM locMarketUnit";
using (var cmd = new SqlCommand(sql, con))
{
con.Open();
using (var rdr = cmd.ExecuteReader())
{
// after the next line the DataSet will have the correct table-names
ds.Load(rdr, LoadOption.OverwriteChanges, "locGSP", "locCountry", "locMarketUnit");
}
}
}
Background:
Populating a DataSet from a DataAdapter
Multiple Result Sets: If the DataAdapter encounters multiple result
sets, it creates multiple tables in the DataSet. The tables are given
an incremental default name of TableN, starting with "Table" for
Table0. If a table name is passed as an argument to the Fill method,
the tables are given an incremental default name of TableNameN,
starting with "TableName" for TableName0.