/// <summary> /// 执行SQL语句,返回影响的行数 /// </summary> /// <param name="sql"></param> /// <returns></returns> public static int Execute(string sql, params object[] parameters) { int result = 0; try { using (BaseSqlContext db = new BaseSqlContext()) { result = db.Database.ExecuteSqlCommand(sql, parameters); } } catch (Exception ex) { throw new Exception("操作数据失败!", ex); } return(result); }
/// <summary> /// 执行SQL语句,返回集合列表 /// </summary> /// <param name="sql"></param> /// <returns></returns> public static List <T> GetQuery <T>(string sql, params object[] parameters) { List <T> enumerable = null; try { using (BaseSqlContext db = new BaseSqlContext()) { enumerable = db.Database.SqlQuery <T>(sql, parameters).ToList(); } } catch (Exception ex) { throw new Exception("操作数据失败!", ex); } return(enumerable); }
/// <summary> /// 执行SQL语句,返回List<string> 集合,限制:sql 语句中 select name from tableName , 只能存在name一个字段 /// </summary> /// <param name="sql"></param> /// <param name="parameters"></param> /// <returns></returns> public static List <string> GetList(string sql, params object[] parameters) { List <string> listString = null; try { using (BaseSqlContext db = new BaseSqlContext()) { listString = db.Database.SqlQuery <string>(sql).ToList(); } } catch (Exception ex) { throw new Exception("操作数据失败!", ex); } return(listString); }