1、Nuget sqlsugarCore 包
2、配置文件 appsettings.json
"ConnectionStrings": {
//Sqlserver
"BaseDbSqlServer": "",
//Oracle
"BaseDbOracle": "",
//MySql
"BaseDbMySql": ""
},
3、创建 sqlsugarbase 类
using SqlSugar;
namespace WebApp.Models
{
public class SqlsugarBase
{
/// <summary>
/// 读取json配置文件
/// </summary>
private static IConfigurationRoot Configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.Build();
/// <summary>
/// 读取配置文件下的连接字符串
/// </summary>
string connectionString = Configuration.GetSection("ConnectionStrings").GetSection("BaseDbSqlServer").Value;
//string connectionString= Configuration.GetConnectionString("BaseDbSqlServer");
public SqlSugarClient db => GetInstance();
SqlSugarClient GetInstance()
{
var db = new SqlSugarClient(
new ConnectionConfig
{
ConnectionString = connectionString,
DbType = DbType.SqlServer,//数据库类型
IsAutoCloseConnection = true,
InitKeyType = InitKeyType.Attribute
}
);
return db;
}
}
}
4、创建 调用类 并继承base类
namespace WebApp.Models
{
public class Service:SqlsugarBase
{
public List<Movie> GetUserList()
{
List<Movie> list = new List<Movie>();
list = db.Queryable<Movie>().ToList();
return list;
}
}
}
5、控制器使用
Service Service=new Service();
public IActionResult Index()
{
ViewData.Model = Service.GetUserList();
return View();
}
6、视图
@{
ViewData["Title"] = "Home Page";
}
<html>
<head>
<script type="text/javascript" src="~/Scripts/jquery-3.4.1.js"></script>
</head>
<body>
<h2>This is a test</h2>
<h4>Movie</h4>
<br />
<table class="table">
<thead>
<tr>
<td>Id</td>
<td>Title</td>
<td>ReleaseDate</td>
<td>Genre</td>
<td>Price</td>
</tr>
</thead>
<tbody id="content">
@foreach (var item in Model)
{
<tr>
<td>@item.Id</td>
<td>@item.Title</td>
<td>@item.ReleaseDate</td>
<td>@item.Genre</td>
<td>@item.Price</td>
</tr>
}
</tbody>
</table>
</body>
</html>
7、安装iis
8、.net core 环境包 部署
下载 .NET 6.0 (Linux、macOS 和 Windows)