C# 在数据集中添加数据表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12178823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
adding a datatable in a dataset
提问by Manikandan Sigamani
I'm adding a datatable to a dataset like this:
我正在向这样的数据集添加数据表:
DataTable dtImage = new DataTable();
//some updates in the Datatable
ds.Tables.Add(dtImage);
But the next time, when the datatable gets updated, will it be reflected in the dataset? or we need to write some code to make it reflected?
但是下一次,当数据表更新时,它会反映在数据集中吗?或者我们需要编写一些代码来反映它?
Also, I'm checking the dataset if the datatable exists in the dataset already using:
此外,我正在检查数据集是否存在于已使用的数据集中:
if(!ds.Tables.Contains("dtImage"))
ds.Tables.Add(dtImage);
In the first iteration, ds.Tables.Contains("dtImage")is false, so, ds.Tables.Add(dtImage)
adds the table to the dataset. But in the second iteration, ds.Tables.Contains("dtImage")is false again, but ds.Tables.Add(dtImage)throws an error:
在第一次迭代中,ds.Tables.Contains("dtImage")为假,因此,ds.Tables.Add(dtImage)
将表添加到数据集。但是在第二次迭代中,又ds.Tables.Contains("dtImage")是false,却ds.Tables.Add(dtImage)抛出了错误:
Datatable already belongs to this dataset.
数据表已经属于这个数据集。
If the dataset doesn't contain the datatable named "dtImage", why is it throwing an error?
如果数据集不包含名为“dtImage”的数据表,为什么会抛出错误?
Update: Thanks, that issue got solved. Pls answer this:
更新:谢谢,问题解决了。请回答这个:
But the next time, when the datatable gets updated, will it be reflected in the dataset? or we need to write some code to make it reflected?
但是下一次,当数据表更新时,它会反映在数据集中吗?或者我们需要编写一些代码来反映它?
采纳答案by Tim Schmelter
I assume that you haven't set the TableNameproperty of the DataTable, for example via constructor:
我假设您尚未设置TableNameDataTable的属性,例如通过构造函数:
var tbl = new DataTable("dtImage");
If you don't provide a name, it will be automatically created with "Table1", the next table will get "Table2"and so on.
如果您不提供名称,它将自动创建"Table1",下一个表将得到"Table2"等等。
Then the solution would be to provide the TableNameand then check with Contains(nameOfTable).
那么解决方案是提供TableName,然后检查Contains(nameOfTable)。
To clarifyit: You'll get an ArgumentExceptionif that DataTable already belongs to the DataSet (the same reference). You'll get a DuplicateNameExceptionif there's already a DataTable in the DataSet with the same name(not case-sensitive).
为了澄清它:你会得到一个ArgumentException如果数据表已经属于数据集(相同的)。DuplicateNameException如果 DataSet 中已经有一个同名的 DataTable(不区分大小写),您将得到一个。
回答by Hassan Boutougha
you have to set the tableName you want to your dtimage that is for instance
你必须将你想要的 tableName 设置为你的 dtimage,例如
dtImage.TableName="mydtimage";
if(!ds.Tables.Contains(dtImage.TableName))
ds.Tables.Add(dtImage);
it will be reflected in dataset because dataset is a container of your datatable dtimage and you have a reference on your dtimage
它将反映在数据集中,因为数据集是数据表 dtimage 的容器,并且您对 dtimage 有参考
回答by Himanshu Shukla
Just give any name to the DataTable Like:
只需为 DataTable 提供任何名称,例如:
DataTable dt = new DataTable();
dt = SecondDataTable.Copy();
dt .TableName = "New Name";
DataSet.Tables.Add(dt );
回答by Ata Hoseini
DataSet ds = new DataSet();
DataTable activity = DTsetgvActivity.Copy();
activity.TableName = "activity";
ds.Tables.Add(activity);
DataTable Honer = DTsetgvHoner.Copy();
Honer.TableName = "Honer";
ds.Tables.Add(Honer);
DataTable Property = DTsetgvProperty.Copy();
Property.TableName = "Property";
ds.Tables.Add(Property);
DataTable Income = DTsetgvIncome.Copy();
Income.TableName = "Income";
ds.Tables.Add(Income);
DataTable Dependant = DTsetgvDependant.Copy();
Dependant.TableName = "Dependant";
ds.Tables.Add(Dependant);
DataTable Insurance = DTsetgvInsurance.Copy();
Insurance.TableName = "Insurance";
ds.Tables.Add(Insurance);
DataTable Sacrifice = DTsetgvSacrifice.Copy();
Sacrifice.TableName = "Sacrifice";
ds.Tables.Add(Sacrifice);
DataTable Request = DTsetgvRequest.Copy();
Request.TableName = "Request";
ds.Tables.Add(Request);
DataTable Branchs = DTsetgvBranchs.Copy();
Branchs.TableName = "Branchs";
ds.Tables.Add(Branchs);

