日期:2014-05-18 浏览次数:21125 次
public List<string> QueryAuthor(string strID)
        {
            string sql = "SELECT au_lname FROM authors WHERE au_id = @au_id";
 
            DbParameter param= dbUtility.CreateDbParameter("@au_id", strID);
            List<DbParameter> lstParam = new List<DbParameter>();
            lstParam.Add(param);
            List<string> result = new List<string>();
          result = dbUtility.QueryForList<string>(sql, lstParam, CommandType.Text);
            return result;
        }
public List<T> QueryForList<T>(string sql, IList<DbParameter> parameters, CommandType commandType) where T : new()
        {
            DataTable data = ExecuteDataTable(sql, parameters, commandType);
            return EntityReader.GetEntities<T>(data);
        }
public static List<T> GetEntities<T>(DataTable dataTable) where T : new()
        {
            if (dataTable == null)
            {
                throw new ArgumentNullException("dataTable");
            }
            //如果T的类型满足以下条件:字符串、ValueType或者是Nullable<ValueType>
            if (typeof(T) == typeof(string) || typeof(T) == typeof(byte[]) || typeof(T).IsValueType)
            {
                return GetSimpleEntities<T>(dataTable);
            }
            else
            {
                return GetComplexEntities<T>(dataTable);
            }
        }
List<Message> result= QueryForList(sql, parameters, commandType, datarow=>
  {
      return new Message{ 创建时间= Datetime.Now,
                          发件人= (string)datarow["发件人"],
                          收件人= (string) datarow["收件人"],
                          内容= (string)datarow["内容"]
                        };
  })