SQLDataReader + DropDownList + HashTable 使用方法
WebForm2.aspx.cs |
using System; using System.Collections; using System.Data; using System.Data.SqlClient; using System.Text; … … private void GetData() { Hashtable ht = new Hashtable(); String connectionString1 = < Connection String >; SqlConnection con1 = new SqlConnection(connectionString1); con1.Open(); StringBuilder sb = new StringBuilder(); sb.Append("SELECT * FROM League;"); SqlCommand cmd1 = new SqlCommand(sb.ToString(), con1); SqlDataReader dr = cmd1.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { ht.Add(dr["League_Name"].ToString().Trim(), dr["League_ID"].ToString().Trim()); } } con1.Close(); con1.Dispose(); this.DropDownList1.DataSource = ht; this.DropDownList1.DataTextField = "Key"; this.DropDownList1.DataValueField = "Value"; this.DropDownList1.DataBind(); } |