I have the code below:
string SQL = "select * from " + TableName;
using (DS = new DataSet())
using (SqlDataAdapter adapter = new SqlDataAdapter())
using (SqlConnection sqlconn = new SqlConnection(connectionStringBuilder.ToString()))
using (SqlCommand objCommand = new SqlCommand(SQL, sqlconn))
{
sqlconn.Open();
adapter.SelectCommand = objCommand;
adapter.Fill(DS);
}
System.Windows.Forms.MessageBox.Show(DS.Tables[0].TableName);
return DS;
However, every time I run this code, the dataset (DS) is filled with one table called “Table”. It does not represent the table name I pass in as the parameter TableName and this parameter does not get mutated so I don’t know where the name Table comes from. I’d expect the table to be the same as the tableName parameter I pass in?
Any idea why this is not so?
EDIT: Important fact: This code needs to return a dataset because I use the dataRelation object in another method, which is dependent on this, and without using a dataset, that method throws an exception. The code for that method is:
DataRelation PartToIntersection = new DataRelation("XYZ",
this.LoadDataToTable(tableName).Tables[tableName].Columns[0],
// Treating the PartStat table as the parent - .N
this.LoadDataToTable("PartProducts").Tables["PartProducts"].Columns[0]);
// 1
// PartsProducts (intersection) to ProductMaterial
DataRelation ProductMaterialToIntersection =
new DataRelation("", ds.Tables["ProductMaterial"].Columns[0],
ds.Tables["PartsProducts"].Columns[1]);
Thanks
The default name for the first table in the DataSet is
Table, the second one will be calledTable1and the thirdTable2and so forth. The DataSet will not read the table name from the underlying store – and there’s no option to make it do so.This is documented behavior – see the MSDN documentation:
If you want other names, you have to supply those – use this statement:
This will name the newly filled table
Tablenameinside the DataSet.