AspNet MVC4 教学-26:Asp.Net MVC4 原生态Sql技术快速应用Demo

A.创建Basic类型项目.

B.在Model目录下面创建以下文件:

Student.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;

namespace MvcSqlTest.Models
{
    public class Student
    {
        public int Id { set; get; }
        public string Name { set; get; }
        public int Age { set; get; }
        public string  Class { set; get; }
        [NotMapped]
        public string Country
        {
            get
            {
                return "China";
            }
        }
    }
}

C.创建Controller:

HomeController.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcSqlTest.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View();
        }

    }
}
StudentController.cs:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcSqlTest.Models;
using System.Transactions;

namespace MvcSqlTest.Controllers
{
    public class StudentController : Controller
    {
        private MvcSqlTestContext db = new MvcSqlTestContext();

        public ActionResult Index()
        {
            return View(db.Students.ToList());
        }
       
        public ActionResult SelectSql(string Name)
        {
            Student student = db.Students.SqlQuery("select * from Students where Name=@p0", Name).Single();
            return View("Details",student);
        }
        public ActionResult DeleteSql(string Name)
        {
            using (TransactionScope ts = new TransactionScope())
            {
              
                    string sql = "delete from Students where Name={0}";
                    db.Database.ExecuteSqlCommand(sql, Name);               
                    ts.Complete();
       
            }
            return RedirectToAction("Index");
        }
     

        public ActionResult Create()
        {
            return View();
        }
     

        [HttpPost]
        public ActionResult Create(Student student)
        {
            if (ModelState.IsValid)
            {
                db.Students.Add(student);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(student);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
D.创建相应的View:
Home/Index.cshtml:

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
<h2>@Html.ActionLink("学生管理","Index","Student")</h2>
提示:通过平台技术以及Entity Framework技术产生Student目录下的View:

Create.cshtml,Details.cshtml,Index.cshtml,同时产生相应的数据库文件.

再一次,修改Index.cshtml:

@model IEnumerable<MvcSqlTest.Models.Student>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
@using (Html.BeginForm("SelectSql","Student"))
{
    @Html.TextBox("Name");
    <input type="submit" value="查询" />
}
@using (Html.BeginForm("DeleteSql","Student"))
{
    @Html.TextBox("Name");
    <input type="submit" value="删除" />
}
<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Age)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Class)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Country)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Age)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Class)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Country)
        </td>       
    </tr>
}

</table>
E.主页启动后,通过学生管理,增加学生。然后,测试[查询]和[删除]功能.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gCodeTop 格码拓普 老师

您的鼓励.我的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值