天天看點

C# MVC與sql server1、開發環境2、sql server身份驗證3、vs搭建mvc架構4、添加model5、添加controller6、配置資料庫7、效果8、代碼

C# MVC與sql server

  • 1、開發環境
  • 2、sql server身份驗證
  • 3、vs搭建mvc架構
  • 4、添加model
  • 5、添加controller
  • 6、配置資料庫
  • 7、效果
  • 8、代碼

1、開發環境

vs2017、sql server2017、

2、sql server身份驗證

1)伺服器名右擊-》屬性

C# MVC與sql server1、開發環境2、sql server身份驗證3、vs搭建mvc架構4、添加model5、添加controller6、配置資料庫7、效果8、代碼

2)

C# MVC與sql server1、開發環境2、sql server身份驗證3、vs搭建mvc架構4、添加model5、添加controller6、配置資料庫7、效果8、代碼

3)

C# MVC與sql server1、開發環境2、sql server身份驗證3、vs搭建mvc架構4、添加model5、添加controller6、配置資料庫7、效果8、代碼

4)更改密碼

C# MVC與sql server1、開發環境2、sql server身份驗證3、vs搭建mvc架構4、添加model5、添加controller6、配置資料庫7、效果8、代碼

5)

C# MVC與sql server1、開發環境2、sql server身份驗證3、vs搭建mvc架構4、添加model5、添加controller6、配置資料庫7、效果8、代碼

6)打開配置管理

C# MVC與sql server1、開發環境2、sql server身份驗證3、vs搭建mvc架構4、添加model5、添加controller6、配置資料庫7、效果8、代碼

7)配置管理中tcp/ip是否開啟

C# MVC與sql server1、開發環境2、sql server身份驗證3、vs搭建mvc架構4、添加model5、添加controller6、配置資料庫7、效果8、代碼

8)防火牆

C# MVC與sql server1、開發環境2、sql server身份驗證3、vs搭建mvc架構4、添加model5、添加controller6、配置資料庫7、效果8、代碼

7)開放端口為1433(預設)

C# MVC與sql server1、開發環境2、sql server身份驗證3、vs搭建mvc架構4、添加model5、添加controller6、配置資料庫7、效果8、代碼

8)開放sql server程式,一般位址為:C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER02【你的執行個體名】\MSSQL\Binn\sqlservr.exe

9)重新啟動sqlserver服務【必須】

ps:

1、某些需要改為自動

2、采用net stop XX,net start XX時注意XX是服務名

C# MVC與sql server1、開發環境2、sql server身份驗證3、vs搭建mvc架構4、添加model5、添加controller6、配置資料庫7、效果8、代碼
C# MVC與sql server1、開發環境2、sql server身份驗證3、vs搭建mvc架構4、添加model5、添加controller6、配置資料庫7、效果8、代碼

3、vs搭建mvc架構

1)

C# MVC與sql server1、開發環境2、sql server身份驗證3、vs搭建mvc架構4、添加model5、添加controller6、配置資料庫7、效果8、代碼

2)

C# MVC與sql server1、開發環境2、sql server身份驗證3、vs搭建mvc架構4、添加model5、添加controller6、配置資料庫7、效果8、代碼

4、添加model

C# MVC與sql server1、開發環境2、sql server身份驗證3、vs搭建mvc架構4、添加model5、添加controller6、配置資料庫7、效果8、代碼

5、添加controller

C# MVC與sql server1、開發環境2、sql server身份驗證3、vs搭建mvc架構4、添加model5、添加controller6、配置資料庫7、效果8、代碼
C# MVC與sql server1、開發環境2、sql server身份驗證3、vs搭建mvc架構4、添加model5、添加controller6、配置資料庫7、效果8、代碼

然後自動生成類似如下内容:

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

namespace fcTools.Controllers
{
    public class MovieMvcsController : Controller
    {
        private MovieMvcDBContext db = new MovieMvcDBContext();
        
        // GET: MovieMvcs
        public ActionResult Index()
        {
            return View(db.movies.ToList());
        }

        // GET: MovieMvcs/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            MovieMvc movieMvc = db.movies.Find(id);
            if (movieMvc == null)
            {
                return HttpNotFound();
            }
            return View(movieMvc);
        }

        // GET: MovieMvcs/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: MovieMvcs/Create
        // 為了防止“過多釋出”攻擊,請啟用要綁定到的特定屬性,有關 
        // 詳細資訊,請參閱 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "ID,title,relaseData,genere,price")] MovieMvc movieMvc)
        {
            if (ModelState.IsValid)
            {
                db.movies.Add(movieMvc);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(movieMvc);
        }

        // GET: MovieMvcs/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            MovieMvc movieMvc = db.movies.Find(id);
            if (movieMvc == null)
            {
                return HttpNotFound();
            }
            return View(movieMvc);
        }

        // POST: MovieMvcs/Edit/5
        // 為了防止“過多釋出”攻擊,請啟用要綁定到的特定屬性,有關 
        // 詳細資訊,請參閱 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "ID,title,relaseData,genere,price")] MovieMvc movieMvc)
        {
            if (ModelState.IsValid)
            {
                db.Entry(movieMvc).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(movieMvc);
        }

        // GET: MovieMvcs/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            MovieMvc movieMvc = db.movies.Find(id);
            if (movieMvc == null)
            {
                return HttpNotFound();
            }
            return View(movieMvc);
        }

        // POST: MovieMvcs/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            MovieMvc movieMvc = db.movies.Find(id);
            db.movies.Remove(movieMvc);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}

           
C# MVC與sql server1、開發環境2、sql server身份驗證3、vs搭建mvc架構4、添加model5、添加controller6、配置資料庫7、效果8、代碼

6、配置資料庫

C# MVC與sql server1、開發環境2、sql server身份驗證3、vs搭建mvc架構4、添加model5、添加controller6、配置資料庫7、效果8、代碼

驗證連接配接成功後在項目下的Web.config中如下配置:

C# MVC與sql server1、開發環境2、sql server身份驗證3、vs搭建mvc架構4、添加model5、添加controller6、配置資料庫7、效果8、代碼

7、效果

進入建立

C# MVC與sql server1、開發環境2、sql server身份驗證3、vs搭建mvc架構4、添加model5、添加controller6、配置資料庫7、效果8、代碼

點選create:

C# MVC與sql server1、開發環境2、sql server身份驗證3、vs搭建mvc架構4、添加model5、添加controller6、配置資料庫7、效果8、代碼

點選edit:

C# MVC與sql server1、開發環境2、sql server身份驗證3、vs搭建mvc架構4、添加model5、添加controller6、配置資料庫7、效果8、代碼

點選save:

C# MVC與sql server1、開發環境2、sql server身份驗證3、vs搭建mvc架構4、添加model5、添加controller6、配置資料庫7、效果8、代碼

8、代碼

github