时间:2021-05-20
复制代码 代码如下:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
IList<LYZX.Model.LYZX_NewsTypeModel> list = GetList<LYZX.Model.LYZX_NewsTypeModel>(System.Configuration.ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString,
"select * from LYZX_NewsType");
GridView1.DataSource = list;
GridView1.DataBind();
}
}
public string GetNewsTypeLink(ref string baseUrl,Guid newsType)
{
return "";
}
/// <summary>
/// 获取泛型集合
/// /// </summary>
/// /// <typeparam name="T">类型</typeparam>
/// /// <param name="connStr">数据库连接字符串</param>
/// <param name="sqlStr">要查询的T-SQL</param>
/// <returns></returns>
public IList<T> GetList<T>(string connStr, string sqlStr)
{
using (SqlConnection conn = new SqlConnection(connStr))
{
using (SqlDataAdapter sda = new SqlDataAdapter(sqlStr, conn))
{
DataSet ds = new DataSet();
sda.Fill(ds);
return DataSetToList<T>(ds, 0);
}
}
}
/// <summary>
/// DataSetToList
/// </summary>
/// <typeparam name="T">转换类型</typeparam>
/// <param name="dataSet">数据源</param>
/// <param name="tableIndex">需要转换表的索引</param>
/// /// <returns>泛型集合</returns>
public IList<T> DataSetToList<T>(DataSet dataset,int tableIndex)
{
//确认参数有效
if (dataset==null || dataset.Tables.Count<=0|| tableIndex<0)
{
return null;
}
DataTable dt = dataset.Tables[tableIndex];
IList<T> list = new List<T>();
for (int i = 0; i < dt.Rows.Count; i++)
{
//创建泛型对象
T _t=Activator.CreateInstance<T>();
//获取对象所有属性
PropertyInfo [] propertyInfo=_t.GetType().GetProperties();
//属性和名称相同时则赋值
for (int j = 0; j < dt.Columns.Count; j++)
{
foreach (PropertyInfo info in propertyInfo)
{
if (dt.Columns[j].ColumnName.ToUpper().Equals(info.Name.ToUpper()))
{
if (dt.Rows[i][j]!=DBNull.Value)
{
info.SetValue(_t, dt.Rows[i][j], null);
}
else
{
info.SetValue(_t, null, null);
}
break;
}
}
}
list.Add(_t);
}
return list;
}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
C#泛型和数组在C#2.0中,下限为零的一维数组自动实现IList。这使您可以创建能够使用相同代码循环访问数组和其他集合类型的泛型方法。此技术主要对读取集合中的
一、#List泛型集合集合是OOP中的一个重要概念,C#中对集合的全面支持更是该语言的精华之一。为什么要用泛型集合?在C#2.0之前,主要可以通过两种方式实现集
C#的System.Collections命名空间包含可使用的集合类和相关的接口,提供了集合的基本功能。包括了.NET下的非泛型集合类以及非泛型接口等,现详述如
要使用Dictionary集合,需要导入C#泛型命名空间System.Collections.Generic(程序集:mscorlib)Dictionary的描
1、要使用Dictionary集合,需要导入C#泛型命名空间 System.Collections.Generic(程序集:mscorlib)2、描述 1)