正在尝试将父节点和子节点插入数据库,但没有得到 [英] am trying to inserting parentnode and child node into DB but not getting

查看:79
本文介绍了正在尝试将父节点和子节点插入数据库,但没有得到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码....

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 WindowsFormsApplication8
{
    public partial class Form1 : Form
    {
        SqlConnection con = new SqlConnection("Data Source=CIODEV03\\SQLEXPRESS;Initial Catalog=EmployeeDB;Integrated Security=True");
        SqlCommand cmd = new SqlCommand();
        SqlDataReader dr;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Trim().Length != 0)
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("insert into treenodes values(" + textBox1.Text + ");", con);
                    TreeNode parentNode = new TreeNode(textBox1.Text);
                    treeView1.Nodes.Add(parentNode);
                    textBox1.Clear();
                    cmd.ExecuteNonQuery();
                    con.Close();
                    BindDataInTreeView();

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    con.Close();
                }
            }
            else
            {
                MessageBox.Show("");
            }
        }

        private void btnchild_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode != null)
            {

                if (textBox1.Text.Length != 0)
                {
                    try
                    {
                        con.Open();
                        SqlCommand cmd = new SqlCommand("insert into treenodes values(" + textBox1.Text + ");", con);
                        TreeNode childNode = new TreeNode(textBox1.Text);
                        treeView1.SelectedNode.Nodes.Add(childNode);
                        treeView1.ExpandAll();
                        textBox1.Clear();
                        cmd.ExecuteNonQuery();
                        con.Close();
                        BindDataInTreeView();
                    }
                     catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    con.Close();
                }
            }
            else
            {
                MessageBox.Show("");
            }
                }
            }
        

        private void BindDataInTreeView()
        {
            treeView1.Nodes.Clear();
            try
            {
                con.Open();
              
                SqlCommand cmd = new SqlCommand("SELECT parentNode,childNode FROM treenodes", con);
                dr = cmd.ExecuteReader();
                while (dr.Read())
                {
               
                    treeView1.Nodes.Add(dr.GetValue(0).ToString());
                }
                dr.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                con.Close();
            }
        }

        private void btndelete_Click(object sender, EventArgs e)
        {
            if (treeView1.Nodes.Count > 0)
            {
               
                if (treeView1.SelectedNode != null)
                {
                
                    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);
            }
        }

        
    }
}

推荐答案

这里发生了几件事:

1)您已经在类级别上声明了SqlCommand cmd,因此您无需再次声明它(您应该为此获得一个异常).只需分配cmd变量即可.
There are a couple things going on here:

1) You have already declared SqlCommand cmd at the class level, so you do not need to declare it again (you should get an exception for this). Just assign the cmd variable.
cmd = new SqlCommand("insert into treenodes values(" + textBox1.Text + ");", con);



2)将字符串插入sql时,该值必须用单引号引起来(您应该为此获得一个例外).



2) When inserting strings into sql, the value must be surrounded by single quote marks (you should get an exception for this).

cmd = new SqlCommand("insert into treenodes values('" + textBox1.Text + "');", con);


注意:最好使用SqlCommand.Parameters添加值,然后根据需要将它们适当包装.这也是避免sql注入攻击的好方法.

3)由于您未指定要插入哪一列,因此该值将始终插入第一列.检索数据时:


Note: It would be better to use SqlCommand.Parameters to add the values, then they would get wrapped appropriately if needed. It is also good practice for avoiding sql injection attacks.

3) Since you do not specify which column to insert into, the value will always insert into the first column. When you retrieve the data:

cmd = new SqlCommand("SELECT parentNode,childNode FROM treenodes", con);


如果parentNode是第一列,则您输入的所有值都将在该列中,而childNode列将充满NULL值.指定要在其中插入值的列:


if the parentNode is the first column, then all the values you entered will be in that column and the childNode column will be full of NULL values. To specify which column to insert values to:

cmd = new SqlCommand("insert into treenodes (parentNode) values('" + textBox1.Text + "');", con);


或者,如果您输入一个子节点,则需要指定谁是父节点:


or, if you enter a child node, you will want to specify who the parent node is:

cmd = new SqlCommand("insert into treenodes (parentNode, childNode) values('" + treeView1.SelectedNode.Text + "','" + textBox1.Text + "');", con);



4)遍历结果的方式是,将所有值添加为好像它们是父级一样,而不是将子节点添加到适当的父级中.



4) The way you are looping through the results, you are adding all the values as if they were parents, you are not adding the child nodes to the proper parent.

while (dr.Read())
{

     treeView1.Nodes.Add(dr.GetValue(0).ToString());
}


这应该给您一些注意事项.


This should give you a few things to look at.


这篇关于正在尝试将父节点和子节点插入数据库,但没有得到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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