Please Help!!

Hi All,

I am new to C# and have been assigned to build a small application in C#. What I have to do is make a database connection and gather the list of available server-databases and then populate a checkedlist box with this list. Then I have to take a user input for a data object(which can be a stored proc or a view) and search for this user input in the list of stored procedures for the selected checkedlist box items (eg: if there is a server-db like datadev - base and the user input is sp_orders then I have to check for sp_orders in all the stored procs listed under datadev-base). when I am able to find the stored procs tht call the user input data object then I have to display them in a list box. So far I have been able to make a DB connection and populate the checkedlistbox. I am not sure how to procced now. All I have in mind is that I can search for the data object using 'ismatch' on the text that I can get thru executing 'helptext' on the stored procs. But I dont know how to code for it and where should this code go(Probably that would be in the clickevent of submit button after taking user input, if I am not wrong). I would really appreciate if anybody can help me with this.

Thanks,



Answer this question

Please Help!!

  • jca_john

    To retrieve the list of databases on that SQL Connection:

    you can execute the sp_databases stored procedure which will retrieve the list of databases on that connection.

    so you can use a dataReader to read each row (column name Database_name) and populate this in a list box/combo box

    you can also retrieve a list of stored procedures for the specific database using this query on the database:

    select name from dbo.sysobjects where type ='P'

    I hope this helps in some way!



  • renyx

    Thanks to both of you,

    I will try these codes and let you know what works for me.


  • biki

    Another Solution differ from what Ahmed Suggest is to use

    SMO (Server Management Objects)

    To Use it first you need to do the following

    Add The Reference To

    • Microsoft.SqlServer.ConnectionInfo.dll
    • Microsoft.SqlServer.Smo.dll
    • Microsoft.SqlServer.SmoEnum.dll

    Write

    • using Microsoft.SqlServer.Management.Smo;
    • using Microsoft.SqlServer.Management.Common;

    You Can do the Following

        //To Connect To The Serevr
        string serverName = "Localhost";
        Server server = new Server(serverName);

    //Get List of Databases in the serevr
    foreach (Database db in server.Databases)
    {
    lstDatabases.Items.Add(db.Name);
    }

    //To get specific Database
    string databaseName = "Northwind";
    Database db = server.Databases[databaseName];

    //To get all the table in database
    foreach (Table table in db.Tables)
    {
    lstTable.Items.Add(tableName);
    }

    //To Get All Stored Procedures
    foreach (StoredProcedure sp in db.StoredProcedures)
    {
    lstSP.Items.Add(sp.Name);
    }

    //To Search For SP
    string spName="spTest";
    if (db.StoredProcedures.Contains(spName) == true)
    {
    //Found it so do it
    }
    else

    {
    //sorry i can not found what you search for
    }

    Look Also To This Thread

    How to add SQL ConnectionString editor form

     



  • Please Help!!