运用SqlSugar框架+Axios写的增删查案例
使用SqlSugar框架需要引用NuGet程序包否则会出现报错。
前台页面创建代码:
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<link href="~/Content/bootstrap.min.css" rel="stylesheet" />
<script src="~/Scripts/vue.min.js"></script>
<script src="~/Scripts/axios.min.js"></script>
<div id="app">
<table style="width:80%;margin:auto;" class="table table-bordered table-hover">
<tr>
<th>姓名:</th>
<td><input type="text" v-model="user.UserName" /></td>
</tr>
<tr>
<th>手机号:</th>
<td>
<input type="text" v-model="user.PhoneNumber" />
</td>
</tr>
<tr>
<th>密码:</th>
<td><input type="text" v-model="user.UserPassword" /></td>
</tr>
<tr>
<th>状态:</th>
<td><input type="text" v-model="user.UserState" /></td>
</tr>
<tr>
<td colspan="2"><button v-on:click="add" class="btn btn-info">添加</button></td>
</tr>
</table>
<table class="table table-bordered table-hover">
<tr>
<td>编号</td>
<td>用户名</td>
<td>手机号</td>
<td>密码</td>
<td>状态</td>
<td>操作</td>
</tr>
@foreach (var item in ViewBag.data)
{
<tr>
<td>@item.UserID</td>
<td>@item.UserName</td>
<td>@item.PhoneNumber</td>
<td>@item.UserPassword</td>
<td>@item.UserState</td>
<td><a href="/Home/Delete/@item.UserID">删除</a> <a href="#">修改</a></td>
</tr>
}
</table>
</div>
<script>
new Vue({
el: "#app",
data: {
user: { UserName: "", PhoneNumber: "", UserPassword: "", UserState: 0 },
} ,
methods: {
add: function () {
axios.post('/Home/Insert', { users:this.user }).then(
res => {
window.location.href = "/Home/Index"
}).catch(
error => {
console.log(error);
});
}
}
});
</script>
创建Config.CS链接数据库
public class Config
{
/// <summary>
/// 数据库连接字符串(私有字段)
/// </summary>
private static readonly string _connectionString = System.Configuration.
ConfigurationManager.ConnectionStrings["SqlServerConn"].ToString();
/// <summary>
/// 数据库连接字符串(公有属性)
/// </summary>
public static string ConnectionString
{
get { return _connectionString; }
}
}
创建DBSuglar.CS
public class DBSuglar
{
public static SqlSugarClient GetSqlSugarClient()
{
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = Config.ConnectionString,//必填, 数据库连接字符串
DbType = DbType.SqlServer, //必填, 数据库类型
IsAutoCloseConnection = true, //默认false, 时候知道关闭数据库连接, 设置为true无需使用using或者Close操作
InitKeyType = InitKeyType.SystemTable //默认SystemTable, 字段信息读取, 如:该属性是不是主键,是不是标识列等等信息
});
return db;
}
}
创建实体类User.cs
public class User
{
public List<UserInfo> Show()
{
return Users.GetAll().ToList();
}
public bool Delete(int id)
{
return Users.Delete(id);
}
public bool Insert(UserInfo us)
{
return Users.Insert(us);
}
}
创建Home控制器
public class HomeController : Controller
{
public static SqlSugarClient db = DBSuglar.GetSqlSugarClient();
User us = new User();
/// <summary>
/// 查询
/// </summary>
/// <returns></returns>
public ActionResult Index()
{
ViewBag.data = db.SqlQueryable<UserInfo>("select * from UserInfo").ToList();
return View();
}
/// <summary>
/// 删除
/// </summary>
/// <param name="id">删除的主键</param>
/// <returns></returns>
public ActionResult Delete(int id)
{
db.Deleteable<UserInfo>().Where(it => it.UserID == id).ExecuteCommand();
return RedirectToAction("Index");
}
/// <summary>
/// 添加
/// </summary>
public ActionResult Insert(UserInfo users)
{
var i= db.Insertable(users).ExecuteReturnBigIdentity();
return Json(i,JsonRequestBehavior.AllowGet);
}
}
整体页面效果
运用SqlSugar框架+Axios写的增删查案例的更多相关文章
- Django框架model实现数据库增删查改
1.创建Django工程 https://www.cnblogs.com/CK85/p/10159159.html 2.在model.py中配置生成表格的类对象. from django.db imp ...
- 快速入门GreenDao框架并实现增删改查案例
大家的项目中不可避免的使用到SQLite,为此我们要花费心思编写一个增删改查框架.而一个好的ORM框架则能够给我们带来极大的方便,今天给大家讲解一个非常火热的ORM-GreenDao. 基本概念 Gr ...
- ssm项目框架搭建(增删改查案例实现)——(SpringMVC+Spring+mybatis项目整合)
Spring 常用注解 内容 一.基本概念 1. Spring 2. SpringMVC 3. MyBatis 二.开发环境搭建 1. 创建 maven 项目 2. SSM整合 2.1 项目结构图 2 ...
- 一套手写ajax加一般处理程序的增删查改
倾述下感受:8天16次驳回.这个惨不忍睹. 好了不说了,说多了都是泪. 直接上代码 : 这个里面的字段我是用动软生成的,感觉自己手写哪些字段太浪费时间了,说多了都是泪 ajax.model层的代码: ...
- TP框架 增删查
TP框架添加数据到数据库1.使用数组方式添加造模型对象 2.使用AR方式 强类型语言存在的方式 3.使用自动收集表单添加 :只能用POST方式,提交数据一个操作方法实现两个逻辑:A显示页面B得到数据 ...
- SSH框架的多表查询和增删查改 (方法一)中
原创作品,允许转载,转载时请务必标明作者信息和声明本文章==>http://www.cnblogs.com/zhu520/p/7774144.html 这边文章是接的刚刚前一遍的基础上敲的 ...
- backbonejs mvc框架的增删查改实例
一:开发环境 coffeescript和nodejs需要先安装,没装网上自己查安装步骤. 代码编写环境及esp框架下载: esp框架下载地址:https://github.com/nonocast/e ...
- SSH框架的多表查询和增删查改 (方法一)上
原创作品,允许转载,转载时请务必标明作者信息和声明本文章==> http://www.cnblogs.com/zhu520/p/7772823.html 因为最近在做Android 练习的 ...
- SSH框架的多表查询(方法二)增删查改
必须声明本文章==>http://www.cnblogs.com/zhu520/p/7773133.html 一:在前一个方法(http://www.cnblogs.com/zhu520/p ...
随机推荐
- IDEA+Maven+Spring MVC HelloWorld示例
用Maven创建Web项目 选择webapp模板 创建成功后点Enable Auto-Import idea给我们创建出来的结构是这样的,这还不标准,需要自己修改. 在main文件夹下创建java文件 ...
- 基于jeesite的cms系统(二):整体设计
一.菜单设计 在系统管理-菜单管理中可以设置内容管理菜单(自动生成) 注意:归属模块应属于核心模块core.如果新加的的菜单设置为内容管理模块cms,系统下次重启时会重置本次设置,具体原因不详. 二. ...
- 爬虫框架Scrapy 之(一) --- scrapy初识
Scrapy框架简介 scrapy是基于Twisted的一个第三方爬虫框架,许多功能已经被封装好,方便提取结构性的数据.其可以应用在数据挖掘,信息处理等方面.提供了许多的爬虫的基类,帮我们更简便使用爬 ...
- shell拼写检查,利用Linux字典
rename #find path -type f -name "*.mp3" -exec mv { } target_dir \; #mp3 file mv dir_file ...
- python学习05
数据类型之字典dict.set集合 1).字典dict 1. dict_1={'name':'tom','age':18} 是以键值对(key-value)的方式,其中键是可hash值的,即表示键是唯 ...
- Linux-Shell编程之求命令行中所有整数之和
前言 复习shell编程知识,书中有此练习题,之前学习时便没有搞透彻,此次是非啃下不可. 但是,摸索许久,却屡碰南墙,于是查阅网络博客,结果有让人比学不懂知识更让生气的是:博文遍地,净是抄袭!连个截图 ...
- 毕业设计——Django邮件发送功能实现及问题记录
django发送邮件:send_mail()&send_mass_mail() 自强学堂 刘江的博客 HTTPS,TLS和SSL django发送邮件及其相关问题 步骤 : 0. 登录作为发送 ...
- UE4 行为树资料
Composites Select 选择 从左往右执行其子节点,直到一个达成,则 Select 达成并返回上层,否则失败并返回上层 Sequence 队列 从左往右执行其子节点,直到一个失败,则 Se ...
- Spring框架-IOC/DI详细学习
一.IOC/DI概念 参考博客:https://www.cnblogs.com/xdp-gacl/p/4249939.html IOC(inversion of control, 控制反转)是一种设计 ...
- Day04.a(对象类型的转换,多态)
对象类型的转换 Dog dog = new Dog(); 通常情况下,对象(new Dog())类型和引用(dog)类型是相同的,当引用类型和对象类型不一致时,就需要类型转换. 向上转型:将较具体的类 ...