實現二級聯動菜單 C#,帶完整資料庫 SQL

來源:互聯網
上載者:User

----------------db.cs------------------------------

   // TODO: 在此處添加建構函式邏輯
   //
  }
  public static SqlConnection createConnection()
 {
 SqlConnection con=new SqlConnection("server=.;database=list;uid=sa;pwd=;");
   return con;
 }
 }
}


--------------------------.cs源檔案--------------------------------

private void Page_Load(object sender, System.EventArgs e)
  {
   if(!Page.IsPostBack)
   {
    SqlConnection con=db.createConnection();
    con.Open();
    SqlCommand cmd=new SqlCommand("select * from sheng",con);
    SqlDataReader sdr=cmd.ExecuteReader();
    this.sheng.DataSource=sdr;
    this.sheng.DataTextField="sheng";
    this.sheng.DataValueField="shengid";
    this.sheng.DataBind();
    sdr.Close();
    cmd.CommandText="select * from city where shengid=1";
    sdr=cmd.ExecuteReader();
    this.city.DataSource=sdr;
    this.city.DataTextField="cityname";
    this.city.DataValueField="cityid";
    this.city.DataBind();
    sdr.Close();
    con.Close();
   }
  }
  #region Web Form設計器產生的程式碼
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 該調用是 ASP.NET Web Form設計器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 設計器支援所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.sheng.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedIndexChanged);
   this.Button1.Click += new System.EventHandler(this.Button1_Click);
   this.Calendar1.SelectionChanged += new System.EventHandler(this.Calendar1_SelectionChanged);
   this.rdl.SelectedIndexChanged += new System.EventHandler(this.drl_SelectedIndexChanged);
   this.mar.SelectedIndexChanged += new System.EventHandler(this.mar_SelectedIndexChanged);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void Button1_Click(object sender, System.EventArgs e)
  {
   
  }
  private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
  {
   SqlConnection con=db.createConnection();
   con.Open();
   SqlCommand cmd=new SqlCommand("select * from city where shengid="+this.sheng.SelectedValue.ToString(),con);
   SqlDataReader sdr=cmd.ExecuteReader();
   this.city.DataSource=sdr;
   this.city.DataTextField="cityname";
   this.city.DataValueField="cityid";
   this.city.DataBind();
   sdr.Close();
   con.Close();
  }

-----------------------sql檔案-----------------------

create database list

use list

create table sheng
(
 shengid int primary key,
 sheng varchar(50) not null,
)

insert into sheng values(1,'四川')
insert into sheng values(2,'安徽')
insert into sheng values(3,'河南')
insert into sheng values(4,'重慶')
insert into sheng values(5,'廣西')

select * from sheng

create table city
(
 cityid int primary key,
 shengid int foreign key references sheng(shengid),
 cityname varchar(50) not null,
)

insert into city values(1,1,'成都')
insert into city values(2,1,'綿陽')
insert into city values(3,1,'廣元')
insert into city values(4,2,'合肥')
insert into city values(5,2,'蚌埠')
insert into city values(6,2,'淮南')
insert into city values(7,3,'鄭州')
insert into city values(8,3,'開封')
insert into city values(10,3,'商丘')
insert into city values(11,4,'墊江')
insert into city values(12,4,'江津')
insert into city values(13,4,'重慶')
insert into city values(14,5,'南寧')
insert into city values(15,5,'柳州')
insert into city values(16,5,'白色')

select * from city

---------如果有朋友學習要用的話最好看清楚,呵呵,我是邊做邊寫的,並不是可以一次執行成功的------

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.