how to call sp_tables from C#

Posted on 2009-04-17
Last Modified: 2013-12-17
I need to populate a Dropdown with the table names in the databse using sp_tables stored proc. Can someone please help me on how to do it.
string con = "------------------------------------"

        cn = new SqlConnection(con);

        cmd = new SqlCommand("sp_tables", cn);


        cmd.CommandType = CommandType.StoredProcedure;

        p1 = new SqlParameter();

        p1.Direction = ParameterDirection.Output;

        p1.SqlDbType = SqlDbType.VarChar;



Open in new window

Question by:aspgeek
    LVL 31

    Accepted Solution

    here you go.

    //create connection string and assign it to SqlConnection object
            string strConn = "Data Source=.;Initial Catalog=AdventureWorks;Persist Security Info=True;User ID=sa;Password=sa";
            System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(strConn);
            //setup SqlCommand and assign SP name along with input parameter variable name and value
            System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand();
            command.CommandType = System.Data.CommandType.StoredProcedure;
            command.Connection = conn;
            command.CommandText = "SP_Tables";
            //create one data adapter which will execute the command and fill the data into data set
            System.Data.SqlClient.SqlDataAdapter recordAdp = new System.Data.SqlClient.SqlDataAdapter();
            recordAdp.SelectCommand = command;
            System.Data.DataSet recordSet = new System.Data.DataSet();
            DropDownList1.DataSource = recordSet.Tables[0];
            DropDownList1.DataTextField = recordSet.Tables[0].Columns["Table_Name"].ToString();
            DropDownList1.DataValueField= recordSet.Tables[0].Columns["Table_Name"].ToString();

    Open in new window


    Author Closing Comment

    excellent !! Thanks for the immediate response.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
    This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
    Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
    Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

    760 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

    13 Experts available now in Live!

    Get 1:1 Help Now