Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How can I bring back multiplet tables in a dataset?

Posted on 2011-03-24
2
Medium Priority
?
249 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 30

Accepted Solution

by:
anarki_jimbel earned 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

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