簡體   English   中英

將數據集添加到另一個數據集,例如C#中的DataRow

[英]Add Dataset to Another Dataset Like a DataRow in C#

            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。

我遇到錯誤- Unable to cast object of type 'System.Data.DataSet' to type 'System.IConvertible'.Couldn't store <System.Data.DataSet>

編輯:想要我想要的是我得到的數據集包含一個特定的int值,每種類型我的for循環運行,我想將所有這些記錄添加到數據集中,然后返回我的數據集

我想知道這是否是錯字,因為您要將相同的數據集添加到其第一個表的行集合中:

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

從技術上講,我們無法將一個數據集添加到其他數據集,因為ASP.NET中的數據集通常是只讀的。

因此,首先,我們必須創建第二個數據集的對象引用。

腳步 :

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.逐行插入

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

{

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM