2013-01-16 57 views
0
  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; 

我想将一个数据集添加到另一个数据集,最后返回累积数据集。将数据集添加到另一个数据集(如C#中的DataRow)

我得到一个错误 - Unable to cast object of type 'System.Data.DataSet' to type 'System.IConvertible'.Couldn't store <System.Data.DataSet>

编辑:要我要的是我得到的是由一个特定的int值的各型我的for循环的运行,我想所有的这些记录添加到DataSet该数据集,然后回到我的数据集

+0

您必须逐行排列DataTable。而不是Add()使用ImportRow() –

回答

0

我不知道这是一个错字,因为要添加相同的数据集,以它的第一个表的行集:

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); 

编辑: 我也将调查DataTableExtensionsCopyToDataTable。它相信你也可以这样做(未经测试):

//copy first table 
DataTable dt = ds1.Tables[0].AsEnumerable().CopyToDataTable();  
//add table to DataSet 
ds.Tables.Add(dt); 
+0

请检查我的编辑 – vini

+0

如果我理解正确,您将检查一个值并根据此值添加记录?最后返回数据集? – Ulises

0

技术上我们不能添加一个数据集为其他数据集,因为在ASP.NET数据集通常是只读的。

因此,首先我们要创建第二个DataSet的对象引用。

步骤:

bool IsFirstTime = true;(Create OutSide Loop) 

内环:

if (IsFirstTime) 

{ 

// dsLoopData Contains Records 

dsTotalRecords = dsLoopData.Clone(); 

IsFirstTime = false; 

} 

1.At一次插入(对于批量复制)

dsTotalRecords.Tables[0].Merge(dsLoopData.Tables[0]); 

2.Row按行插入

for (int i = 0; i < dsLoopData.Tables[0].Rows.Count; i++) 

{ 

dsTotalRecords.Tables[0].ImportRow(dsLoopData.Tables[0].Rows[i]); 

} 
相关问题