开始直接建个空的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

最新文章

  1. 关于javascript的一些知识以及循环
  2. vmware12用 unlocker206能不能解锁 OS X系统
  3. 转:PHP Composer 管理工具的介绍 这个相对清晰点
  4. windows系统mysql定时自动备份
  5. 自动构建Makefile(1)--C/C++编译流程&amp;Makefile规则简介
  6. React Native 简介:用 JavaScript 搭建 iOS 应用 (1)
  7. 对于Oracle中分页排序查询语句执行效率的比较分析
  8. 深入Android开发之--理解View#onTouchEvent
  9. 【PAT】1025. PAT Ranking (25)
  10. how to make a git repo un-git?
  11. VMware中Ubuntu 14.04出现Unknown Display问题解决
  12. linux学习(六)绝对路径、相对路径、cd、mkdir、rmdir、rm
  13. Python内置函数(9)——int
  14. Python3 标准库概览
  15. BZOJ_3942_[Usaco2015 Feb]Censoring_KMP
  16. Big big world
  17. 定时器QTimer
  18. A - 地精部落 (DP)
  19. js代码之编程习惯
  20. 在Docker中运行EOS(MAC版)

热门文章

  1. 获取Android包名和activity名
  2. linux 上使用yum 安装openjdk1.8
  3. Java对姓名, 手机号, 身份证号, 地址进行脱敏
  4. [Gamma阶段]第十次Scrum Meeting
  5. 用Python画一颗特别的心送给她
  6. Multihypothesis Trajectory Analysis for Robust Visual Tracking
  7. pyzbar 安装
  8. C++11原子操作与无锁编程(转)
  9. gcc 编译两个so其中soA依赖soB
  10. 解决:Could not load type &#39;System.ServiceModel.Activation.HttpModule&#39; from assembly &#39;System.ServiceMode