接着刚才那个结题的泛型问题在问一下
public struct arrWare{
public int id;
public string name;
public string desc;
}
public IList <arrWare> GetWares()
{
IList <arrWare> wares = new List <arrWare> ();
//IList <CustomersM> customer = new List <CustomersM> ();
strSql = "select top 10 infoName,infos,infoId from WareInfo ";
//SqlParameter parms = new SqlParameter( "@id ", SqlDbType.Int);
//parms.Value = id;
using (SqlDataReader rdr = SqlHelper.ExecuteReader(CommandType.Text, strSql, null))
{
arrWare ar = new arrWare();
while (rdr.Read())
{
ar.name = rdr.GetString(0);
ar.desc = rdr.GetString(1);
ar.id = rdr.GetInt32(2);
wares.Add(ar);
}
return wares;
}
}
这获得的数据咱个使用呢?如有一个label控件,怎么才能把它的Text属性设为我这获得的ar.name
[解决办法]
<asp:GridView ID= "GridView1 " runat= "server " DataSourceID= "SqlDataSource1 "> </asp:GridView> <asp:SqlDataSource ID= "SqlDataSource1 " runat= "server " ConnectionString= "Data Source=.;Initial Catalog=Test;User ID=sa;pwd=sa " ProviderName= "System.Data.SqlClient " SelectCommand= "SELECT * FROM [表] where Name like '% '+@Name+ '% ' "> <SelectParameters> <asp:ControlParameter ControlID= "DropDownList1 " Name= "Name " Type= "String " /> </SelectParameters> </asp:SqlDataSource>
[解决办法]
这获得的数据咱个使用呢?如有一个label控件,怎么才能把它的Text属性设为我这获得的ar.name
===========
1。
既然获取的是一个集合,那么应该是用列表控件显示,如 GridView
<asp:GridView ID= "GridView1 " runat= "server ">
<asp:boundfield datatextfield = "name ";
</asp:GridView>
GridView1.DataSource = new arrWare().GetWares();
GridView1.DataBind();
2.
获取单项记录
arrWare aw = new arrWare().GetWares()[0];
MyLabel.Text = aw.name;
3.
实际上,这里你的 arrWare 应该定义为 class ,而不是 struct
4。
另外,对于方法 GetWares(), 定义为 static 的更合适