排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:
50010702506256
50010702506256
欢迎加群交流技术
原
sqlsugar官方文档与基本用法,sqlsugar直接执行sql,sqlsugar分组函数。Sqlsugar官网。sqlsugar在直接执行sql中使用In。sqlsugar 返回第一行第一列。Sql分页
分类:
sqlsugar
sqlsugar官方文档
https://www.donet5.com/Home/Doc
Sql分页
https://www.donet5.com/Home/Doc?typeId=1197
sqlsugar基本用法
下载依赖:
<ItemGroup> <PackageReference Include="SqlSugarCore" Version="5.0.5.4" /> </ItemGroup>
使用:
class Program
{
static void Main(string[] args)
{
//创建数据库对象
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = "Server=.;Database=OA;uid=sa;password=123456",
DbType = DbType.SqlServer,
IsAutoCloseConnection = true
});
db.Aop.OnLogExecuting = (sql, pars) =>
{
Console.WriteLine(sql);//输出sql
Console.WriteLine(string.Join(",", pars?.Select(it => it.ParameterName + ":" + it.Value)));//参数
};
//查询
List<Users> List = db.Queryable<Users>().Where(a=>a.Number.Contains("004")).ToList();
Console.ReadLine();
}
}实体类:
public class Users
{
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int Id { get; set; }
public string UserName { get; set; }
public string Number { get; set; }
}
直接执行sql:
直接执行sql的官方文档:
https://www.donet5.com/Home/Doc?typeId=1198
基础的查询
var list = SqlSugarHelper.Db.Ado.SqlQuery<statisticsdataday>(sql,new { pubdate = "2022-8-1" }).ToList();这样也可以:
public List<CourseDTO> GetAllCourseByTeacher(string teacherId)
{
string sql = @"select ID as CourseId,CourseName from education.course where id in (select CourseID from v_cmcourseteacher where teacherid = @teacherId) and CourseType = 1
and TenantID = @TenantID";
List<SugarParameter> sugarParameters = new List<SugarParameter>(){
new SugarParameter("@teacherId",teacherId),
new SugarParameter("@TenantID",TenantID) //执行sql语句
};
List<CourseDTO> courseDTOs = Db.Ado.SqlQuery<CourseDTO>(sql, sugarParameters).ToList();
return courseDTOs;
}还可以这样,这样可以使用异步的形式:
await Db.SqlQueryable<ScoreOther>(sql)
.AddParameters(new { StuId = stuId, STime = startDate, ETims = endDate })
.ToListAsync();
sqlsugar在直接执行sql中使用In
public List<LabTearchInfoDto> GetTearchInfoByCourseIdListAndStudentId(List<string> courseIdList, string studentId, int tenantID)
{
string sql = @"select TeacherID,CourseID from education.cmcourseteacher where CourseID in (@CourseID) and CMID
in(select ID from education.classmajor where ClassID in
(select ClassID from education.studentclass where Stats = 0 and StudentID = @StudentID and TenantID = @TenantID) )";
//sql = @"select TeacherID from education.cmcourseteacher where CourseID = '1a9b5c35f6ad47ddab639d39f4c02664' and CMID in(select ID from education.classmajor where ClassID in (select ClassID from education.studentclass where Stats = 0 and StudentID = '51399fc9cafc449092a0d0f90e6024af' and TenantID = 32) )";
List<LabTearchInfoDto> labTearchInfoDtos = Db.Ado.SqlQuery<LabTearchInfoDto>(sql, new { CourseID = courseIdList, StudentID = studentId, TenantID = tenantID }).ToList();
// 线下库没有配置教员信息,我直接模拟一下数据吧
//labTearchInfoDtos.Add(new LabTearchInfoDto() { TeacherID= "579cd78257eb4a32a22549b86bcb3f73" });
//labTearchInfoDtos.Add(new LabTearchInfoDto() { TeacherID = "b6393314ee084f7c8bc41aba11b33f06" });
return labTearchInfoDtos;
}
sqlsugar 返回第一行第一列
代码如下:
string sql = @"select COUNT(labr.ID) Count from labtaskresult labr where labr.TenantID = @TenantID and labr.TaskType = 4 and labr.LastUpdateTime>=@startDate
and labr.LastUpdateTime<@endDate";
// 开始日期
DateTime startDate = DateTime.Now.Date;
// 结束时间等于今天加一天,因为时间取的是一个范围
DateTime endDate = DateTime.Now.AddDays(1).Date;
int completeCount = Db.Ado.SqlQuery<int>(sql, (new { stId = input.StdetailID, TenantID= input.TenantID, startDate = startDate, endDate = endDate })).FirstOrDefault();
分组函数:
var listM = SqlSugarHelper.Db.Queryable<statisticsdata_month>().WhereIF(query.MajorID != 0, a => a.majorId == query.MajorID)
.WhereIF(!string.IsNullOrWhiteSpace(query.RegLevelCode), a => a.levelCode.Contains(query.RegLevelCode))
.SplitTable(stime, etime).GroupBy(a => a.pubDate).Select(a => new
{
pubDate = a.pubDate,
reqCount = SqlSugar.SqlFunc.AggregateSum(a.reqCount),
positionCount = SqlSugar.SqlFunc.AggregateSum(a.positionCount)
}).OrderBy(a => a.pubDate, SqlSugar.OrderByType.Asc).ToList();欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739。有需要软件开发,或者学习软件技术的朋友可以和我联系~(Q:815170684)
评价