SqlSugar 执行sql语句
var t12 = psdb.Db.SqlQueryable<dynamic> ("sql") .OrderBy(x => x.CreateTime, OrderByType.Desc) .ToPageList(pageModel.PageIndex, pageModel.PageSize, ref totalCount);//返回动态类型
ISugarQueryable<Yszycx> d;
var d = newsdb.Db.SqlQueryable<Kyzycx>(sql); if (type.Length > 0) { d = d.Where(n => n.资源类型 == type); }
if (key.Length > 0)
{
d = d.Where(n => n.资源名称.Contains(key));
}
list = d.ToPageList(pageinfo.PageIndex, pageinfo.PageSize, ref totalNumber);//返回动态类型
pageinfo.PageCount = totalNumber;
namespace Dal { public class Dal_News : DbContext<Model_News> { #region Dal /// <summary> /////插入一条数据,插入并返回受影响行数用ExecuteCommand /// </summary> /// <param name="model"></param> /// <returns></returns> public int Insert_(Model_News model) { return Db.Insertable(model).ExecuteCommand(); } public DataTable GetDataTable_(string sql) { return Db.Ado.GetDataTable(sql); } //string.Format("select max(M_Serial)M_Serial from T_Sys_MenuInfo where 1=1 and M_PKey = 0") /// <summary> /// 执行sql 语句 返回一行一列 /// </summary> /// <param name="sql"></param> /// <returns></returns> public object GetOneRowOneColumn_(string sql) { var dt = Db.Ado.GetDataTable(sql); if (dt.Rows.Count > 0) { return dt.Rows[0][0]; } return null; } public DataTable selectTable() { string sql = "SELECT 1 as nId,0 as num,'' as name,'' as link "; return Db.Ado.GetDataTable(sql); } public DataTable SelectTop(string where, int top) { string sql = "SELECT TOP " + top + " * FROM [News] " + where; return GetDataTable_(sql); } #endregion } }
#region 执行 public DataTable ExecuteDataTable(DbCommand cmd) { var sql = cmd.CommandText; var paras = cmd.Parameters; if (paras.Count > 0) { //参数1 var data = new List<SugarParameter>(); for (int i = 0; i < paras.Count; i++) { data.Add(new SugarParameter(paras[i].ParameterName, paras[i].Value)); } return dal.Db.Ado.GetDataTable(sql, data); } else { return dal.Db.Ado.GetDataTable(sql); } } public DataRow ExecuteDataRow(DbCommand cmd) { DataTable dataTable = new DataTable(); var sql = cmd.CommandText; var paras = cmd.Parameters; if (paras.Count > 0) { //参数1 var data = new List<SugarParameter>(); for (int i = 0; i < paras.Count; i++) { data.Add(new SugarParameter(paras[i].ParameterName, paras[i].Value)); } dataTable = dal.Db.Ado.GetDataTable(sql, data); } else { dataTable = dal.Db.Ado.GetDataTable(sql); } if (dataTable.Rows.Count > 0) { return dataTable.Rows[0]; } else { return null; } } public int ExecuteNonQuery(DbCommand cmd) { var sql = cmd.CommandText; var paras = cmd.Parameters; if (paras.Count > 0) { var data = new List<SugarParameter>(); for (int i = 0; i < paras.Count; i++) { data.Add(new SugarParameter(paras[i].ParameterName, paras[i].Value)); } return dal.Db.Ado.ExecuteCommand(sql, data); } else { return dal.Db.Ado.ExecuteCommand(sql); } } public object ExecuteScalar(DbCommand cmd) { DataTable dataTable = new DataTable(); var sql = cmd.CommandText; var paras = cmd.Parameters; if (paras.Count > 0) { //参数1 var data = new List<SugarParameter>(); for (int i = 0; i < paras.Count; i++) { data.Add(new SugarParameter(paras[i].ParameterName, paras[i].Value)); } dataTable = dal.Db.Ado.GetDataTable(sql, data); } else { dataTable = dal.Db.Ado.GetDataTable(sql); } if (dataTable.Rows.Count > 0) { return dataTable.Rows[0][0]; } else { return null; } } /// <summary> /// 摘要: /// 数据分页函数 /// 参数: /// cmdText:传入要执行sql语句 /// orderField:排序字段 /// orderType:排序类型 /// pageIndex:当前页 /// pageSize:页大小 /// count:返回查询条数 /// </summary> public DataTable GetPageList(string cmdText, string orderField, string orderType, int pageIndex, int pageSize, ref int count) { int num = (pageIndex - 1) * pageSize; int num1 = (pageIndex) * pageSize; string sql = "Select * From (Select ROW_NUMBER() Over (Order By " + orderField + " " + orderType; sql += ") As rowNum, * From (" + cmdText + ") As T ) As N Where rowNum > " + num + " And rowNum <= " + num1; string sql1 = "Select Count(1) From (" + cmdText + ") As t"; DbCommand cmd = GetSqlStringCommond(sql1); count = (int)ExecuteScalar(cmd); cmd = GetSqlStringCommond(sql); return ExecuteDataTable(cmd); } #endregion