解决方案 »
- BBSMAX部署default.aspx"中模版变量"PageTitle"不存在,请检查模版
- Jquery问题
- 这两段代码哪个比较好呀
- 图片显示链接,image类型
- ASP.NET中的RadioButton问题???
- .cs文件为什么始终不能用csc命令编译为.dll
- [馨郁星愿]web打印小问题,不知有人知道怎么解决否?
- 请问GridView排序后更新的问题
- 急问HtmlEditor问题,如何用HtmlEditor获取文章内容并编辑修改保存!!在线等!~
- 如何在web实现选择文件的对话框啊????????
- 【迷惑】一直没有明白ASP.NET中的变量作用域问题
- Dataset和SqlDataReader
T t = new T()public class Parent
{
public string PropertyA {get; set}
}public static List<xxx> Method(List<xxx> listXxx) where xxx : Parent 泛型约束
xxEntity.PropertyA = value; //泛型约束,由父类或接口定义属性 那么xxx才有明确的属性
根据你的提示,搞定了,谢谢。给后来人做参考,把搞定后的代码贴上来,本来是实现数据访问层的一个通用查询方法。using System;
using System.Configuration;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;namespace S6.Data
{
internal class DataHandler<T> where T:new ()
{
private static SqlConnection con = null;
private static string conString = ConfigurationManager.ConnectionStrings["bmcConnection"].ConnectionString; public static List<T> ExcuteList(SqlCommand cmd)
{
con=new SqlConnection(conString);
cmd.Connection = con;
List<T> listEntity = new List<T>(); try
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
T entity = new T();
Type t = entity.GetType();
foreach (PropertyInfo pi in t.GetProperties())
{
pi.SetValue(entity, reader[pi.Name],null);
}
listEntity.Add(entity);
}
reader.Close();
}
catch
{
throw new Exception("在数据库中获取列表导常。");
}
finally
{
con.Close();
} return listEntity;
}
}
}