ASP.NET MVC4 調用存儲過程

BooksController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Configuration;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using MvcApplication1.Models;
namespace MvcApplication1.Controllers
{
    public class BooksController : Controller
    {
        //
        // GET: /Books/

        string conf = ConfigurationManager.ConnectionStrings["MYSQL"].ConnectionString;
        /// <summary>
        /// 圖書列表
        /// </summary>
        /// <returns></returns>
        public ActionResult List()
        {
             DataSet ds=null;
            using (SqlConnection conn = new SqlConnection(conf))
            {
                conn.Open();
                SqlDataAdapter adapter = new SqlDataAdapter("selectAllBooks", conn);
                adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
                ds= new DataSet();
                adapter.Fill(ds);

            }
            List<Books> books = new List<Books>();
            DataTable dt=ds.Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                books.Add(new Books() { Id = Convert.ToInt32(dt.Rows[i]["Id"]), Title = dt.Rows[i]["Title"].ToString() });
            }
            ViewData["books"] = books;
                return View();
        }
        /// <summary>
        /// 圖書詳情
        /// </summary>
        /// <returns></returns>
        public ActionResult Detail()
        {
            int id = Convert.ToInt32(RouteData.Values["id"]);
            ////
            DataSet ds = null;
            using (SqlConnection conn = new SqlConnection(conf))
            {
                conn.Open();
                SqlDataAdapter adapter = new SqlDataAdapter("selectabook", conn);
                adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
                SqlParameter param = new SqlParameter("@id", SqlDbType.Int);
                param.Value = id;
                adapter.SelectCommand.Parameters.Add(param);
                ds = new DataSet();
                adapter.Fill(ds);
            }
            DataTable dt = ds.Tables[0];
            Books book = new Books(){Id = Convert.ToInt32(dt.Rows[0]["Id"]), 
                                    Title = dt.Rows[0]["Title"].ToString(),
                                    Author=dt.Rows[0]["Author"].ToString(),
                                     ISBN = dt.Rows[0]["ISBN"].ToString()
            };
            return View(book);
        }

    }
}

List.aspx

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<!DOCTYPE html>

<html>
<head runat="server">
    <meta name="viewport" content="width=device-width" />
    <title>List</title>
</head>
<body>
    <div>
        <%
            List<MvcApplication1.Models.Books> list = ViewData["books"] as List<MvcApplication1.Models.Books>;
           
         %>
        <table border="1">
            <%
                for (int i = 0; i < list.Count; i++)
                {
                    Response.Write("<tr>");
                    Response.Write("<td>");
                    Response.Write(list[i].Id);
                    Response.Write("</td>");
                    Response.Write("<td>");
                    Response.Write(list[i].Title);
                    Response.Write("</td>");
                    Response.Write("<td>");
                       string url= Url.Action("Detail", "Books", new { id = list[i].Id });
                       string aurl = "<a href=" + url + ">查看詳情</a>";
                    Response.Write(aurl);
                    Response.Write("</td>");
                    Response.Write("</tr>");
                }
                 %>
        </table>
    </div>
</body>
</html>

Detail.aspx

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MvcApplication1.Models.Books>" %>

<!DOCTYPE html>

<html>
<head runat="server">
    <meta name="viewport" content="width=device-width" />
    <title>Detail</title>
</head>
<body>
    <div>
       標題: <%=Model.Title %><br />
        作者: <%=Model.Author %><br />
         ISBN: <%=Model.ISBN %><br />
    </div>
</body>
</html>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章