如何为记录数据库创建添加函数?

发布于 2024-11-30 03:00:31 字数 2616 浏览 1 评论 0原文

在我的程序中,我需要添加一个添加函数来插入类别。我正在使用树视图来显示数据。

如何在数据库中对此类进行建模?

在此处输入图像描述

用户插入关卡,程序必须将该类别插入那个水平。但我累了。因为它需要检查其他级别是否存在(即:treeView 为空,我想添加 2.1,所以这是一个错误)。

有时,您可能会添加已设置的级别,因此必须禁止这样做。

我的代码需要一些帮助。它已经完成,但我想改进它或修复错误(如果是这样)。

这是代码:

    private void AddButton_Click(object sender, RoutedEventArgs e)
    {
        NorthwindDataContext cd = new NorthwindDataContext();

        int[] levels = LevelTextBox.Text.ToIntArray('.');
        string newName = NameTextBox.Text;

        int[] parentLevels = new int[levels.Length - 1];
        Array.Copy(levels, parentLevels, parentLevels.Length);
        Objective current = GetNode(levels);
        Objective parent = GetNode(parentLevels);

        if (current != null)
        {
            MessageBox.Show("Level already exists");
            return;
        }
        else if (parent == null && parentLevels.Length != 0)
        {
            MessageBox.Show("Parent level doesn't exist");
            return;
        }

        var newObjective = new Objective();
        newObjective.Name = newName;
        newObjective.Level = levels.Last();
        newObjective.Parent_ObjectiveID = parent == null ? null : (int?)parent.ObjectiveID;

        cd.Objective.InsertOnSubmit(newObjective);
        cd.SubmitChanges();

        MessageBox.Show("The new objective has added successfully");
        NameTextBox.Clear();
        LoadObjectives();
    }

    public Objective GetNode(params int[] indexes)
    {
        return GetNode(null, 0, indexes);
    }

    public Objective GetNode(int? parentid, int level, params int[] indexes)
    {
        NorthwindDataContext cd = new NorthwindDataContext();
        Objective item = null;

        if (indexes.Length == 0)
            return null;

        if (parentid == null)
        {
            item = (from p in cd.Objective
                    where p.Level == indexes[level] && p.Parent_ObjectiveID == null
                    select p).SingleOrDefault();

        }
        else
        {
            item = (from p in cd.Objective
                    where p.Level == indexes[level] && p.Parent_ObjectiveID == parentid
                    select p).SingleOrDefault();
        }

        if (item == null)
            return null;

        if (++level < indexes.Length)
            item = GetNode(item.ObjectiveID, level, indexes);

        return item;
    }

In my program, I need to put an add function to insert categories. I'm using a treeView to show the data.

How can I model this class in a database?

enter image description here

The user insert the level, and the program must insert that category in that level. But I'm getting tired. Because it needs to check if the others levels exist (IE: The treeView is empty and I want to add 2.1, so it's an error).

Sometimes, you may add an already level setted and so that must be forbidden.

I need a little of help in my code. It's finished, but I want to improve it or fixed errors (if so).

Here is the code:

    private void AddButton_Click(object sender, RoutedEventArgs e)
    {
        NorthwindDataContext cd = new NorthwindDataContext();

        int[] levels = LevelTextBox.Text.ToIntArray('.');
        string newName = NameTextBox.Text;

        int[] parentLevels = new int[levels.Length - 1];
        Array.Copy(levels, parentLevels, parentLevels.Length);
        Objective current = GetNode(levels);
        Objective parent = GetNode(parentLevels);

        if (current != null)
        {
            MessageBox.Show("Level already exists");
            return;
        }
        else if (parent == null && parentLevels.Length != 0)
        {
            MessageBox.Show("Parent level doesn't exist");
            return;
        }

        var newObjective = new Objective();
        newObjective.Name = newName;
        newObjective.Level = levels.Last();
        newObjective.Parent_ObjectiveID = parent == null ? null : (int?)parent.ObjectiveID;

        cd.Objective.InsertOnSubmit(newObjective);
        cd.SubmitChanges();

        MessageBox.Show("The new objective has added successfully");
        NameTextBox.Clear();
        LoadObjectives();
    }

    public Objective GetNode(params int[] indexes)
    {
        return GetNode(null, 0, indexes);
    }

    public Objective GetNode(int? parentid, int level, params int[] indexes)
    {
        NorthwindDataContext cd = new NorthwindDataContext();
        Objective item = null;

        if (indexes.Length == 0)
            return null;

        if (parentid == null)
        {
            item = (from p in cd.Objective
                    where p.Level == indexes[level] && p.Parent_ObjectiveID == null
                    select p).SingleOrDefault();

        }
        else
        {
            item = (from p in cd.Objective
                    where p.Level == indexes[level] && p.Parent_ObjectiveID == parentid
                    select p).SingleOrDefault();
        }

        if (item == null)
            return null;

        if (++level < indexes.Length)
            item = GetNode(item.ObjectiveID, level, indexes);

        return item;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜是你 2024-12-07 03:00:31

为什么不让用户选择新类别应添加到的父节点?您可以让他们只需单击父节点,然后向其添加新节点。不涉及任何检查。我知道这并不能回答您的直接问题,但您当前的方法对您和您的用户来说很难。

Why don't you let the user select a parent node to which the new category should be added? You could let them just click a parent node and then add a new node to it. No checks involved. I know that this does not answer your direct question, but your current approach is hard on you and your users.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文