ASP.NET与数据库相关技巧
程序员文章站
2022-07-19 12:01:47
一、取得数据表 i 行 j 列的值 ...
一、取得数据表 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>
(2)c#代码:
/// <summary>
/// "分类"列根据数字返回文字
/// </summary>
/// <param name="ispassed"></param>
/// <returns></returns>
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
}
//建立并打开数据库连接
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>
(2)c#代码:
/// <summary>
/// "分类"列根据数字返回文字
/// </summary>
/// <param name="ispassed"></param>
/// <returns></returns>
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
}
上一篇: 干燥的秋天女人吃什么水果好?
下一篇: 男性健身七个小方法 让排骨男变身肌肉男