sqlHelper = new SqlHelper();
sqlHelper.OpenConnection();
int i;
String sqlSt = string.Empty;
for (i = 0; i < tag.Count; i++)
{
sqlSt = "Select TagID from TagsList where TagName= '" + tag[i] + "'";
ds = sqlHelper.ExecuteDataSet(sqlSt, CommandType.Text);
if (ds != null)
{
if (ds.Tables[0].Rows.Count > 0)
{
//dt = ds1.Tables[0];
ds.Tables[0].Rows.Add(ds);
}
}
}
return ds;
我想将一个DataSet添加到另一个DataSet,最后将返回累积的DataSet。
我收到错误 - Unable to cast object of type 'System.Data.DataSet' to type 'System.IConvertible'.Couldn't store <System.Data.DataSet>
编辑:我想要的是我得到的数据集包含一个特定的int值,每个类型我的for循环运行我想将所有这些记录添加到DataSet然后返回我的数据集
答案 0 :(得分:0)
我想知道这是否是一个错字,因为你将相同的DataSet添加到它的第一个表的行集合中:
ds.Tables [0] .Rows.Add(DS);
如果我理解正确,您有一个DataSet ds1
,并且您希望将其表格添加到ds
。为此,您需要分两步逐步移动:克隆表并导入其行。
例如,这会复制第一个表(索引0):
//clone first table
DataTable dt = ds1.Tables[0].Clone();
//add rows
foreach (DataRow dr in ds1.Tables[0].Rows)
{
dt.ImportRow(dr);
}
//add table to DataSet
ds.Tables.Add(dt);
<强>被修改强>:
我还会调查DataTableExtensions
CopyToDataTable。它相信你也可以做(未经测试):
//copy first table
DataTable dt = ds1.Tables[0].AsEnumerable().CopyToDataTable();
//add table to DataSet
ds.Tables.Add(dt);
答案 1 :(得分:0)
从技术上讲,我们无法将一个数据集添加到其他数据集,因为ASP.NET中的DataSet通常是只读的。
因此,首先我们必须创建第二个数据集的对象引用。
步骤:
bool IsFirstTime = true;(Create OutSide Loop)
内圈:
if (IsFirstTime)
{
// dsLoopData Contains Records
dsTotalRecords = dsLoopData.Clone();
IsFirstTime = false;
}
1.时间插入(批量复制)
dsTotalRecords.Tables[0].Merge(dsLoopData.Tables[0]);
2.Row By Row Insertion
for (int i = 0; i < dsLoopData.Tables[0].Rows.Count; i++)
{
dsTotalRecords.Tables[0].ImportRow(dsLoopData.Tables[0].Rows[i]);
}