使用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>&nbsp;<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写的增删查案例的更多相关文章

  1. Django框架model实现数据库增删查改

    1.创建Django工程 https://www.cnblogs.com/CK85/p/10159159.html 2.在model.py中配置生成表格的类对象. from django.db imp ...

  2. 快速入门GreenDao框架并实现增删改查案例

    大家的项目中不可避免的使用到SQLite,为此我们要花费心思编写一个增删改查框架.而一个好的ORM框架则能够给我们带来极大的方便,今天给大家讲解一个非常火热的ORM-GreenDao. 基本概念 Gr ...

  3. ssm项目框架搭建(增删改查案例实现)——(SpringMVC+Spring+mybatis项目整合)

    Spring 常用注解 内容 一.基本概念 1. Spring 2. SpringMVC 3. MyBatis 二.开发环境搭建 1. 创建 maven 项目 2. SSM整合 2.1 项目结构图 2 ...

  4. 一套手写ajax加一般处理程序的增删查改

    倾述下感受:8天16次驳回.这个惨不忍睹. 好了不说了,说多了都是泪. 直接上代码 : 这个里面的字段我是用动软生成的,感觉自己手写哪些字段太浪费时间了,说多了都是泪 ajax.model层的代码: ...

  5. TP框架 增删查

    TP框架添加数据到数据库1.使用数组方式添加造模型对象 2.使用AR方式 强类型语言存在的方式 3.使用自动收集表单添加 :只能用POST方式,提交数据一个操作方法实现两个逻辑:A显示页面B得到数据 ...

  6. SSH框架的多表查询和增删查改 (方法一)中

    原创作品,允许转载,转载时请务必标明作者信息和声明本文章==>http://www.cnblogs.com/zhu520/p/7774144.html   这边文章是接的刚刚前一遍的基础上敲的  ...

  7. backbonejs mvc框架的增删查改实例

    一:开发环境 coffeescript和nodejs需要先安装,没装网上自己查安装步骤. 代码编写环境及esp框架下载: esp框架下载地址:https://github.com/nonocast/e ...

  8. SSH框架的多表查询和增删查改 (方法一)上

    原创作品,允许转载,转载时请务必标明作者信息和声明本文章==>  http://www.cnblogs.com/zhu520/p/7772823.html   因为最近在做Android 练习的 ...

  9. SSH框架的多表查询(方法二)增删查改

     必须声明本文章==>http://www.cnblogs.com/zhu520/p/7773133.html  一:在前一个方法(http://www.cnblogs.com/zhu520/p ...

随机推荐

  1. Kettle中并行执行测试

    整个作业截图: 设置并行方法:右键 START 组件,勾选最后一个选项: Run Next Entries In Parallel 设置aa, bb, cc, dd, ee 都是shell脚本,内容都 ...

  2. [C#] .NET4.0中使用4.5中的 async/await 功能实现异步

    在.NET Framework 4.5中添加了新的异步操作库,但是在.NET Framework 4.0中却无法使用.这时不免面临着抉择,到底是升级整个解决方案还是不使用呢? 如果你的软件还没发布出去 ...

  3. mp的猜猜看

    ~~~~|yjb1072452141---dc9339b4c33103abc4919375203e7a24|A1482583628---0142e0b6090b9b2838328445a79cd1b8 ...

  4. 一秒钟带你走进P图世界-----(python)PIL库的使用

    python-----PIL库的使用 一.什么是PIL库 1.PIL(Python Image Library)库是python语言的第三方库,具有强大的图像处理能力,不仅包含了丰富的像素.色彩操作功 ...

  5. Turtle库的学习积累

    1.什么是turtle库 Python的Turtle库是一个直观有趣的图形绘制函数库,Turtle英文翻译过来是乌龟的意思,在绘图时可以想象成一只乌龟在移动. 2.绘图坐标体系 海龟的移动方向 3.绘 ...

  6. Knockoutjs 响应式计算研究

    reactive programming https://en.wikipedia.org/wiki/Reactive_programming In computing, reactive progr ...

  7. Exp1:PC平台逆向破解 20164314 郭浏聿

    一.实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. 该程序同时包含另一个代码片段,getS ...

  8. Intellij IDEA 修改jsp 不能实时更新

    Intellij IDEA 修改jsp 不能实时更新 1. 首先,output要指定到项目的webapp下,这样应该就可以实时更新了 2. 我的问题是这样设置之后,也不可以,原来是可以的,重装系统之后 ...

  9. Hper-V卸载

    1.txt文件输入以下内容,后缀改为cmd,以管理员身份执行 mountvol X: /s copy %WINDIR%\System32\SecConfig.efi X:\EFI\Microsoft\ ...

  10. Codeforces Round #484 (Div. 2)Cut 'em all!(dfs)

    题目链接 题意:给你一棵树,让你尽可能删除多的边使得剩余所有的联通组件都是偶数大小. 思路:考虑dfs,从1出发,若当前节点的子节点和自己的数目是偶数,说明当前节点和父亲节点的边是可以删除的,答案+1 ...