Solved

How can I bring back multiplet tables in a dataset?

Posted on 2011-03-24
2
240 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
Comment Utility
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
Comment Utility
smart!  Thank you.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now