如何将数据表对象添加到数据集和returnm数据集 [英] How to add datatable object to dataset and returnm dataset

查看:89
本文介绍了如何将数据表对象添加到数据集和returnm数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想将datatable对象添加到数据集.我可以一次将单个对象添加到数据集,但是现在我想从数据表中的多个表中选择数据.最后,我想将所有数据表添加到数据集并返回数据集.

我无法将数据表追加到数据集.

以下是我的代码,其中tablename是表名称的列表:

Hi,

I want to add datatable object to dataset. I can add at a time single object to dataset but now I want to select data from multiple tables in datatables. Finally, I want to add all datatables to dataset and return dataset.

I am unable to append datatable to dataset.

Following is the code I have where tablename is the list of table names:

public DataSet MultipleSelectRecord(string databasename,List<string>tablename)
       {
           try
           {
              DataSet AppendDs =new DataSet ();
               for (int i = 0; i < tablename.Count; i++)
               {
                   string query = " USE " + databasename + " SELECT * FROM " + tablename[i] + " ";
                   da = new SqlDataAdapter(query , con);
                   DataTable dtobj = new DataTable();
                   da.Fill(dtobj);
                  
                   AppendDs.Tables.Add(dtobj);//it replace previous dataset record this is problem
               }
               return AppendDs;
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message);
           }

       }</string>



谢谢.

添加了前置标记.



Thank you.

Added pre tag.

推荐答案

此代码将无法编译.也许您错过了粘贴的原因.除此之外,对我来说还不错.调试器说什么?

除非DataSet声明在实际代码中的循环内,否则应添加表.
This code will not compile. Maybe you missed why pasting it. Other than that it looks fine to me. What does the debugger say?

Unless DataSet declaration is inside the loop in actual code, it should add tables.


这篇关于如何将数据表对象添加到数据集和returnm数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