1:首先创建asp.net mvc3应用程序

MVC3+Linq to sql 显示数据库中数据表的数据

2:创建项目完成后 找到controllers文件鼠标右击选择添加控制器

MVC3+Linq to sql 显示数据库中数据表的数据

3 为models文件夹添加一个linq to sql类文件,然后把数据库中的数据库复制进来。如截图操作

MVC3+Linq to sql 显示数据库中数据表的数据

4:添加控制器好后会生成一个HomeController.cs类文件,其代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcTestData.Models;
namespace MvcTestData.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            TestDataContext txtData = new TestDataContext();
            var result=from info in txtData.StuTable
                       select info;
            ViewData["data"] = result;
            return View(result);
        }

    }
}
View Code

相关文章: