Dataset holds a table called "Table", not the table I pass in?
Posted
by dotnetdev
on Stack Overflow
See other posts from Stack Overflow
or by dotnetdev
Published on 2010-04-04T21:31:36Z
Indexed on
2010/04/04
21:53 UTC
Read the original article
Hit count: 235
c#
Hi,
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
© Stack Overflow or respective owner