日期:2014-05-17  浏览次数:21328 次

数据库查询的时候报错,“string”必须是具有公共的无参数构造函数的非抽象类型,才能用作泛型类型或方法
本帖最后由 hjhxqxx 于 2012-05-20 07:57:01 编辑
代码:

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);
            }