DataTable ImportRow方法 代替 insert方法 和add 方法

来源:互联网 发布:淘宝能评论几次 编辑:程序博客网 时间:2024/05/22 10:28

大家可能遇到这样的问题:this   row   already   belongs   to   another   table  

 

 

我也在网上找了一段时间,可是还没有解决方案,最后就用 ImportRow方法代替了,唉!!!

 

 

 DataSet ds = ForumPostInfoProvider.SelectForumPosts(this.ForumID, "/%", null, orderBy, 0, !ForumContext.UserIsModerator(this.ForumID, this.CommunityGroupID), -1);
            DataSet ds1 = new DataSet();
            DataTable dt = new DataTable();

            dt = ds.Tables[0].Clone();
            if (ds != null)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    int PostForumID = (int)ds.Tables[0].Rows[i]["PostForumID"];
                    string path = ds.Tables[0].Rows[i]["PostIDPath"].ToString();
                    DataSet da = ForumPostInfoProvider.SelectForumPosts(ForumID, "/%", "PostIDPath Like '" + path + "%'", this.ThreadOrderBy, 3, true);
                    for (int j = 0; j < da.Tables[0].Rows.Count; j++)
                    {
                        DataRow dr = da.Tables[0].Rows[j];
                        dt.ImportRow(dr);
                    }
                }
            }
            ds1.Tables.Add(dt);

 

 

上面带有背景颜色的代码,要小心了,一开始我用的是:

           DataSet ds1 = new DataSet();
            DataTable dt = new DataTable();

会报错的,不可以随便用null啊,还是New一个安全。


原创粉丝点击