一、取得數(shù)據(jù)表 i 行 j 列的值
//建立并打開數(shù)據(jù)庫連接
oledbconnection conn=new oledbconnection();
conn.connectionstring=strconnectionstring;//strconnectionstring為數(shù)據(jù)庫連接字符串
conn.open();
string sql="select * from newsclass order by classid desc";
string x;
dataset ds=new dataset();
oledbdataadapter da=new oledbdataadapter(sql,conn);
da.fill(ds,"newstable");
datatable dt=ds.tables["newstable"];
x=dt.rows[i][1].tostring()//數(shù)據(jù)表i行j列的值
conn.close();
二、將數(shù)據(jù)讀入dropdownlist
(1) dropdownlist中添加數(shù)據(jù)
//建立并打開數(shù)據(jù)庫連接
oledbconnection conn=new oledbconnection();
conn.connectionstring=strconnectionstring;//strconnectionstring為數(shù)據(jù)庫連接字符串
conn.open();
string sql="select * from newsclass order by classid desc";
//建立數(shù)據(jù)集
dataset ds=new dataset();
oledbdataadapter da=new oledbdataadapter(sql,conn);
da.fill(ds,"newstable");
this.dropdownlist1.datasource=ds;
this.dropdownlist1.datatextfield = "classname";//text值
this.dropdownlist1.datavaluefield = "classid";//value值
this.dropdownlist1.databind();
conn.close();
(2)選中dropdownlist的某項
this.dropdownlist1.items.findbyvalue(dr["classid"].tostring().trim()).selected=true;//dr為datarow
三、分類編碼檢索出相應(yīng)的分類名稱并在datagrid中顯示
(1).aspx中的代碼(classid為分類編碼):
<asp:templatecolumn headertext="類 別">
<itemtemplate>
<asp:label id=lblclass runat="server" text='<%# getclassname(convert.toint32(databinder.eval(container, "dataitem.classid"))) %>'>
</asp:label>
</itemtemplate>
</asp:templatecolumn>
新聞熱點(diǎn)
疑難解答
圖片精選