Click here to Skip to main content
15,911,891 members
Home / Discussions / C#
   

C#

 
GeneralRe: binding tree view with sql databases and tables Pin
Henry Minute28-Mar-09 22:09
Henry Minute28-Mar-09 22:09 
GeneralRe: binding tree view with sql databases and tables [modified] Pin
Henry Minute29-Mar-09 2:59
Henry Minute29-Mar-09 2:59 
GeneralRe: binding tree view with sql databases and tables Pin
mariamkhaqan29-Mar-09 21:34
mariamkhaqan29-Mar-09 21:34 
GeneralRe: binding tree view with sql databases and tables Pin
Henry Minute30-Mar-09 1:45
Henry Minute30-Mar-09 1:45 
GeneralRe: binding tree view with sql databases and tables Pin
mariamkhaqan30-Mar-09 3:31
mariamkhaqan30-Mar-09 3:31 
GeneralRe: binding tree view with sql databases and tables Pin
mariamkhaqan30-Mar-09 3:33
mariamkhaqan30-Mar-09 3:33 
GeneralRe: binding tree view with sql databases and tables Pin
Henry Minute30-Mar-09 3:48
Henry Minute30-Mar-09 3:48 
GeneralRe: binding tree view with sql databases and tables Pin
Henry Minute30-Mar-09 7:12
Henry Minute30-Mar-09 7:12 
When I started trying to help you, I just tried to get your code working for you. Trying out your code has made me realize that part of what I was telling you to do would make your code run very slowly. Filling the TreeView with all of the data in one go would take a very long time.

So I am suggesting that you just get the names of the databases and add a node for each. Each of these database nodes has nodes for Tables, Views etc. But do not fetch the data for these until the user tries to expand them. So that the expand icon shows up for these nodes, they each have an empty node added. This is removed later when it is not needed.

This code works correctly on my system. The only difference between my system and yours is the name of the server. I have tried to change this code so that it will work on your system exactly as it is. Try to use it as it is first, before you alter the layout etc. to suit your style of working.


private TreeNode parentNode;
private readonly string connString = "server={0};Integrated Security=true;database={1}";

// where i am calling the function
private void sQLServer2005ToolStripMenuItem_Click(object sender, EventArgs e)
{
    parentNode = new TreeNode();
    DBTree.Nodes.Add(Show_SqlConnection());
    parentNode.Text = "Databases";
    DBTree.Nodes[0].Nodes.Add(parentNode);
    LoadSQL_DB(parentNode);
}

// Method which loads databases and tables
public void LoadSQL_DB(TreeNode DB)
{
    DataTable table;
    TreeNode treeNode;
    TreeNode tempNode;
    int count = 0;

                // Note the use of the 'using' statement. This ensures that the connection gets closed
                // and disposed of when it is finished with.
    //SqlCon = new SqlConnection("Data Source= Localhost; Integrated security=True;database='' ");
    using (SqlConnection SqlCon = GetConnection(@"Localhost", ""))
    {
        SqlCommand SqlCom = new SqlCommand();
        SqlCom.Connection = SqlCon;
        SqlCom.CommandType = CommandType.StoredProcedure;
        SqlCom.CommandText = "sp_databases";

        SqlCon.Open();
        SqlDataReader SqlDR = SqlCom.ExecuteReader();
        table = ReaderToTable(SqlDR);
    }

    //Get the Database Nodes
    foreach (DataRow row in table.Rows)
    {
        treeNode = new TreeNode();
        treeNode.Text = row["DATABASE_NAME"].ToString();
        tempNode = treeNode.Nodes.Add("Tables");
        tempNode.Nodes.Add(""); // All that this does, is add a dummy node
        tempNode = treeNode.Nodes.Add("Stored Procedure");
        tempNode.Nodes.Add(""); // adding a dummy node ensures that the '+' icon appears
        tempNode = treeNode.Nodes.Add("Views");
        tempNode.Nodes.Add("");
        tempNode = treeNode.Nodes.Add("Roles");
        tempNode.Nodes.Add("");
        count++;

        DB.Nodes.Add(treeNode);
    }
}

// function which loads tables
public void LoadSQL_Tables(TreeNode arg_dbNode, string database)
{
    DataTable table;

    using (SqlConnection SqlCon = GetConnection("Localhost", database))
    {
        SqlCommand tablesCommand = new SqlCommand();
        tablesCommand.CommandType = CommandType.StoredProcedure;
        tablesCommand.CommandText = "sp_tables";
        tablesCommand.Parameters.Add("@table_qualifier", SqlDbType.NVarChar, 384).Value = database;
        string paramString = "'TABLE'";  // The inner single quotes are required
        tablesCommand.Parameters.Add("@table_type", SqlDbType.VarChar, 32).Value = paramString;
        tablesCommand.Connection = SqlCon;

        SqlCon.Open();
        SqlDataReader SqlDR = tablesCommand.ExecuteReader();
        table = ReaderToTable(SqlDR);
    }

    foreach (DataRow row in table.Rows)
    {
        arg_dbNode.Nodes.Add(row["TABLE_NAME"].ToString());
    }

    // get rid of the dummy node. If we have found any tables we don't need it
                // and if there are no tables we don't need to show the '+' icon.
    arg_dbNode.Nodes.RemoveAt(0);
}

