Asp. NET and database related skills

Source: Internet
Author: User

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
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.