Pieter

Pieter

  • NA
  • 3
  • 2.3k

List SQL stored procedures for specific database based on combobox selection

Jun 3 2012 2:37 PM
Hi. Noob asking. I have a windows form that needs to enable you to select a database from a combobox, and then enable you to list and execute the stored procedures in the listbox separately. I have already established a connection to SQL and have loaded the database names to the combobox. I'm struggling to find a way to list the stored procedures to the listbox based on the database selected. I've tried using a query in SQL that lists all the sp's, but it only lists the sp's in the master db. So trying to call them with parameters to the listbox feels like a dead end to me. Any ideas will be greatly appreciated.

Here is what I have so far:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SqlServer.Server;
using System.IO;
using System.Data.SqlClient;

namespace StoredProcedureExecution_Pieter_Bekker
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                LoadCombo();
            }
            catch (Exception)
            {

                err.SetError(cboSelectDatabase,"Problem loading databases. Please contact the system administrator.");    
            }
        }

        private void LoadCombo()
        {
            String conxString = (@"Data Source = WOLF-PC\WOLF; Integrated Security=True;");
            using (SqlConnection conn = new SqlConnection(conxString))
            {
                conn.Open();
                DataTable tblDatabases = conn.GetSchema("Databases");
                conn.Close();

                foreach (DataRow row in tblDatabases.Rows)
                {
                    cboSelectDatabase.Items.Add(row["database_name"]);
                }
            }
        }

        private void cboSelectDatabase_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnLoadProcedures.Enabled = true;
        }

        private void btnLoadProcedures_Click(object sender, EventArgs e)
        {
            //This is where I need some help...
        }
    }
}   

Answers (2)