Solved

How can I bring back multiplet tables in a dataset?

Posted on 2011-03-24
2
243 Views
Last Modified: 2012-05-11
Hi, I'm using asp.net 3.5, C#
How can I modified my current method to return multiple tables in a same dataset?  Thank you

    public DataSet SelectFromATable(string tableName)
    {
        string query = "select * from @tableName";
        SqlCommand cmd = new SqlCommand(query);
        cmd.Parameters.Add(@tableName, SqlDbType.NVarChar);
        cmd.Parameters["@tableName"].Value = tableName;
        return FillDataSet(cmd, tableName);
    }

   private DataSet FillDataSet(SqlCommand cmd, string tableName)
    {
        SqlConnection con = new SqlConnection(connectionString);
        cmd.Connection = con;
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        try
        {
            con.Open();
            adapter.Fill(ds, tableName);

        }
        finally
        {
            con.Close();
            con.Dispose();
        }
        return ds;
    }
}
0
Comment
Question by:lapucca
2 Comments
 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 35212473
I didn't test but you may use approach something like below. I changed method signatures. The public method accepts list of table names as a parameter.
public DataSet SelectFromTables(List<string> tableNames)
        {
            DataSet ds = new DataSet();
            foreach(string tableName in tableNames)
            {
                SelectFromATable( tableName,  ds);
            }
            return ds;
        }

        private void SelectFromATable(string tableName, DataSet ds)
        {
            string query = "select * from @tableName";
            SqlCommand cmd = new SqlCommand(query);
            cmd.Parameters.Add(@tableName, SqlDbType.NVarChar);
            cmd.Parameters["@tableName"].Value = tableName;
            FillDataSet(cmd, tableName, ds);
        }

        private void FillDataSet(SqlCommand cmd, string tableName, DataSet ds)
        {
            SqlConnection con = new SqlConnection(connectionString);
            cmd.Connection = con;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            try
            {
                con.Open();
                adapter.Fill(ds, tableName);

            }
            finally
            {
                con.Close();
                con.Dispose();
            }
        }

Open in new window

0
 

Author Closing Comment

by:lapucca
ID: 35212581
smart!  Thank you.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question