熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

ASP.NET與數據庫相關技巧

2022-06-13   來源: .NET編程 
    取得數據表 i 行 j 列的值

//建立並打開數據庫連接

OleDbConnection conn=new OleDbConnection();

connConnectionString=strConnectionString;//strConnectionString為數據庫連接字符串

connOpen();



string sql=select * from NewsClass order by ClassId desc;

string x;

DataSet ds=new DataSet();

OleDbDataAdapter da=new OleDbDataAdapter(sqlconn);

daFill(dsNewsTable);

DataTable dt=dsTables[NewsTable];

x=dtRows[i][]ToString()//數據表i行j列的值

connclose(); 

    將數據讀入DropDownList 

    ()DropDownList中添加數據

//建立並打開數據庫連接

OleDbConnection conn=new OleDbConnection();

connConnectionString=strConnectionString;//strConnectionString為數據庫連接字符串

connOpen();



string sql=select * from NewsClass order by ClassId desc;

//建立數據集

DataSet ds=new DataSet();

OleDbDataAdapter da=new OleDbDataAdapter(sqlconn);

daFill(dsNewsTable);

thisDropDownListDataSource=ds;

thisDropDownListDataTextField = ClassName;//Text值

thisDropDownListDataValueField = ClassID;//Value值

thisDropDownListDataBind();



connClose();

    ()選中DropDownList的某項

thisDropDownListItemsFindByValue(dr[ClassID]ToString()Trim())Selected=true;

//dr為DataRow

分類編碼檢索出相應的分類名稱並在DataGrid中顯示 

ASPX中的代碼(ClassID為分類編碼)

<asp:TemplateColumn HeaderText=類 別>

<ItemTemplate>

<asp:Label id=lblClass runat=server

Text=<%# GetClassName(ConvertToInt(DataBinderEval(Container

DataItemClassID))) %>>

</asp:Label>

</ItemTemplate>

</asp:TemplateColumn>

)C#代碼

///



/// 分類列根據數字返回文字

///


///

///

public string GetClassName(int ClassID)

{

OleDbConnection conn=new OleDbConnection();

connConnectionString=strConnectionString;

connOpen();



string sql=select * from NewsClass where ClassID=+ClassID;

DataSet ds=new DataSet();

OleDbDataAdapter da=new OleDbDataAdapter(sqlconn);

daFill(dsClassTable);

DataTable dt=dsTables[ClassTable];

string strClassName=dtRows[][ClassName]ToString();

connClose();



return strClassName;//返回 ClassID對應的ClassName

}


From:http://tw.wingwit.com/Article/program/net/201311/15348.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.