开始直接建个空的WEB项目-建Controllers文件夹-开启MVC-添加NuGet程序包SqlSugarCore

  public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(); //注册mvc服务
} // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
} app.UseMvc(routes=> { //开启mvc
routes.MapRoute(
name:"default",
template:"{controller=Home}/{action=Index}/{id?}"
);
});
}
}

把数据库的连接语句写到appsettings.json里面:

{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"DBSetting": {
"ConnectString": "server=.;database=test_core;uid=sa;pwd=123" },
"AllowedHosts": "*"
}

创建BaseHelper类:

 public class BaseHelper
{
public SqlSugarClient db; static IConfiguration configure = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json").Build(); private static readonly string _connectionstring = configure["DBSetting:ConnectString"];
// public BaseHelper(string connectionString)
public BaseHelper()
{
db = new SqlSugarClient(
new ConnectionConfig()
{
ConnectionString = _connectionstring,
DbType = DbType.SqlServer,//设置数据库类型
IsAutoCloseConnection = true,//自动释放数据务,如果存在事务,在事务结束后释放
InitKeyType = InitKeyType.Attribute //从实体特性中读取主键自增列信息
}); //用来打印Sql方便你调式
db.Aop.OnLogExecuting = (sql, pars) =>
{
Console.WriteLine(sql + "\r\n" +
db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
Console.WriteLine();
};
}
public SqlSugarClient GetDb()
{
return db;
} public bool InsertInto<T>(T obj) where T : class, new()
{
return db.Insertable(obj).ExecuteCommandIdentityIntoEntity();
} public int UpdateInfo<T>(Expression<Func<T, bool>> set, Expression<Func<T, bool>> where) where T : class, new()
{
return db.Updateable<T>().SetColumns(set).Where(where).ExecuteCommand();
}
}

直接在控制器操作即可:

 public class HomeController : Controller
{
private static SqlSugarClient _db = new BaseHelper().GetDb();
private SimpleClient<Student> db = new SimpleClient<Student>(_db);
public IActionResult Index()
{
//db.Insert(new Student()
//{
// ClassId = 113,
// Name = "小明"
//}); //_db.Insertable(new Student() {ClassId = 1,Name = "小高"}).ExecuteCommandIdentityIntoEntity(); //var re = _db.Updateable(new Student() { ClassId=2, Name = "小梅" }).Where(p=>p.ClassId==2).ExecuteCommand(); //更新全部 //_db.Updateable<Student>().SetColumns(p=>p.Name=="小小").Where(p => p.ClassId == 2).ExecuteCommand(); //更新指定字段 //_db.Deleteable<Student>().Where(p => p.Name == "2").ExecuteCommand(); //删除 UpdateInfo<Student>(p=>p.Name=="大大",p=>p.ClassId==); new BaseHelper().InsertInto<Student>(new Student()
{
ClassId = ,
Name = "小明"
});
return View();
}
public bool InsertInto<T>(T obj) where T : class, new()
{
return _db.Insertable(obj).ExecuteCommandIdentityIntoEntity();
}
public int UpdateInfo<T>(Expression<Func<T, bool>> set, Expression<Func<T, bool>> where) where T : class, new()
{
return _db.Updateable<T>().SetColumns(set).Where(where).ExecuteCommand();
}
}

还有很多可以直接使用的方法,可以去官网看看 => http://www.codeisbug.com/Home/Doc

