2014-11-05 131 views
-4

我想爲類別和子類創建一個表。爲類別和子類別創建表

此外還會有其他列,如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

回答

0

,你可以做以下

從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