日期:2008-11-12  浏览次数:20426 次

一、取得数据表 i 行 j 列的值   

     //建立并打开数据库连接
    OleDbConnection conn=new OleDbConnection();
    conn.ConnectionString=strConnectionString;//strConnectionString为数据库连接字符串
    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()//数据表i行j列的值  
    conn.close();

二、将数据读入DropDownList

(1)  DropDownList中添加数据

  //建立并打开数据库连接
    OleDbConnection conn=new OleDbConnection();
    conn.ConnectionString=strConnectionString;//strConnectionString为数据库连接字符串
    conn.Open();

    string sql="select * from NewsClass order by ClassId desc";
    //建立数据集
    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

三、分类编码检索出相应的分类名称并在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>