.NET Core的SqlSugar上手使用小例子的更多相关文章

  1. ASP.NET Nlog上手练习小例子

    添加NuGet程序包-             Nlog             Nlog.Web.AspNetCore 两个包. public void Configure(IApplication ...

  2. 【zTree】 zTree使用的 小例子

    使用zTree树不是第一次了  但是 还是翻阅着之前做的 对照着 使用起来比较方便  这里就把小例子列出来   总结一下使用步骤 这样方便下次使用起来方便一点 使用zTree树的步骤: 1.首先  在 ...

  3. 2、Lucene 最简单的使用(小例子)

    在了解了Lucene以后,我打算亲手来做一个Lucene的小例子,这个例子只是Lucene最简单的应用:使用Lucene实现标准的英文搜索: 1.下载Lucene 下载Lucene,到Lucene的官 ...

  4. jsf小例子

    有人问我用过jsf没?   当时没有用过,就看了一下: 写了一个小例子  JSF和struts2 差不多的,都有一些配置和跳转 struts2的action配置和JSF的faces-config.xm ...

  5. c/c++ 继承与多态 文本查询的小例子(非智能指针版本)

    问题:在上一篇继承与多态 文本查询的小例子(智能指针版本)在Query类里使用的是智能指针,只把智能指针换成普通的指针,并不添加拷贝构造方法,会发生什么呢? 执行时,代码崩掉. 分析下面一行代码: Q ...

  6. Tag recommendaion... 论文中的小例子,使用HOSVD算法推荐

    本文内容来自于论文:Tag recommendations based on tensor dimensioanlity reduction 在社会标签系统中,存在三元关系,用户-物品-标签.这些数据 ...

  7. Spring.Net在ASP.NET Mvc里使用的一个小例子

    就贴个小例子,就不注意格式了. 1.下载dll NuGet的下载地址:http://docs.nuget.org/docs/start-here/installing-nuget 在vs的NuGet里 ...

  8. springmvc入门的第一个小例子

    今天我们探讨一下springmvc,由于是初学,所以简单的了解一下 springmvc的流程,后续会持续更新... 由一个小例子来简单的了解一下 springmvc springmvc是spring框 ...

  9. java即时通信小例子

    学习java一段时间了,今天写来一个即时通信的小例子练手在其过程中也学到了一些知识拿出来和大家分享,请路过的各位大神多多赐教... 好了下面讲一下基本的思路: 首先,编写服务器端的程序,简单点说吧就是 ...

随机推荐

  1. js使浏览器窗口最大化(适用于IE的方法)

    这里使用的方法是IE的私有特性,只能在IE中有效.主要是window.moveTo和 window.resizeTo方法.       效果和点击最大化按钮差不多,有一点区别.点击最大化按钮后,浏览器 ...

  2. DNS域名解析失败 和 何时会查询下一个nameserver

    阿里DNS:域名解析失败的那些事:https://zhuanlan.zhihu.com/p/40659713 只有第一个nameserver响应超时,才会请求下一个nameserver. 收到 NOD ...

  3. MySQL 性能调优

    MySQL 性能调优   索引 索引是什么 官方介绍索引是帮助MySQL高效获取数据的数据结构.笔者理解索引相当于一本书的目录,通过目录就知道要的资料在哪里,不用一页一页查阅找出需要的资料. 索引目的 ...

  4. Linux环境下安装python3

    1.安装前准备 CentOS 7 中默认安装了 Python,版本:2.7.5,由于很多基本的命令.软件包都依赖旧版本,比如:yum.所以,在更新 Python 时,建议不要删除旧版本,而且新旧版本可 ...

  5. dubbo学习笔记(二)dubbo中的filter

    转:https://www.cnblogs.com/cdfive2018/p/10219730.html dubbo框架提供了filter机制的扩展点(本文基于dubbo2.6.0版本). 扩展接口 ...

  6. dubbo源码分析之基于SPI的强大扩展

    https://blog.csdn.net/luoyang_java/article/details/86609045 Dubbo采用微内核+插件体系,使得设计优雅,扩展性强.那所谓的微内核+插件体系 ...

  7. GitLab的权限管理及Merge Request

    GitLab的权限管理及Merge Request 原创尘世间一名迷途小码农 发布于2019-06-09 12:40:30 阅读数 2909  收藏 展开 目录 1.前言 2.角色权限 3.强制代码审 ...

  8. (转)plsql11 x64 安装和配置 解决OCI: not initialized

    跟帖子一样,安装了pl/sql ,设置了oci.dll 以及 TNS_ADMIN,加入path后不能显示数据库连接. 安装 microsoft visual c++ redistributable 2 ...

  9. idea 出现 bootstrap.properties 中的内容不能识别

    错误截图如下 依赖库引用问题 第一种: <dependency> <groupId>org.springframework.cloud</groupId> < ...

  10. maven-archetype-plugin 的正确打开方式

    1.  准备好一个编辑好的模板工程 2. 在 pom.xml 中添加 maven-archetype-plugin 插件 <plugin> <groupId>org.apach ...