保存数据并显示数据 [英] Save Data and Show Data

查看:61
本文介绍了保存数据并显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类别,但我不知道有多少个子类别
喜欢
电子电视
电子风扇
电子计算机
电子计算机键盘密码
电子计算机鼠标
我不知道这种类型的类别和子类别
我想将所有数据保存在数据库中并显示
有人可以帮我吗?

I Have A Category and I Don''t Know How Many Sub_Category
LIke
Electronic-tv
Electronic-Fan
Electronic-Computer
Electronic-Computer-KeyBord
Electronic-Computer-Mouse
This Type i don''t know how many category and sub category
I want To Save This All Data in database and Show It
Can anybody help me?

推荐答案

该类可以定义如下:

The Class could be defined as the following:

public class Category
        {
            public int CategoryID { get; set; }
            public int ParentCategoryID { get; set; }
            public string CategoryName { get; set; }
            public List<Category> subCategories { get; set; }
        }



当然,您还需要数据库中具有CategoryID,ParentCategoryID和CategoryName作为字段的表.要获取某个类别的所有子类别,请执行从CategoriesTable中选择categoryID,其中CategoryName =``Electronic-tv"" ...然后使用该类别ID获取所有相关的子类别:从CategoriesTable中选择CategoryID,CategoryName,其中ParentCategoryID =``ID_FROM_PREVIOUS_QUERY''''

这将只需要数据库中的一个表.

要显示数据:

如果表中没有大量数据,我建议使用DataAdapter和.Fill()方法填充DataTable.然后设置一个带有DataGridView的ResultsForm.将DataTable传递到ResultsForm中并显示在dataGridView中.对于大量数据来说,这不是一个很好的解决方案,但是实现起来却很容易.

您可能在主窗体上有一个主要类别的列表,并且用户双击列表中的一个项目.此时,您执行上述查询以从数据库中收集该类别的所有子类别.您填写了dataTable或List< Category>.查询结果,并显示在datagridview中.然后,用户双击ResultsForm中的子类别,此时,您可以执行相同的操作(执行上述查询,获取Cat的Cat类,在resultsForm中显示)



Of course you would also want a Table in the DB that has the CategoryID, ParentCategoryID, and CategoryName as Fields. To get all subcategories for a Category, perform a "select categoryID from CategoriesTable where CategoryName = ''Electronic-tv''"... then use this categoryID to obtain all subCategories that are related: "Select CategoryID, CategoryName from CategoriesTable where ParentCategoryID = ''ID_FROM_PREVIOUS_QUERY''"

This would require only one table in the database.

To display the data:

If there is not a large amount of data in the table, I would recommend using a DataAdapter and the .Fill() method to fill a DataTable. Then setup a ResultsForm with a DataGridView on it. Pass the DataTable into the ResultsForm and display in the dataGridView. This is not a great solution for large amounts of data, but the implementation is fairly easy.

You might have a list of main categories on your Main Form, and the user double-clicks an item in the list. At which point you perform the above queries to gather all subcategories from the database for that category. You fill up a dataTable or a List<Category> with the results of the query, and display in the datagridview. The user then double-clicks a sub-category in the ResultsForm, at which point, you could perform the same operation (perform above queries, get subCats for Cat, display in resultsForm)


这篇关于保存数据并显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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