protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
IList list = GetList(System.Configuration.ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString,
"select * from LYZX_NewsType");
GridView1.DataSource = list;
GridView1.DataBind();
}
}
public string GetNewsTypeLink(ref string baseUrl,Guid newsType)
{
return "";
}
///
/// 获取泛型集合
/// ///
/// /// 类型
/// /// 数据库连接字符串
/// 要查询的T-SQL
///
public IList GetList(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(ds, 0);
}
}
}
///
/// DataSetToList
///
/// 转换类型
/// 数据源
/// 需要转换表的索引
/// /// 泛型集合
public IList DataSetToList(DataSet dataset,int tableIndex)
{
//确认参数有效
if (dataset==null || dataset.Tables.Count<=0|| tableIndex<0)
{
return null;
}
DataTable dt = dataset.Tables[tableIndex];
IList list = new List();
for (int i = 0; i < dt.Rows.Count; i++)
{
//创建泛型对象
T _t=Activator.CreateInstance();
//获取对象所有属性
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;
}