ASP DropDownList DataBinding DB data
ref https://dotblogs.com.tw/mis2000lab/2008/10/15/databinding_column081015
String Constr = @"Persist Security Info=False;Integrated Security=true;
Initial Catalog=Travel;Server=.\SQLExpress";
SqlConnection Conn = new SqlConnection(Constr);
SqlDataAdapter da = new SqlDataAdapter("SELECT Id,Name FROM Travel.dbo.Spot order by Name", Conn);
DataSet ds = new DataSet();
da.Fill(ds, "Spot"); //第二、執行SQL指令,取出資料
DropDownList1.DataValueField = "Id"; //在此輸入的是資料表的欄位名稱
DropDownList1.DataTextField = "Name"; //在此輸入的是資料表的欄位名稱
DropDownList1.DataSource = ds.Tables["Spot"].DefaultView;
DropDownList1.DataBind();
Conn.Close();
String Constr = @"Persist Security Info=False;Integrated Security=true;
Initial Catalog=Travel;Server=.\SQLExpress";
SqlConnection Conn = new SqlConnection(Constr);
SqlDataAdapter da = new SqlDataAdapter("SELECT Id,Name FROM Travel.dbo.Spot order by Name", Conn);
DataSet ds = new DataSet();
da.Fill(ds, "Spot"); //第二、執行SQL指令,取出資料
DropDownList1.DataValueField = "Id"; //在此輸入的是資料表的欄位名稱
DropDownList1.DataTextField = "Name"; //在此輸入的是資料表的欄位名稱
DropDownList1.DataSource = ds.Tables["Spot"].DefaultView;
DropDownList1.DataBind();
Conn.Close();
留言
張貼留言