我该如何在SQL数据库中将parentnode和chaildnode存储为此代码........ [英] how can i store parentnode and chaildnode in sql database for this code...........

查看:56
本文介绍了我该如何在SQL数据库中将parentnode和chaildnode存储为此代码........的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码


this is my code


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 System.Data.SqlClient;

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

        private void Form1_Load(object sender, EventArgs e)
        {
        }
        private void btnparent_Click(object sender, EventArgs e)
        {

            //if textbox is not empty.
            if (textBox1.Text.Trim().Length != 0)
            {
                //create an object of //Tree Node class and pass node name to the constructor of Tree Node.
                TreeNode parentNode = new TreeNode(textBox1.Text);
                treeView1.Nodes.Add(parentNode);
                textBox1.Clear();
            }

            else
            {
                MessageBox.Show("Please Enter Value In The TextBox.", "Data Entry Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
            }
        }

        private void btnchaild_Click(object sender, EventArgs e)
        {    
            //Check parent node is selected or not
            if (treeView1.SelectedNode != null)
            {
                //If child node name is entered.
                if (textBox1.Text.Length != 0)
                {
                    //Create an object of the child node and pass child node name to the constructor of Tree Node
                    TreeNode childNode = new TreeNode(textBox1.Text);
                    //Add child node to the selected parent node
                    treeView1.SelectedNode.Nodes.Add(childNode);
                    treeView1.ExpandAll();
                    textBox1.Clear();
                }
                else
                {
                    MessageBox.Show("Please Enter Value In The TextBox.", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                }

            }

            else
            {
                MessageBox.Show("Please Select Parent Node.", "Warning Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);


            }
        }

        private void btndel_Click(object sender, EventArgs e)
        {
            //Check whether tree view contains any node or //not.
            if (treeView1.Nodes.Count > 0)
            {
                //Check whether any node in tree view //control is selected or not.
                if (treeView1.SelectedNode != null)
                {
                    //If node is selected the remove that //node.
                    treeView1.SelectedNode.Remove();
                   MessageBox.Show("Node Removed Successfully", "Success Message", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                }

                else
                {

                    MessageBox.Show("Please Select Any Node To Be Deleted.", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);

                }

            }

            else
            {

                MessageBox.Show("There Is No Node In The Tree View Control.", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            }
        }

    }
}





这是我的表查询.......

我的数据库名称是EmployeeDB

我的表名是treenodes





this is my table query.......

my database name is EmployeeDB

my table name is treenodes

parentNode nvarchar(50) Nulls
childNode  nvarchar(50) Nulls

推荐答案

为每行添加一个唯一ID和一个可为空的ParentID.在这种情况下,孩子可以找到其父母.
add a uniqueID to each row and a nullable ParentID. In that case a child can find it''s parent.


这篇关于我该如何在SQL数据库中将parentnode和chaildnode存储为此代码........的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