使用LINQ TO SQL类连接数据库。
create database mydbgouse mydbgoCREATE TABLE [dbo].[ChinaStates] ( [AreaCode] varchar(20) COLLATE Chinese_PRC_CI_AS NOT NULL, [AreaName] nvarchar(20) COLLATE Chinese_PRC_CI_AS NOT NULL, [ParentAreaCode] varchar(20) COLLATE Chinese_PRC_CI_AS NOT NULL, [Root] bit NOT NULL, [Zone] smallint NOT NULL, [NavigateURL] varchar(100) COLLATE Chinese_PRC_CI_AS, CONSTRAINT [PK_TB_Area] PRIMARY KEY CLUSTERED ([AreaCode]))ON [PRIMARY]
编辑页面
编写cs
public DataClassesDataContext context; public Listchinastates; protected void Page_Load(object sender, EventArgs e) { context = new DataClassesDataContext(); if (!IsPostBack) { binddata("0001"); } } //根据parentcode查 private void binddata(string parentcode) { var querydata = from q in context.ChinaStates where q.ParentAreaCode == parentcode select q; Repeater1.DataSource = querydata; Repeater1.DataBind(); Literal2.Text = parentcode;//显示ParentAreaCode } protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e) { //删除 if(e.CommandName=="delete") { string code = e.CommandArgument.ToString();//获取这一及编号 ChinaStates cstates = context.ChinaStates.Single(r=>r.AreaCode==code);//获取对应地区编号的数据 context.ChinaStates.DeleteOnSubmit(cstates);//删除 context.SubmitChanges(); binddata(cstates.ParentAreaCode);//显示删除后的数据 } //显示下级 if(e.CommandName=="Showxiaji") { string code = e.CommandArgument.ToString();//获取这一级的唯一AreaCode ChinaStates cstates = context.ChinaStates.Single(r => r.AreaCode == code); Literal1.Text = cstates.ParentAreaCode;//存储这级的ParentAreaCode binddata(code);//显示,下一级ParentCode是这级的AreaCode } } //添加地区 protected void Button3_Click(object sender, EventArgs e) { ChinaStates cdata = new ChinaStates() { AreaCode=DateTime.Now.Millisecond.ToString(), AreaName=TextBox1.Text, ParentAreaCode=Literal2.Text }; try { context.ChinaStates.InsertOnSubmit(cdata); context.SubmitChanges(); binddata(Literal2.Text); Response.Write(" "); } catch(Exception ex) { Response.Write(" "); } } protected void Button1_Click(object sender, EventArgs e) { Panel1.Visible = true; } //返回上级 protected void Button2_Click(object sender, EventArgs e) { if (Literal1.Text != "0000" && Literal1.Text.Length > 0) { binddata(Literal1.Text);//显示上一级,ParentAreaCode ChinaStates cdata = context.ChinaStates.Single(r=>r.AreaCode==Literal1.Text); Literal1.Text = cdata.ParentAreaCode; } } }