This paper gives the program to implement the source code when using ASP.net database, for example, to get the value of the J column of the data table I row, to read the data into the DropDownList and the classification code to retrieve the corresponding classification name and display it in the DataGrid for your reference!
First, get the value of data table I row J column
//建立并打开数据库连接
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();
II. read the data into the DropDownList
(1) Add data in 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) Select one of the DropDownList
this.DropDownList1.Items.FindByValue(dr["ClassID"].ToString().Trim()).Selected=true;
//dr为DataRow
Third, the classification of the code to retrieve the corresponding category name and display in the DataGrid
(1). The code in ASPX (ClassID is a classified encoding)
<asp:TemplateColumn HeaderText="类 别">
<ItemTemplate>
<asp:Label id=lblClass runat="server"
Text='<%# GetClassName(Convert.ToInt32(DataBinder.Eval(Container,
"DataItem.ClassID"))) %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
(2) C # code
///
/// "分类"列根据数字返回文字
///
///
///
public string GetClassName(int ClassID)
{
OleDbConnection conn=new OleDbConnection();
conn.ConnectionString=strConnectionString;
conn.Open();
string sql="select * from NewsClass where ClassID="+ClassID;
DataSet ds=new DataSet();
OleDbDataAdapter da=new OleDbDataAdapter(sql,conn);
da.Fill(ds,"ClassTable");
DataTable dt=ds.Tables["ClassTable"];
string strClassName=dt.Rows[0]["ClassName"].ToString();
conn.Close();
return strClassName;//返回 ClassID对应的ClassName
}