# 期末考猜題紀錄 {%hackmd sEuzLBuLSqOjsLIpuIDRvg %} ## 接收任務吧!題目在這! 圖書查詢 1. 資料庫欄位需要有1.ISBN 2.書名(bookname) 3.作者(author)4.出版日期(publicationdate) 2. 共收了5本書,要有以上資訊 3. 可以新增刪除修改查詢 4. 可用書名或作者查詢,也能關鍵字查詢 5. 顯示資料 --- ### 1.建立資料庫 第一點和第二點完成~ :::info  1.1 建立欄位 1.ISBN 字串>數字會出事 2.書名(bookname) 可varchar(50) 3.作者(author) 4.出版日期(publicationdate) date就好 5.Table改成book  | ID | ISBN | bookname | author | publicationdate | | --- | ------------- | --------------------------- | --------------------| --------------- | | 1 | 9789573317241 | 哈利波特(1):神秘的魔法石 | J. K. Rowling | 2000/6/1 | | 2 | 9789573317586 | 哈利波特(2):消失的密室 | J. K. Rowling | 2000/12/25 | | 3 | 9789573318002 | 哈利波特(3):阿茲卡班的逃犯 | J. K. Rowling | 2001/7/1 | | 4 | 9789570841008 | 魔戒首部曲:魔戒現身 | J. R. R. Tolkien | 2012/12/10 | | 5 | 9789570841015 | 魔戒二部曲:雙城奇謀 | J. R. R. Tolkien | 2012/12/10 | 1.2 建立書本資訊 真的有這些書喔...(抱歉了魔戒3真的看不完...) ::: ### 2.建立MVC專案>新增刪除修改 第三點完成~ :::info  2.1 我建立了一個名叫finaltask的專案  2.2 先安裝~entityframework  2.3 建立model>我取名為bookmodel  2.4 建立controller>我取名為bookscontroller(記得建置) 2.5 先run一次看可不可以新增刪除修改~(記得app_start改index) ::: ### 3.新增查詢 完成第四點和第五點~ :::info  3.1 views>books>index.html>找到@Html.ActionLink("Create New", "Create")>在下方新增 ``` <a href="~/books/Search">Search</a> ```  3.2 control>bookscontroller>最下方新增 ``` public ActionResult Search() { return View(); } ```  3.3 在程式碼右鍵>新增檢視>取名Search的html  ``` <form method="post"> Search: <input type="text" id="search" name="search" /> <input type="submit" value="Search" /> </form> ``` 3.4 Search的html>新增textbox和按鈕 可先回到bookscontroller run看看有沒有成功~  3.5 bookscontroller>最下方新增 ``` public ActionResult Search(string search) { var result = db.book.Where(s => s.bookname.Contains(search) || s.author.Contains(search)).ToList(); return View(result);} ``` 這樣就可用書名或作者查詢,也能關鍵字查詢   ``` <div> @if (Model != null) { <table> <thead> <tr> <td> ISBN </td> <td> bookname </td> <td> author </td> <td> publicationdate </td> </tr> </thead> <tbody> @foreach(var a in Model) { <tr> <td> @a.ISBN </td> <td> @a.bookname </td> <td> @a.author </td> <td> @a.publicationdate </td> </tr> } </tbody> </table> } </div> ``` 3.6 再到Search的html>打上這些code>顯示資料!   ヾ(●´∀`●)大功告成 最後感謝我的好朋友ChatGPT協助  :::
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up