Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 774
  • Last Modified:

how to call sp_tables from C#

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);
 
        cn.Open();
        cmd.CommandType = CommandType.StoredProcedure;
        p1 = new SqlParameter();
        p1.Direction = ParameterDirection.Output;
        p1.SqlDbType = SqlDbType.VarChar;
        cmd.Parameters.Add(p1);
        cmd.ExecuteNonQuery();

Open in new window

0
aspgeek
Asked:
aspgeek
1 Solution
 
RiteshShahCommented:
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);
        conn.Open();
 
        //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();
        recordAdp.Fill(recordSet);
        command.Dispose();
        conn.Close();
 
        DropDownList1.DataSource = recordSet.Tables[0];
        DropDownList1.DataTextField = recordSet.Tables[0].Columns["Table_Name"].ToString();
        DropDownList1.DataValueField= recordSet.Tables[0].Columns["Table_Name"].ToString();
        DropDownList1.DataBind();

Open in new window

0
 
aspgeekAuthor Commented:
excellent !! Thanks for the immediate response.
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now