如何为记录数据库创建添加函数?
在我的程序中,我需要添加一个添加函数来插入类别。我正在使用树视图来显示数据。
用户插入关卡,程序必须将该类别插入那个水平。但我累了。因为它需要检查其他级别是否存在(即: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?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不让用户选择新类别应添加到的父节点?您可以让他们只需单击父节点,然后向其添加新节点。不涉及任何检查。我知道这并不能回答您的直接问题,但您当前的方法对您和您的用户来说很难。
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.