Discussion:
(C#.NET)關於DropDownList(下拉式選單)連接資料庫的問題!!??
(时间太久无法回复)
Wesley
2004-10-20 02:11:01 UTC
Permalink
請問一下各位老師:
我在C#.NET中,拉了八個以上的DropDownList,
而且這些DropDownList的TEXT跟VALUE值都是抓取同樣一個TABLE,
連結一個DropDownList的語法==>例如:
string sqlstring =
"select * from TABLE";
SqlConnection MyConnection = new SqlConnection("server=(local) database =
DDSD;Trusted_connection=yes");
SqlCommand MyCommand = new SqlCommand(sqlstring, MyConnection);

MyConnection.Open();
SqlDataReader dr = MyCommand.ExecuteReader();


DropDownList2.DataSource = dr;
DropDownList2.DataTextField = "CSS_F_Name";
DropDownList2.DataValueField = "CSS_F_No";

DropDownList2.DataBind();

MyConnection.Close();
==============================================
但是,我剛剛有提到,我連的都是同一個TABLE,請問,我要怎麼樣只開一次資料庫,將八個DropDownList的DataSource都指到同一個資料集,因為我現在的做法是開八次的OPEN、八次的連結、八次的CLOSE,那這樣執行起來的效率應該會大幅的下降吧!!
請問各位老師,我該如何解決,讓我只連一次資料庫,就能將這八個DropDownList的DataSource都能指到同一個資料集??
謝謝各位老師!!
€pŠ¶ [MS SQL MVP]
2004-10-20 02:25:40 UTC
Permalink
«ØijšÏ¥Î DataSet .
³oŒË¥u»Ý­n¶ñ€@Šžžê®ÆŽN¥išÑ€K­Ó DropDownList šÏ¥Î€F .
--
==========================================================
·L³n³ÌŠ³»ù­È±M®a
Microsoft MVP
Š¶©ú€€(Jeff Chu) for SQL Server

Protected by Windows XP Service Pack 2 with Advanced Security Technologies

Åý§Ú­Ì§â·L³n§K¶O§Þ³NªA°È¶Ç¥X¥h
šÓŠÛ·L³n§Þ³NªÀžs(Technical Communities)¡AšSŠ³ªÅ¶¡­­šîªº§K¶O§Þ³N€äŽ©ªA°È !

Who am I ? Please visit my Web Site:
€pŠ¶žê°T€u§@«Ç http://www.jcistudio.idv.tw
==========================================================
§ÚŠbC#.NET€€¡A©Ô€F€K­Ó¥H€WªºDropDownList¡A
string sqlstring =
"select * from TABLE";
SqlConnection MyConnection = new SqlConnection("server=(local) database =
DDSD;Trusted_connection=yes");
SqlCommand MyCommand = new SqlCommand(sqlstring, MyConnection);
MyConnection.Open();
SqlDataReader dr = MyCommand.ExecuteReader();
DropDownList2.DataSource = dr;
DropDownList2.DataTextField = "CSS_F_Name";
DropDownList2.DataValueField = "CSS_F_No";
DropDownList2.DataBind();
MyConnection.Close();
==============================================
ÁÂÁŠUŠìŠÑ®v!!
unknown
2004-10-22 00:23:02 UTC
Permalink
"Wesley" 來函:
Post by Wesley
請問一下各位老師:
我在C#.NET中,拉了八個以上的DropDownList,
而且這些DropDownList的TEXT跟VALUE值都是抓取同樣一個TABLE,
string sqlstring =
"select * from TABLE";
SqlConnection MyConnection = new SqlConnection("server=(local) database =
DDSD;Trusted_connection=yes");
SqlCommand MyCommand = new SqlCommand(sqlstring, MyConnection);
MyConnection.Open();
SqlDataReader dr = MyCommand.ExecuteReader();
DropDownList2.DataSource = dr;
DropDownList2.DataTextField = "CSS_F_Name";
DropDownList2.DataValueField = "CSS_F_No";
DropDownList2.DataBind();
MyConnection.Close();
==============================================
但是,我剛剛有提到,我連的都是同一個TABLE,請問,我要怎麼樣只開一次資料庫,將八個DropDownList的DataSource都指到同一個資料集,因為我現在的做法是開八次的OPEN、八次的連結、八次的CLOSE,那這樣執行起來的效率應該會大幅的下降吧!!
請問各位老師,我該如何解決,讓我只連一次資料庫,就能將這八個DropDownList的DataSource都能指到同一個資料集??
謝謝各位老師!!
你可以用一個loop 來做,如以下的程式內容

DDSD;Trusted_connection=yes");
SqlCommand MyCommand = new SqlCommand(sqlstring, MyConnection);

MyConnection.Open();
SqlDataReader dr = MyCommand.ExecuteReader();

DropDownList dpl =new DropDownList ();
for(int i=1;i<8;i++)
{
dpl=(DropDownList)Page.FindControl ("DropDownList" + i.ToString ());
dp1.DataSource = dr;
dp1.DataTextField = "CSS_F_Name";
dp1.DataValueField = "CSS_F_No";

dp1.DataBind();
}
MyConnection.Close();

继续阅读narkive:
Loading...