Solved

How can I bring back multiplet tables in a dataset?

Posted on 2011-03-24
2
246 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 30

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

635 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