Solved

How can I bring back multiplet tables in a dataset?

Posted on 2011-03-24
2
242 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

777 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