为类别和子类别创建表

问题描述:

我想为类别和子类创建一个表。为类别和子类别创建表

此外还会有其他列,如CategoriesName和SubCategoriesName。

在前端执行此操作时,我希望正确填充每个类别和子类别的数据。

+1

这没有一个具体的问题,太宽泛。请让我们知道你的具体问题是什么,你收到什么错误消息,你试过什么,等等。 – 2014-11-05 14:02:27

+0

你看过你的问题吗?给我们这么少的信息,我们可能会提出什么建议?你*身体*只给**两列标题**继续?所有***我***可以告诉你,你应该把它分成两个独立的表格(用外键)。 – jbutler483 2014-11-05 14:02:29

+0

@ jbutler483:我想要一个列将有CategoryID映射SubCategory.Rest我可以包括其他列。 – 2014-11-05 14:03:43

,你可以做以下

从MS-SQL侧1-和创建表

create table [dbo].[categories](
    id int identity(1,1) not null, 
    name nvarchar(50) not null, 
    parent int null, 
constraint [pk_categories] primary key clustered (id asc)) 

go 

alter table [dbo].[categories] with check add constraint [fk_subcategories] foreign key(parent) 
references [dbo].[categories] ([id]) 
go 

alter table [dbo].[categories] check constraint [fk_subcategories] 
go 

上面的代码将创建一个表叫你的类别,它包含父场,将建立从asp.net的类别和它的子类别

2-之间的关系(假设使用的是asp.net应用程序)

添加页面,并把2下拉列表,并呼吁他们以下几点:categoryDropDownList和subCategoryDropDownList并添加GridView和调用它的页面代码的dataGridView

3-作如下填充第一个下拉列表

var sql="Select * from dbo.categories where parent is null"; 
var connection=new System.Data.SqlClient.SqlConnection("Your connectionstring"); 
var adapter=new System.Data.SqlClient.SqlDataAdapter(sql,connection); 
var table =new DataTable(); 
adapter.Fill(table); 
categoryDropDownList.DataSource=table; 
catagoryDropDwonList.Databind(); 

4-上catagoryDropDownList,以填充所述第二下拉列表中选择折射率变化,则执行下列操作:

var sql=string.Format("Select * from dbo.categories where parent={0}",categoryDropDownList.SelectedIndex); 

// here you are calling to retrieve all the categories with parent from the category dropdown list, which means you will get all sub categories of the selected category from category list 

var connection=new System.Data.SqlClient.SqlConnection("Your connectionstring"); 
var adapter=new System.Data.SqlClient.SqlDataAdapter(sql,connection); 
var table =new DataTable(); 
adapter.Fill(table); 
subCategoryDropDownList.DataSource=table; 
subCatagoryDropDwonList.Databind(); 

5-上subCatagoryDropDownList的所选索引改变你做必要的编码,以popul吃了datagrid

希望这会帮助你

+0

Hasan:我正在使用SQL语句来创建表。 – 2014-11-05 14:05:50

+0

如何显示数据?在树视图中? – Monah 2014-11-05 14:08:37

+0

我将在下拉列表中显示类别和子类别。子类别将会有一张上传的表单,它将以gridview显示。 Excel工作表将显示在网格 – 2014-11-05 14:10:49