private SqlConnection GetConnection(string server, string database)
{
    return new SqlConnection(string.Format(this.connString, server, database));
}

public DataTable ReaderToTable(SqlDataReader reader)
{
    DataTable newTable = new DataTable();
    DataColumn col;
    DataRow row;
    int i;

    for (i = 0; i < reader.FieldCount; i++)
    {
        col = new DataColumn();
        col.ColumnName = reader.GetName(i);
        col.DataType = reader.GetFieldType(i);

        newTable.Columns.Add(col);
    }

    while (reader.Read())
    {
        row = newTable.NewRow();
        for (i = 0; i < reader.FieldCount; i++)
        {
            row[i] = reader[i];
        }

        newTable.Rows.Add(row);
    }

    return newTable;
}

        // Dont forget to add this to the BeforeExpand event of your TreeView
private void DBTree_BeforeExpand(object sender, TreeViewCancelEventArgs e)
{
    if ((e.Node.Nodes.Count == 1) && (e.Node.Nodes[0].Text == ""))
    {
        // You will have to add a case for any other types
        // that you want to get data for.
        // I have put an example in and commented it out
        switch (e.Node.Text)
        {
            case "Tables":
                LoadSQL_Tables(e.Node, e.Node.Parent.Text);
                break;
            //case "Views":
            //    LoadSQL_Views(e.Node, e.Node.Parent.Text);
            //    break;
        }
    }
}


Good Luck! Smile | :)

Henry Minute

Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”

GeneralRe: binding tree view with sql databases and tables Pin
mariamkhaqan30-Mar-09 17:33
mariamkhaqan30-Mar-09 17:33 
GeneralRe: binding tree view with sql databases and tables Pin
mariamkhaqan1-Apr-09 2:29
mariamkhaqan1-Apr-09 2:29 
GeneralRe: binding tree view with sql databases and tables Pin
Henry Minute1-Apr-09 4:16
Henry Minute1-Apr-09 4:16 
GeneralRe: binding tree view with sql databases and tables Pin
mariamkhaqan3-Apr-09 2:24
mariamkhaqan3-Apr-09 2:24 
QuestionChart in windows Pin
lnmca28-Mar-09 3:05
lnmca28-Mar-09 3:05 
AnswerRe: Chart in windows Pin
dan!sh 28-Mar-09 5:32
professional dan!sh 28-Mar-09 5:32 
AnswerRe: Chart in windows Pin
Natza Mitzi29-Mar-09 19:54
Natza Mitzi29-Mar-09 19:54 
QuestionValidating IP Addresses Pin
Jammer28-Mar-09 2:02
Jammer28-Mar-09 2:02 
AnswerRe: Validating IP Addresses Pin
quacks_a_lot28-Mar-09 9:59
quacks_a_lot28-Mar-09 9:59 
GeneralRe: Validating IP Addresses Pin
Jammer30-Mar-09 2:12
Jammer30-Mar-09 2:12 
QuestionQuestion about DCT Pin
Flying_Doc28-Mar-09 1:09
Flying_Doc28-Mar-09 1:09 
AnswerRe: Question about DCT Pin
Luc Pattyn28-Mar-09 6:31
sitebuilderLuc Pattyn28-Mar-09 6:31 
QuestionSuggestion Needed : Creating Dynamic Build Pin
Abhijit Jana28-Mar-09 0:51
professionalAbhijit Jana28-Mar-09 0:51 
AnswerRe: Suggestion Needed : Creating Dynamic Build Pin
Eddy Vluggen28-Mar-09 1:33
professionalEddy Vluggen28-Mar-09 1:33 
GeneralRe: Suggestion Needed : Creating Dynamic Build Pin
Abhijit Jana28-Mar-09 1:36
professionalAbhijit Jana28-Mar-09 1:36 
GeneralRe: Suggestion Needed : Creating Dynamic Build Pin
Eddy Vluggen28-Mar-09 1:50
professionalEddy Vluggen28-Mar-09 1:50 
GeneralRe: Suggestion Needed : Creating Dynamic Build Pin
Abhijit Jana28-Mar-09 2:03
professionalAbhijit Jana28-Mar-09 2:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.