1. 程式人生 > >2018-Asp.net-MVC4教學 Linq,原生Sql等資料庫相關技術的應用及比較

2018-Asp.net-MVC4教學 Linq,原生Sql等資料庫相關技術的應用及比較

主要步驟如下:

A.建立資料庫SpaceSchool資料庫

資料中有兩張表和一個檢視,建立程式碼是:

USE [SpaceSchool]
GO

/****** 物件: Table [dbo].[Formteacher] 指令碼日期: 2018/4/23 星期一 17:05:45 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Formteacher] (
    [ID]              INT        IDENTITY (1, 1) NOT NULL,
    [FormteacherName] NCHAR (10) NULL
);
go


CREATE TABLE [dbo].[Student] (
    [ID]            INT        IDENTITY (1, 1) NOT NULL,
    [StuNo]         NCHAR (10) NULL,
    [StuName]       NCHAR (10) NULL,
    [Sex]           NCHAR (1)  NULL,
    [FormteacherID] INT        NULL
);
go
create view V_StudentInfo
as
select Student.ID,StuNo,StuName,FormteacherName
from Student,Formteacher
where Student.FormteacherID=Formteacher.ID
go
 

註解:Student表中的FormteacherID和Formteacher中的ID是外來鍵約束關係。

然後,給Formteacher和Student表中填寫測試資訊。

B.建立Asp.Net MVC4專案,模板採用International Application。

C.右擊Model,新增新專案 資料->ADO.NET 實體資料模,...,最終新增三個類檔案:Student.cs,Formteacher.cs,V_StudentInfo.cs

D.修改Web.config檔案,預設連結sql伺服器名稱改為上述資料庫所在的伺服器。

E.建立StudentInfo控制器,並增加兩個相關的Action。

首先編譯,然後右擊Controllers資料夾,新增StudentInfoController控制器,程式碼如下:

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

namespace MulTableQueryDemo.Controllers
{
    public class StudentInfoController : Controller
    {
        private SpaceSchoolEntities db = new SpaceSchoolEntities();

        //
        // GET: /StudentInfo/

        public ActionResult Index()
        {
            
           return View(db.V_StudentInfo.ToList());
           
        }
        public ActionResult LinqQuery()
        {
            var q = from s in db.Student
                    from f in db.Formteacher
                    where s.FormteacherID == f.ID
                    select new
                    {
                        s.ID,
                        s.StuNo,
                        s.StuName,
                        f.FormteacherName
                    };
            List<V_StudentInfo> Mylist = new List<V_StudentInfo>();
            foreach (var i in q)
            {
                V_StudentInfo item = new V_StudentInfo();
                item.ID = i.ID;
                item.StuNo = i.StuNo;
                item.StuName = i.StuName;
                item.FormteacherName = i.FormteacherName;
                Mylist.Add(item);

            }
            return View("Index",Mylist);
        }


        public ActionResult SqlQuery()
        {
          string strID = "001";
          var q=db.V_StudentInfo.SqlQuery("select * from V_StudentInfo where 
[email protected]
",strID);           return View("Index", q.ToList());         } // // GET: /StudentInfo/Details/5 public ActionResult Details(int id = 0) { V_StudentInfo v_studentinfo = db.V_StudentInfo.Find(id); if (v_studentinfo == null) { return HttpNotFound(); } return View(v_studentinfo); } // // GET: /StudentInfo/Create public ActionResult Create() { return View(); } // // POST: /StudentInfo/Create [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(V_StudentInfo v_studentinfo) { if (ModelState.IsValid) { db.V_StudentInfo.Add(v_studentinfo); db.SaveChanges(); return RedirectToAction("Index"); } return View(v_studentinfo); } // // GET: /StudentInfo/Edit/5 public ActionResult Edit(int id = 0) { V_StudentInfo v_studentinfo = db.V_StudentInfo.Find(id); if (v_studentinfo == null) { return HttpNotFound(); } return View(v_studentinfo); } // // POST: /StudentInfo/Edit/5 [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit(V_StudentInfo v_studentinfo) { if (ModelState.IsValid) { db.Entry(v_studentinfo).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(v_studentinfo); } // // GET: /StudentInfo/Delete/5 public ActionResult Delete(int id = 0) { V_StudentInfo v_studentinfo = db.V_StudentInfo.Find(id); if (v_studentinfo == null) { return HttpNotFound(); } return View(v_studentinfo); } // // POST: /StudentInfo/Delete/5 [HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public ActionResult DeleteConfirmed(int id) { V_StudentInfo v_studentinfo = db.V_StudentInfo.Find(id); db.V_StudentInfo.Remove(v_studentinfo); db.SaveChanges(); return RedirectToAction("Index"); } protected override void Dispose(bool disposing) { db.Dispose(); base.Dispose(disposing); } } }

F.修改相關View:

修改View/Shared/_Layout.cshtml檔案:

新增一行[學生資訊]相關連結程式碼:

<li>@Html.ActionLink("主頁", "Index", "Home")</li>

<li>@Html.ActionLink("學生資訊", "Index", "StudentInfo")</li>

<li>@Html.ActionLink("關於", "About", "Home")</li>

<li>@Html.ActionLink("聯絡方式", "Contact", "Home")</li>

修改View/StudentInfo中的Index.cshtml檔案。在Index.cshtml上方位置,新增三行程式碼:

<p>

@Html.ActionLink("Default Query", "Index")

@Html.ActionLink("Linq Query", "LinqQuery")

@Html.ActionLink("Sql Query", "SqlQuery")

</p>

G.小結:

linq語法有兩種形式,一種是查詢語法,一種是方法語法。原生Sql程式碼有兩個執行環境:一種是做到Sql伺服器中,一種是做到MVC控制器當中。